[C++] Declaring an array within a loop

  • Context: C/C++ 
  • Thread starter Thread starter torquerotates
  • Start date Start date
  • Tags Tags
    Array Loop
Click For Summary
SUMMARY

The discussion focuses on declaring an array within a loop in C++. The code provided demonstrates that the array `a[3]` is allocated on the stack and initialized during each iteration of the outer loop. It clarifies that while the array appears to be reset each time, it is only allocated once, and the stack pointer is adjusted accordingly. The internal workings of stack allocation and deallocation are explained, emphasizing the importance of understanding stack pointers and assembly instructions like PUSH and POP.

PREREQUISITES
  • Understanding of C++ syntax and control structures
  • Knowledge of stack vs. heap memory allocation
  • Familiarity with assembly language concepts, particularly stack operations
  • Experience with Visual Studio and its memory management in 32-bit and 64-bit modes
NEXT STEPS
  • Research stack memory management in C++
  • Learn about assembly language instructions like PUSH and POP
  • Explore the differences between stack and heap allocation in programming
  • Investigate stack overflow scenarios and how to prevent them in recursive functions
USEFUL FOR

C++ developers, software engineers interested in memory management, and students learning about stack allocation and assembly language integration.

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++)
count<<a;

count<<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:

Similar threads

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