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 8980571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:07:15+00:00 2026-06-15T20:07:15+00:00

If you have a variable, there is a memory address associated with that variable,

  • 0

If you have a variable, there is a memory address associated with that variable, and in the case of a pointer variable, the “value” of that memory address is a reference to the memory address that holds the actual data that the pointer points to.

so if I have:

for (int x = 0; x < 2; x++)
{
    char * a = (char*)malloc(20);
    printf("%p\r\n", &a);
    printf("%p\r\n", a);
}

the output should be something like:

    00999999
    04427310
    00999999
    0442ECF0

And as you can see, the 1st and the 3rd memory address remain the same for the pointer variables declared during each pass of the loop, and my understanding is that this is so because the previous variable went out of scope and the next available address is the same address.

Can this generalization be extended to all variables declared inside a loop or are there exceptions?

  • 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-06-15T20:07:16+00:00Added an answer on June 15, 2026 at 8:07 pm

    That’s how a stack usually works. And this is independent of variable type. When the stack grows downwards it might look more or less like this

    <top>
    |                 |
    +-----------------+
    | argument1       |
    | argument2       |
    +-----------------+
    | return address  |
    +-----------------+
    | saved register1 |
    | saved register2 |
    +-----------------+
    | local variable1 | <- base register
    | local variable2 |
    | x               |
    | a               |
    |                 | <- stack pointer
    <bottom>
    

    The compiler assigns space on the stack to each variable relative to some base register. When the scope of the loop ends, the space for a effectively becomes “free” and can be reused.

    If there’s a second loop later or some other nested scope

    for (int x = 0; x < 2; x++)
    {
        char * a = (char*)malloc(20);
        printf("%p\r\n", &a);
        printf("%p\r\n", a);
    }
    ...
    for (int x = 0; x < 2; x++)
    {
        char * b = (char*)malloc(20);
        printf("%p\r\n", &b);
        printf("%p\r\n", b);
    }
    

    b might reuse the space previously occupied by a, since it is not needed anymore for a. This all depends on how the compiler optimizes the space on the stack.

    This is how it works for compiled C like languages at least. There are other memory models as well, of course.

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

Sidebar

Related Questions

I have defined a table type PL/SQL variable and added some data there. create
I have a variable name subject. For each unique subject there are 240 response
There is a JSP page in which i have an ArrayList variable. I need
I have a class with an integer variable called layer, there is a list
I have a array with a variable amount of values. Is there a more
Is there anything similar to global variable in SSIS? I have 4 variables (FromAddress,
I have a div of fixed length. there is anorher div of variable length.
I have a SourceViewController and a DesViewController , there's a variable in DesViewController :
If you have a template class with a static variable, is there any way
I have a memory address, its the memory address of a function in another

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.