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

  • SEARCH
  • Home
  • 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 8393535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:48:58+00:00 2026-06-09T19:48:58+00:00

I have learnt that memory for global variables are allocated at program startup whereas

  • 0

I have learnt that memory for global variables are allocated at program startup whereas memory for local variables are allocated whenever function call is made.

Case 1:
I have declared a global integer array of size 63500000 and memory used is 256 MB
Ideone Link

include <stdio.h>
int a[63500000];
int main()
{
    printf ("This code requires about 250 MB memory\n");
    return 0;
}

Case 2:
I have declared a local integer array of same size in main() and memory used is 1.6 MB
Ideone link

#include <stdio.h>
int main()
{
    int a[63500000]= {1,5,0};
    printf ("This code requires only 1.6 MB \n");
    //printf ("%d\n", a[0]);
    return 0;
}

Case 3:
I have declared a local integer array of same size in another function and memory used is 1.6 MB
Ideone Link

#include <stdio.h>
void f()
{
    int a[63500000];
}

int main()
{
    f();
    return 0;
}

Please explain why there is difference in memory used or my concept of memory allocation is wrong ??

  • 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-09T19:48:59+00:00Added an answer on June 9, 2026 at 7:48 pm

    First of all: the ideone compiler is GCC.

    So, what does GCC do when you compile this?:

    void foo ()
    {
      int a[63500000];
    }
    

    gcc -S -O2 foo.c generates:

    foo:
        pushl   %ebp
        movl    %esp, %ebp
        popl    %ebp
        ret
    

    i.e. nothing is allocated on the stack, at all.

    The array is simply optimized away by GCC because it is never used.

    GCC won’t do this with a global, because it is possible that a global is used in another compilation unit, and so it isn’t sure that it is never used. Also: The global is not on the stack (since it is a global).

    Now, lets see what happens when you actually use the local array:

    int bar (int a, int b, int c)
    {
      int f[63500000];
      f[a] = 9;
      f[b] = 7;
      return f[c];
    }
    

    Things are very different:

    bar:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $254000000, %esp
        movl    8(%ebp), %eax
        movl    $9, -254000000(%ebp,%eax,4)
        movl    12(%ebp), %eax
        movl    $7, -254000000(%ebp,%eax,4)
        movl    16(%ebp), %eax
        movl    -254000000(%ebp,%eax,4), %eax
        leave
        ret
    

    This line: subl $254000000, %esp corresponds to the size of the array. i.e. memory is allocated on the stack.

    Now, what if I tried to use the bar function in a program:

    int bar (int a, int b, int c)
    {
      int f[63500000];
      f[a] = 9;
      f[b] = 7;
      return f[c];
    }
    
    int main (void)
    {
      return bar (0, 0, 0);
    }
    

    We already saw, that the bar function allocates 250 or so megabytes on the stack. On my default GNU/Linux install, the stack size is limited to 8MB. So when the program runs, it causes a “Segmentation fault”. I can increase it if I want, by executing the following in a shell:

    ulimit -s 1000000 #i.e. allow stack size to grow close to 1GB
    

    Then I can run the program, and it will indeed run.

    The reason why it fails on the ideone website is that they have limited the stack size when executing programs (and they should, otherwise malicious users could mess up their system).

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

Sidebar

Related Questions

I have a struct that only contains pointers to memory that I've allocated. Is
Being a Java developer in an agile development cycle, I have learnt that it
I have learnt from this post that always use <a> tags or <button> tags
Below I have written a sample program that I have written to learn about
After having learnt the hard way that shared variables are currently not guarded by
As I have so far learnt, a pointer is a place in memory (heap)
I have a simple program which uses select and stuff like that for multiplexing
What I'm trying to do is have a single function for each UIButton that
I have this memory game app that I'm working on, trying to learn WP7.
I have a book, Essential ActionScript 3 (O'Reilly), to learn about using that language.

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.