char x[256] vs. char* = malloc(256*sizeof(char));

后端 未结 6 1593
日久生厌
日久生厌 2020-12-29 09:53

Someone here recently pointed out to me in a piece of code of mine I am using

char* name = malloc(256*sizeof(char));
// more code
free(name);
6条回答
  •  盖世英雄少女心
    2020-12-29 10:19

    This is incorrect - the array declaration does not require a free. Further, if this is within a function, it is allocated on the stack (if memory serves) and is automatically released with the function returns - don't pass a reference to it back the caller!

提交回复
热议问题