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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:32:57+00:00 2026-05-12T23:32:57+00:00

I have this piece of code in C: int q = 10; int s

  • 0

I have this piece of code in C:

int q = 10;
int s = 5;
int a[3];

printf("Address of a: %d\n",    (int)a);
printf("Address of a[1]: %d\n", (int)&a[1]);
printf("Address of a[2]: %d\n", (int)&a[2]);
printf("Address of q: %d\n",    (int)&q);
printf("Address of s: %d\n",    (int)&s);

The output is:

Address of a: 2293584
Address of a[1]: 2293588
Address of a[2]: 2293592
Address of q: 2293612
Address of s: 2293608

So, I see that from a to a[2], memory addresses increases by 4 bytes each.
But from q to s, memory addresses decrease by 4 byte.

I wonder 2 things:

  1. Does stack grow up or down? (It looks like both to me in this case)
  2. What happens between a[2] and q memory addresses? Why there is a big memory difference there? (20 bytes).

Note: This is not homework question. I am curious on how stack works. Thanks for any help.

  • 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-12T23:32:57+00:00Added an answer on May 12, 2026 at 11:32 pm

    The behavior of stack (growing up or growing down) depends on the application binary interface (ABI) and how the call stack (aka activation record) is organized.

    Throughout its lifetime a program is bound to communicate with other programs like OS. ABI determines how a program can communicate with another program.

    The stack for different architectures can grow the either way, but for an architecture it will be consistent. Please check this wiki link. But, the stack’s growth is decided by the ABI of that architecture.

    For example, if you take the MIPS ABI, the call stack is defined as below.

    Let us consider that function ‘fn1’ calls ‘fn2’. Now the stack frame as seen by ‘fn2’ is as follows:

    direction of     |                                 |
      growth of      +---------------------------------+ 
       stack         | Parameters passed by fn1(caller)|
    from higher addr.|                                 |
    to lower addr.   | Direction of growth is opposite |
          |          |   to direction of stack growth  |
          |          +---------------------------------+ <-- SP on entry to fn2
          |          | Return address from fn2(callee) | 
          V          +---------------------------------+ 
                     | Callee saved registers being    | 
                     |   used in the callee function   | 
                     +---------------------------------+
                     | Local variables of fn2          |
                     |(Direction of growth of frame is |
                     | same as direction of growth of  |
                     |            stack)               |
                     +---------------------------------+ 
                     | Arguments to functions called   |
                     | by fn2                          |
                     +---------------------------------+ <- Current SP after stack 
                                                            frame is allocated
    

    Now you can see the stack grows downward. So, if the variables are allocated to the local frame of the function, the variable’s addresses actually grows downward. The compiler can decide on the order of variables for memory allocation. (In your case it can be either ‘q’ or ‘s’ that is first allocated stack memory. But, generally the compiler does stack memory allocation as per the order of the declaration of the variables).

    But in case of the arrays, the allocation has only single pointer and the memory needs to be allocated will be actually pointed by a single pointer. The memory needs to be contiguous for an array. So, though stack grows downward, for arrays the stack grows up.

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

Sidebar

Related Questions

I have this piece of code can you explain me the output unsigned int
everyone, I have this piece of the code: void foo(int var, int var1) {
I have a piece of code that looks like this: ipCount = defaultdict(int) for
i have a piece of code like this: int main (int argc, char *argv[])
i have this piece of code int main() { char a[100]; a[0]='a'; a[1]='b'; fun(a[0]);
I have this piece of code: int casted_dbValue=3; wchar_t* nativeData=(wchar_t*)casted_dbValue; it is incorrect conversion
Say we have this piece of C code: int x[] = {1, 2, 3,
I have this simple piece of code in c++: int main(void) { string text
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code that works fine in subsonic 2.2, I migrated

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.