我已经实现了一个简单的链表,但是在删除我使用的所有指针后,它似乎并没有释放内存。我希望有人能指出我在这里缺少的东西。我正在使用活动监视器查看 xcode 的内存使用情况,也许这是查看内存使用情况的不准确方法?我不确定。这是代码:
#include <iostream>
struct node {
int data;
node *next;
};
//function to traverse linked list
void traverse(node *root){
node *trav;
std::cout << "The list is as follows:\n";
//traverses through lists
trav = root;
do{
std::cout << (*trav).data << "\n";
trav = trav->next;
}while(trav != NULL);
std::cout << "\n";
delete trav;
}
void addToList(node *root){
node *tmp;
tmp = root;
if(tmp->next != NULL){
while(tmp->next !=0){
tmp = tmp->next;
}
}
//adds new nodes to list
for(int i = 0 ; i<1000000 ; i++){
tmp->next = new node;
tmp = tmp->next;
tmp->data = i;
}
tmp->next = NULL;
delete tmp;
}
int main(int argc, const char * argv[])
{
node *root; //this is the root node. it will not change
//sets up root node
root = new node; //root points to a node structure (this is a memory address)
root->next=NULL; //next pointer is now "0"
root->data = 5; //data in this node = 5
//print initial list
traverse(root);
//add to list
addToList(root);
//print new list
traverse(root);
delete root;
return 0;
}
===================编辑 - 美国东部标准时间上午 9:28,1 月 27 日===================== ===========
感谢大家在这里的精彩反馈。我知道我做错了很多事情(这是我对 C++ 和动态内存分配的介绍。感谢您的帮助!)
请在下面找到更新的代码。我遇到了以下问题: 1 - 再次,活动监视器报告的实际内存使用情况没有从运行它的进程中释放(从终端运行和 xcode 都显示这个)。当发生以下故障时会发生这种情况,而且当我将 addToList 中的 for 循环设置为远低于 65514 时也会发生这种情况。--> 已修复 - 这不再是一个问题(缺少一对括号,导致我的代码无法正确删除。哎呀!)
2 -- 在 data = 65514 的节点,我得到: ~node 第一行的 EXC_BAD_ACCESS (code 2) -- 为什么?Xcode 提供以下内容:
Exception State Registers
trapno unsigned int
err unsigned int
faultvaddr unsigned long
Floating Point Registers
fctrl unsigned short
fstat unsigned short
ftag unsigned char
fop unsigned short
fioff unsigned int
fiseg unsigned short
fooff unsigned int
foseg unsigned short
mxcsr unsigned int
mxcsrmask unsigned int
stmm0 <invalid>
stmm1 <invalid>
stmm2 <invalid>
stmm3 <invalid>
stmm4 <invalid>
stmm5 <invalid>
stmm6 <invalid>
stmm7 <invalid>
xmm0 <invalid>
xmm1 <invalid>
xmm2 <invalid>
xmm3 <invalid>
xmm4 <invalid>
xmm5 <invalid>
xmm6 <invalid>
xmm7 <invalid>
xmm8 <invalid>
xmm9 <invalid>
xmm10 <invalid>
xmm11 <invalid>
xmm12 <invalid>
xmm13 <invalid>
xmm14 <invalid>
xmm15 <invalid>
General Purpose Registers
rax unsigned long
rbx unsigned long 0x0000000000000000
rcx unsigned long
rdx unsigned long
rdi unsigned long
rsi unsigned long
rbp unsigned long 0x00007fff6ca0c210
rsp unsigned long 0x00007fff6ca0c1c0
r8 unsigned long
r9 unsigned long
r10 unsigned long
r11 unsigned long
r12 unsigned long 0x0000000000000000
r13 unsigned long 0x0000000000000000
r14 unsigned long 0x0000000000000000
r15 unsigned long 0x0000000000000000
rip unsigned long 0x000000010d60cb96
rflags unsigned long
cs unsigned long
fs unsigned long
gs unsigned long
我认为这与无符号短路的限制有关,如在某些寄存器中所见,但是,我没有使用无符号短路。???!?!
这是我当前的代码:
#include <iostream>
struct node {
long data;
node *next;
node():next(NULL){} //just to be safe, initialize as null to start
//destructor used to clean up list nodes
~node(){
std::cout << data << " ";
if(next != NULL){
std::cout << data << ": deleted\n";
delete next;
next = NULL;
}
}
};
//function to traverse linked list
void traverse(node *root){
node *trav;
std::cout << "The list is as follows:\n";
//traverses through lists
trav = root;
do{
std::cout << (*trav).data << "\n";
trav = trav->next;
}while(trav != NULL);
std::cout << "\n";
}
void addToList(node *root){
node *tmp;
tmp = root;
if(tmp->next != NULL){
while(tmp->next !=0){
tmp = tmp->next;
}
}
//adds new nodes to list
for(long i = 0 ; i<100000 ; i++){
tmp->next = new node;
tmp = tmp->next;
tmp->data = i;
}
tmp->next = NULL;
}
int main(int argc, const char * argv[])
{
node *root; //this is the root node. it will not change
//sets up root node
root = new node; //root points to a node structure (this is a memory address)
root->next=NULL; //next pointer is now "0"
root->data = 5; //data in this node = 5
//print initial list
//traverse(root);
//add to list
addToList(root);
//print new list
//traverse(root);
delete root;
return 0;
}