[英]C++ Is it possible to leak memory if i'm not using dynamic memory
None of my code uses dynamic memory, but I do have a vector of pointers to a struct called Node
, and in my code, I do lose references to those Node
s at one point. 我的代码都没有使用动态内存,但是我确实有一个指向Node
的结构的指针向量,并且在我的代码中,确实确实丢失了对那些Node
的引用。 The struct looks like this: 该结构如下所示:
struct Node {
int value;
Node* next;
};
I also have a for loop that tries to find the smallest value in my vector of Node pointers by taking the smallest Node
off as I go. 我也有一个for循环,它试图通过删除最小的Node
来寻找我的Node指针向量中的最小值。 Here, lists is the vector of Node
pointers, and add is the previous smallest value. 在这里,列表是Node
指针的向量,而添加是先前的最小值。
for (int i = 1; i < int(lists.size()); ++i) {
if (lists[i]->value <= add) {
add = lists[i]->value;
lists[i] = lists[i]->next;
break;
}
}
I thought I couldn't leak memory if I was just in the stack though... 我以为如果我只是在堆栈中就不会泄漏内存...
If the Node
referenced in the lists
array is dynamically allocated, you should free all of them manually. 如果在lists
数组中引用的Node
是动态分配的,则应手动释放它们。 Otherwise there will be memory leak. 否则会发生内存泄漏。 You can find more details on https://en.wikipedia.org/wiki/Memory_leak 您可以在https://en.wikipedia.org/wiki/Memory_leak上找到更多详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.