Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 632115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:02:27+00:00 2026-05-13T20:02:27+00:00

Suppose we have the following: void print() { int a; // declaration a =

  • 0

Suppose we have the following:

void print()
{
     int a;  // declaration
     a = 9;
     cout << a << endl;
}

int main ()
{
     print();
}

Is the storage for variable a allocated at the moment function print is called in main or is it when execution reaches the declaration inside the function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:02:28+00:00Added an answer on May 13, 2026 at 8:02 pm

    This is very much compiler dependent under the covers, but logically the storage is assigned as soon as the variable is declared.

    Consider this simplistic C++ example:

    // junk.c++
    int addtwo(int a)
    {
        int x = 2;
    
        return a + x;
    }
    

    When GCC compiles this, the following code is generated (; comments mine):

    .file   "junk.c++"
        .text
    .globl _Z6addtwoi
        .type   _Z6addtwoi, @function
    _Z6addtwoi:
    .LFB2:
        pushl   %ebp           ;store the old stack frame (caller's parameters and locals)
    .LCFI0:
        movl    %esp, %ebp     ;set up the base pointer for our parameters and locals
    .LCFI1:
        subl    $16, %esp      ;leave room for local variables on the stack
    .LCFI2:
        movl    $2, -4(%ebp)   ;store the 2 in "x" (-4 offset from the base pointer)
        movl    -4(%ebp), %edx ;put "x" into the DX register
        movl    8(%ebp), %eax  ;put "a" (+8 offset from base pointer) into AX register
        addl    %edx, %eax     ;add the two together, storing the results in AX
        leave                  ;tear down the stack frame, no more locals or parameters
        ret                    ;exit the function, result is returned in AX by convention
    .LFE2:
        .size   _Z6addtwoi, .-_Z6addtwoi
        .ident  "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
        .section    .note.GNU-stack,"",@progbits
    

    Everything between _Z6addtwoi and .LCFI2 is boilerplate code used to set up the stack frame (store the previous function’s variables, etc. safely out of the way). That last “subl $16, %esp” is the allocation of the local variable x.

    .LCFI2 is the first bit of actual executing code that you’ve typed. “movl $2, -4(%ebp)” is putting the value 2 into the variable. (Initialization, in other words.) Now your space is allocated AND initialized. After that it loads the value into register EDX and follows that by moving your parameter, found in “8(%ebp)”, into another register EAX. It then adds the two together, leaving the result in EAX. This is now the end of any code you’ve actually typed. The rest is again just boilerplate. Since GCC mandates that integers are returned in EAX, no work has to be done for the return value. The “leave” instruction tears down the stack frame and the “ret” instruction returns control back to the caller.

    TL;DR summary: you can think of your space as having been allocated with the very first line of executable code in your block (paired {}).


    I thought I’d clean this up a bit with explanatory comments seeing as this is the selected answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 431k
  • Answers 431k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I ran into a similar issue the other day. Here's… May 15, 2026 at 2:17 pm
  • Editorial Team
    Editorial Team added an answer You are missing a .at the end of the line… May 15, 2026 at 2:17 pm
  • Editorial Team
    Editorial Team added an answer I have been having a problem with similar behaviour but… May 15, 2026 at 2:17 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.