[C++] Declaring an array within a loop

  • C/C++
  • Thread starter torquerotates
  • Start date
  • Tags
    Array Loop
In summary: This codeblock is in C++int a[3]={0};for(int j=0; j<3; j++)a[i]=j;for(int i=0; i<=2; i++)cout<<a[i];endl;In summary, the code creates an array of three integers, and prints each one out.
  • #1
torquerotates
207
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
  • #2


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.
 
  • #3


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:

Related to [C++] Declaring an array within a loop

1. What is the purpose of declaring an array within a loop in C++?

The purpose of declaring an array within a loop in C++ is to create a new array with each iteration of the loop. This allows for dynamically sized arrays and can be useful for tasks such as sorting or filtering data.

2. How is an array declared within a loop in C++?

To declare an array within a loop in C++, you can use the for loop syntax and include the array declaration within the loop body. For example, for (int i = 0; i < 5; i++) { int array[i]; } would declare an array with 5 elements with each iteration of the loop.

3. Can an array be declared within any type of loop in C++?

Yes, an array can be declared within any type of loop in C++, including for loops, while loops, and do-while loops. However, it is important to note that the array will only exist within the scope of the loop and will be re-declared with each iteration.

4. What are the potential drawbacks of declaring an array within a loop in C++?

One potential drawback of declaring an array within a loop in C++ is that it can increase the runtime of the program, as the array is being re-declared with each iteration. This can also lead to memory management issues if the array is not properly initialized or if the loop runs for a large number of iterations.

5. Is it necessary to declare an array within a loop in C++?

No, it is not necessary to declare an array within a loop in C++. Arrays can also be declared outside of loops, with a fixed size, or using pointers. Declaring an array within a loop is just one way to dynamically create and manipulate arrays in C++.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
5
Views
931
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
996
Back
Top