Home:ALL Converter>Memory Leak in C

Memory Leak in C

Ask Time:2009-11-27T10:19:44         Author:Midnight Blue

Json Formatter

I am a C beginner, and I am writing a very simple linked-list. I am wondering if there would be a memory leak in the following code:

void removeListEntry(struct tableEntry *symp, struct tableEntry *previous) {
  if (symp->next = 0){
    symbolList.tail = previous;
    previous->next =0;
  } else {
    previous->next = symp->next;
    symp->next = 0;
  }
}

I am pretty sure if the pointer symp is not stored in another variable, there's no way of accessing the list entry that was pointed by the pointer, thus I will have a memory leak. In C, we use malloc() function to allocate memory space for a data structure, and I remember using new keyword to "dynamically" allocate memory in C++. What are the differences between allocating memory using malloc() and using new? Is there indeed a memory leak in my code?

Author:Midnight Blue,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/1806483/memory-leak-in-c
sth :

if (symp->next = 0) {\n\n\nThis if-\"condition\" is an assignment, setting symp->next to 0. If a pointer to another object was stored in symp->next, that object is lost and the objects memory will not be freed.\n\nFor a comparision you need to use == instead:\n\nif (symp->next == 0) {\n\n\nor do it without an explicit comparision:\n\nif (!symp->next) {\n\n\nIn the else case you remove symp from the list (assuming previous actually contains the element before symp), but you don't free it's memory. This might be a memory leak, but it depends on the code calling the function: That code might still free symp or do something else with the removed element, or it might just forget about it and leak it's memory.",
2009-11-27T02:31:02
James Black :

I am curious about what is supposed to be happening in your code:\n\nif (symp->next = 0){\n symbolList.tail = previous;\n previous->next =0;\n} else {\n previous->next = symp->next;\n symp->next = 0;\n}\n\n\nWhen would symb->next not be zero? You aren't doing anything if symb is empty as the head node would also be null.\n\nThe confusing part is that you are appending previous to symb in the first if (which should always be the case) but in the next one you are appending symb to previous. What rationale is there for this second one, and in what case will it ever happen?\n\nAs others have mentioned, if you allocate memory you need to free it, else you have a memory leak, as there is no garbage collector in C/C++, so every node that is going to be freed needs to be deallocated.\n\nThe symb->next = 0 is probably just a typo, as was pointed out, as that will always be true, and is a frequent bug. What I started to do to help catch this is to do:\nif (0 == symb->next), so if you did 0=symb->next then you will get a compiler error.\n\nUPDATE:\n\nAs was pointed out in a comment, this function will always go to the 'else' clause, which may be expected behavior, actually.",
2009-11-27T02:42:56
Simon Righarts :

You need to call free() once you've finished using memory alloc'd by malloc().",
2009-11-27T02:24:07
yy