Memory allocation and char arrays

I still do not quite understand what exactly will happen in the situation:

int i = 0;
for(i; i <100; i ++)
{
    char some_array[24];
     //...
    strcpy(some_array,"abcdefg");

}

Will some_arrayact like:

some_array = malloc(24);

At the beginning of the cycle and free(some_array)at the end of the cycle?

Or will these variables be allocated on the stack even after the function is destroyed?

+4
source share
3 answers

some_array is local to the block, so it is created at the beginning of each iteration of the loop and destroyed again at the end of each iteration of the loop.

In the case of a simple array, creating and destroying does not mean much. If (in C ++) you replace it (for example) with an object that prints something when it is created and destroyed, you will see that these side effects occur.

+7

. 1. malloc, char   , . 2.

+1

malloc() .

, "free (some_array)" . .

- - . , C , .

It depends on the compiler as to whether the array is created on the stack or how it optimizes the re-creation and destruction of the array. In practice, it is often created on the stack.

+1
source

All Articles