free() on stack memory

后端 未结 5 1194
余生分开走
余生分开走 2020-12-06 18:07

I\'m supporting some c code on Solaris, and I\'ve seen something weird at least I think it is:

char new_login[64];
...
strcpy(new_login, (char *)login);
...
         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 18:53

    IN MOST CASES, you can only free() something allocated on the heap. See http://www.opengroup.org/onlinepubs/009695399/functions/free.html .

    HOWEVER: One way to go about doing what you'd like to be doing is to scope temporary variables allocated on the stack. like so:

    {
    char new_login[64];
    ... /* No later-used variables should be allocated on the stack here */
    strcpy(new_login, (char *)login);
    }
    ...
    

提交回复
热议问题