C/C++ [C++] Declaring an array within a loop

  • Thread starter Thread starter torquerotates
  • Start date Start date
  • Tags Tags
    Array Loop
Click For Summary
The discussion focuses on a C++ code snippet that aims to output a specific pattern using a local array declared within a loop. The code initializes an integer array `a[3]` to zero in each iteration of the outer loop, then modifies its values in the inner loop to produce the desired output. Participants clarify that the array is allocated on the stack, and while it appears to be reset with each iteration, it is actually allocated once. The stack pointer is adjusted to manage memory, and when the loop ends, the stack pointer is restored, preventing memory from "vanishing." The conversation highlights the mechanics of stack allocation, including the potential for stack overflow in recursive scenarios, and provides insights into how local variables are managed in assembly language, specifically in 32-bit and 64-bit contexts.
torquerotates
Messages
207
Reaction score
0
The following code block is in C++ Everything is inside the first for loop.

for( int i=0; i<=2; i++)
{
int a[3]={0};

for(int j=0; j<3; j++)
a=j;

for( int i=0; i<=2; i++)
cout<<a;

cout<<endl;

}


basically I am trying to get the screen to output
200
020
002

I'm going this by simply erasing the a[3] array every time the first loop interates

Well it worked. Does that mean that the array a[] is reset every time the first loop iterates?
Does the memory simply vanish?
 
Technology news on Phys.org


Hey torquerotates.

Arrays like this are declared on the stack and not the heap.

What happens internally is that you have what is called a stack-space for each process (and each thread as well). When you declare an array like this, what you do is you increase the stack pointer by pushing machine words (i.e. in assembler its PUSH EBX or some other register).

When the loop terminates or the context terminates, the stack pointer is restored by using a POP EBX or simply setting the stack pointer directly (like SUB ESP,4 as an example).

Everytime you declare a variable like this (integer, data structure, etc) this is what happens.

When you get a situation where you run out of stack space, you get what is called a stack overflow and this is more prone when you have recursive routines where each function call adds more local variables on to the stack.

For the specifics look up what the stack pointer is (ESP is the pointer in 32-bit) and look up PUSH and POP mneumonics to see how stuff works.
 


Although the array a[] is reinitialized for each iteration of the loop, it's only allocated one time on the stack, and the memory does not "vanish".

In the case of Visual Studio, stack variables are allocated by subracting a constant for the size of all allocated variables from the stack pointer (ESP), and the generated code references variable as offsets from ESP, or the generated code may start off with PUSH EBP ... MOV EBP,ESP ... SUB ESP,# ... and reference variables as offsets from EBP. This is for 32 bit mode, if in 64 bit mode, then the register names would be RSP and RBP.

If the array a[] is the only stack variable (with registers used for i and j), then the assembly code could look like this:

Code:
        sub     esp, 12       ;allocate 12 bytes for int a[3]
;       ...                   ;this is only done once

;       ...                   ;initialize a[] in loop
        xor     eax,eax       ;zero eax
        mov     [esp+0],eax   ;set a[0] = 0
        mov     [esp+4],eax   ;set a[1] = 0
        mov     [esp+8],eax   ;set a[2] = 0

;       ...

        add     esp, 12       ;restore esp before return
 
Last edited:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
12
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K