Free memory allocated in a different function?

前端 未结 3 1113
孤街浪徒
孤街浪徒 2021-02-01 12:05

I\'m trying to learn C and I\'m currently trying to write a basic stack data structure, but I can\'t seem to get basic malloc/free right.

Here\

3条回答
  •  感情败类
    2021-02-01 12:36

    Whenever you pass a parameter to a function, a copy is made, and the function works on that copy. So in your case, you are trying to free a copy of the original object, which doesn't make any sense.

    You should modify your function to take a pointer, and then you can have it call free directly on that pointer.

提交回复
热议问题