0

pretty self explanatory. I need assistance solving the memory leak problem. Please have a look at the codes and screenshots below. I am happy to answer any additional questions or clarify more :)

I am tasked with building a Stack Data structure (LIFO). In doing so, I am having some trouble freeing up heap memory that I malloc. I am using mystack.h as the header file, and main.c to test my program. Both are requirements. V.Imp: I am not allowed to change the signature of any function.

1 Answers1

2

In the stack_enqueue function you have these two lines:

node_t *n = (node_t*)malloc(sizeof(node_t));
s->head[s->count++] = *n;

The first line dynamically allocates memory. The second line copies from where n is pointing. Then you simply discard the pointer n and the memory it points to. This is indeed a memory leak.

The simple solution is to add:

free(n);

after the assignment.

There's a slight problem with this and your whole "list" implementation though, because what you have isn't really a list, but a simple array of structure objects. You could replace relevant lines of stack_enqueue with e.g.:

s->head[s->count++].data = item;

and then not bother with the list part at all (essentially remove the next member of the structure, together with all "list" handling).


I think you misunderstand the purpose of the exercise. If s->head is a pointer, it should not be treated as an array but a pointer to the first node of the list.

So you would have something like

+---------+    +------------+    +-------------+           +-----------+
| s->head | -> | first node | -> | second node | -> ... -> | last node | --> NULL
+---------+    +------------+    +-------------+           +-----------+

That is, s->head points to the first node. The first nodes next pointer points to the second node, etc.

You don't have linking of nodes like this at all in your current code.

If you have trouble with understanding linked lists, your text-books should have information about it, and your class should have brought it up as well. And there must be millions of resources available all over the greater Internet about how to implement hand handle lists.

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
  • some of the things I cant change are: Signiture of all functions, whats inside of typedef struct node, and typedef structt stack. So unfortunately I would have to make use of next i believe – Khalid Ayaz Khan May 26 '20 at 13:04
  • I added free(n); right below those 2 lines, but unfortunately does not work still – Khalid Ayaz Khan May 26 '20 at 13:34
  • There is segmentation error with the free(n); and there is memory leak without the free(n); – Khalid Ayaz Khan May 26 '20 at 13:35
  • @KhalidAyazKhan If you catch the crash in a debugger, I'll bet it's not because of the `free` call, but because you probably still use `s->head` as an array instead of a list. The whole loop "freeing" nodes in `free_stack` is also wrong. And you haven't shown us the `create_stack` function, which might also contain errors. All in all, this is really becoming much to broad as it needs to be a tutorial about linked lists, and this isn't really a tutorial site. But as mentioned there must be millions of tutorials all over the Internet (as well as in your text book or class). – Some programmer dude May 26 '20 at 14:56
  • sorry about that, some parts of the code was missing. I have updated it now – Khalid Ayaz Khan May 26 '20 at 15:02