开发者

Is this use of C pointers safe from leaking memory?

char *a() {
    char *t = malloc(8);
    t[0] = 'a'; 
    t[1] = 'b';
    //...
    t[7] = 'h';
    return t;
}

int m开发者_开发问答ain(void) {
    char *x = a();
    //do something with x 
    //...
    free(x);
    return 0;
}

Does this code have any potential problems since memory is allocated in a() and used memory in main()?


First, a() is declared as returning void, but you attempt to return a char*. Change the signature to return a char*.

Second, your function is fine, but your example code has a memory leak because you never free the memory that the returned pointer points to.

Third, as gbrandt pointed out, you are not checking for success after the call to malloc. malloc can fail, and checking to see if it did is a good habit to get into.


Another way to accomplish this would be to pass a pointer to a pointer into a() instead, and then the caller has to create the pointer themselves before passing it to a(), but either way you still need to free the memory. To be honest, I would go with your approach over this in this case. There is no compelling reason to do it this way, just thought I would mention it.

void a(char **p)
{
    *p = malloc(8);
    if (*p)
    {
        **p[0] = 'a'; 
        **p[1] = 'b';
        ...
        **p[7] = 'h';
    }
}

int main(void)
{
    char *x;
    a(&x);
    //do something with x 
    .....
    free(x);
}

If this alternate approach confuses you, please let me know as I would be happy to provide an explanation (though, at the moment, I need to get back to work!)


All good advice above. Small problems with the code, the big one is...

You are not checking for success in the malloc or after the function call. Never forget error handling.


No problems—in fact, this is the correct way to allocate memory that will still be used after a function returns—but you may want to free() the memory when you're done using it. Eight bytes wouldn't be a problem, but coding against memory leaks is a good habit to get into.


Apart from having the wrong return type on a() (should be char * instead of void), the code doesn't have any problems.

Just be sure to free() the memory you allocated when you're done with it.


Most of the answers here indicate that this isn't a problem, just remember to free() it later. This is technically true, but is really a problem. Any time you allocate memory in one scope and expect it to be freed in another, you are asking for a memory leak. People don't remember to free the memory. There is also the problem that the caller will have to know that you used malloc and not alloca() or new() or something else. If they call anything but the matched deallocation routine, the results are undefined.

In short, it is usually a mistake to allocate memory and pass it back to the caller.

It is better to expect the memory to be allocated by the caller because if they allocate it, they will remember to free it and to do so correctly.


No you won't have any problems explicitly because you allocated the function in a and use it in main.


As long as you remember to free the memory that x points to then you won't have any problems. By using malloc() you've allocated memory on the heap which is left alone when returning to the calling function.


Thanks. I just edited the code. I guess this time it should have no problem. Btw, in C, when a function finishes, does the compiler clear all variables (including array & pointer) in the stack?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜