Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[C++] Declaring an array within a loop

  1. Jul 27, 2012 #1
    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 im 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?
     
  2. jcsd
  3. Jul 27, 2012 #2

    chiro

    User Avatar
    Science Advisor

    Re: Declaring an array within a loop

    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.
     
  4. Jul 27, 2012 #3

    rcgldr

    User Avatar
    Homework Helper

    Re: Declaring an array within a loop

    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 (Text):

            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: Jul 28, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [C++] Declaring an array within a loop
  1. C++ Declarations help (Replies: 1)

Loading...