C Array Instantiation - Stack or Heap Allocation?

前端 未结 2 1159
抹茶落季
抹茶落季 2020-12-08 15:33

I guarantee that this question has been asked before, but I haven\'t been able to find it via search; sorry in advance for any redundancies.

It\'s my (potentially wr

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 16:00

    Similarly, is the 100% sole situation where you have to use free() when the data was allocated via malloc?

    Yes. (Apart from calloc and realloc, their return value is also to be free()'d. Similarly, there are functions that use malloc() and this fact is documented, for example strdup() - the return value of these functions is also to be freed using free(), obviously.)

    char anotherCharArray[someVariable + 50];
    

    Would this go on the stack as well?

    Yes, it does (in most implementations - of course, it's not always true that you assume, but on most of the platforms, it is). And yes this is valid code, but it is only standard in C99.

提交回复
热议问题