Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The memory does not leak. It is not allocated on the heap. Now, there is likely 2 different memory locations involved here. </p> <ol> <li>The location where the <code>{ 4,3,1,5 }</code> initializer is stored. Typically initializers like this are stored in the data segment. In an embedded system this could be read-only flash. On a PC or whatever though, this is going to just be a RAM area that is initialized with your program image (eg. EXE) and isn't modified. After the function exits, it is still there. But it is not a leak -- just a part of your program's memory footprint (some might say static memory).</li> <li>The stack memory allocated for <code>a[]</code> in <code>PrintArray()</code> function scope. What the compiler does is allocate some stack space and then copies the values at the beginning of the function from the data segment. This is generally what takes place, so that if you modify any of the elements in <code>a[]</code>, it only effects that array for that particular function call. When <code>PrintArray()</code> gets called again, the original initializer is unmodified and available for reuse. It would be weird/unexpected if subsequent calls to <code>PrintArray()</code> initialized to something besides <code>{ 4,3,1,5 }</code> due to a previous invocation mutating the value. However, you aren't modifying it here, so it's plausible that optimizations may not allocated anything on the stack. YMMV. Assuming <code>a[]</code> does reside on the stack, it will be deallocated automatically (hence <code>auto</code> variable) upon function exit.</li> </ol> <p>Of course, <em>exactly</em> what is taking place depends upon your compiler, linker, settings (especially optimizations), and target.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload