Basic Malloc/Free

喜你入骨 提交于 2020-01-11 09:52:33

问题


If I have a snippit of my program like this:

struct Node *node;
while(...){
    node = malloc(100);
    //do stuff with node
}

This means that every time I loop through the while loop I newly allocate 100 bytes that is pointed to by the node pointer right?

If this is true, then how do I free up all the memory that I have made with all the loops if I only have a pointer left pointing to the last malloc that happened?

Thanks!


回答1:


Please allocate exactly the size you need: malloc(sizeof *node); -- if you move to a 64-bit platform that doubles the size of all your members, your old 96-byte structure might take 192 bytes in the new environment.

If you don't have any pointers to any of the struct Nodes you have created, then I don't think you should be allocating them with malloc(3) in the first place. malloc(3) is best if your application requires the data to persist outside the calling scope of the current function. I expect that you could re-write your function like this:

struct Node node;
while(...){
    //do stuff with node
}

or

while(...){
    struct Node node;
    //do stuff with node
}

depending if you want access to the last node (the first version) or not (the second version).

Of course, if you actually need those structures outside this piece of code, then you need to store references to them somewhere. Add them to a global list keeping track of struct Node objects, or add each one to the next pointer of the previous struct Node, or add each one to a corresponding struct User that refers to them, whatever is best for your application.




回答2:


If you set node = NULL before the loop and then use free(node) before node = malloc(100) you should be OK. You will also need to do a free(node) after the loop exits. But then again, it all depends on what "//do stuff with node" actually does. As others have pointed out, malloc(100) is not a good idea. What I would use is malloc(sizeof(*node)). That way, if the type of node changes, you don't have to change the malloc line.




回答3:


If you don't need the malloc'ed space at the end of one iteration anymore, you should free it right away.

To keep track of the allocated nodes you could save them in a dynamically growing list:

#include <stdlib.h>

int main() {
    int i;
    void *node;
    int prt_len = 0;
    void **ptrs = NULL;
    for (i = 0; i < 10; i++) {
        node = malloc(100);
        ptrs = realloc(ptrs, sizeof(void*) * ++prt_len);
        ptrs[prt_len-1] = node;
        /* code */
    }
    for (i = 0; i < prt_len; i++) {
        free(ptrs[i]);
    }
    free(ptrs);
    return 0;
}

Note: You should probably re-think your algorithm if you need to employ such methods!

Otherwise see sarnold's answer.




回答4:


then how do I free up all the memory that I have made with all the loops if I only have a pointer left pointing to the last malloc that happened?

You can't. You just created a giant memory leak.

You have to keep track of every chunk of memory you malloc() and free() it when you're done using it.




回答5:


You can not. You need to store all the pointer to free the memory. if you are saving those pointer somewhere then only you can free the memory.



来源:https://stackoverflow.com/questions/7942178/basic-malloc-free

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!