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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:38:30+00:00 2026-05-27T19:38:30+00:00

Can anyone clearly explain, in terms of C,C++ and Java. What all goes on

  • 0

Can anyone clearly explain, in terms of C,C++ and Java.
What all goes on stack and what all goes on Heap and when is the allocation done.

As far as I know,

All local variables whether primitives,pointers or reference variables per function call are on a new stack frame.

and anything created with new or malloc goes on heap.

I am confused about few things.

Are references/primitives which are members of an object created on heap also stored on heap ?

and what about those local members of a method that are being recursively created in each frame.
Are they all on stack, If yes then is that stack memory allocated at runtime ?
also for literals, are they part of the code segment ?
and what about globals in C, static in C++/Java and static in C .

  • 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-27T19:38:30+00:00Added an answer on May 27, 2026 at 7:38 pm

    Structure of a Program in Memory

    The following is the basic structure of any program when loaded in the memory.

     +--------------------------+
     |                          |
     |      command line        |
     |        arguments         |
     |    (argc and argv[])     |
     |                          |
     +--------------------------+
     | Stack                    |
     | (grows-downwards)        |
     |                          |
     |                          |
     |                          |
     |         F R E E          |
     |        S P A C E         |
     |                          |
     |                          |
     |                          |
     |                          |
     |     (grows upwards) Heap |
     +--------------------------+
     |                          |
     |    Initialized data      |
     |         segment          |
     |                          |
     +--------------------------+
     |                          |
     |     Initialized to       |
     |        Zero (BSS)        |
     |                          |
     +--------------------------+
     |                          |
     |      Program Code        |
     |                          |
     +--------------------------+
    

    Few points to note:

    • Data Segment
      • Initialized data segment (initialized to explicit initializers by programmers)
      • Uninitialized data segment (initialized to zero data segment – BSS [Block Start with Symbol])
    • Code Segment
    • Stack and Heap areas

    Data Segment

    The data segment contains the global and static data that are explicitly initialized by the users containing the intialized values.

    The other part of data segment is called BSS (because of the old IBM systems had that segment initialized to zero). It is the part of memory where the OS initializes the memory block to zeros. That is how the uninitialized global data and static get default value as zero. This area is fixed and has static size.

    The data area is separated into two areas based on explicit initialization because the variables that are to be initialized can be initialized one-by-one. However, the variables that are not initialized need not be explicitly initialized with 0’s one-by-one. Instead of that, the job of initializing the variable is left to the OS. This bulk initialization can greatly reduce the time required to load the executable file.

    Mostly the layout of the data segment is in the control of the underlying OS, still some loaders give partial control to the users. This information may be useful in applications such as embedded systems.

    This area can be addressed and accessed using pointers from the code. Auto variables have overhead in initializing the variables each time they are required and code is required to do that initialization. However, the variables in the data area does not have such runtime overload because the initialization is done only once and that too at loading time.

    Code segment

    The program code is the code area where the executable code is available for execution. This area is also of fixed size. This can be accessed only be function pointers and not by other data pointers. Another important information to note here is that the system may consider this area as read only memory area and any attempt to write in this area leads to undefined behavior.

    Constant strings may be placed either in code or data area and that depends on the implementation.

    The attempt to write to code area leads to undefined behavior. For example (I’m going to give only C based examples) the following code may result in runtime error or even crash the system.

    int main()
    {
        static int i;
        strcpy((char *)main,"something");
        printf("%s",main);
        if(i++==0)
        main();
    }
    

    Stack and heap areas

    For execution, the program uses two major parts, the stack and heap. Stack frames are created in stack for functions and heap for dynamic memory allocation. The stack and heap are uninitialized areas. Therefore, whatever happens to be there in the memory becomes the initial (garbage) value for the objects created in that space.

    Lets look at a sample program to show which variables get stored where,

    int initToZero1;
    static float initToZero2;
    FILE * initToZero3; 
    // all are stored in initialized to zero segment(BSS)
    
    double intitialized1 = 20.0;
    // stored in initialized data segment
    
    int main()
    {
        size_t (*fp)(const char *) = strlen;
        // fp is an auto variable that is allocated in stack
        // but it points to code area where code of strlen() is stored
    
        char *dynamic = (char *)malloc(100);
        // dynamic memory allocation, done in heap
    
        int stringLength;
        // this is an auto variable that is allocated in stack
    
        static int initToZero4; 
        // stored in BSS
    
        static int initialized2 = 10; 
        // stored in initialized data segment   
    
        strcpy(dynamic,”something”);    
        // function call, uses stack
    
        stringLength = fp(dynamic); 
        // again a function call 
    }
    

    Or consider a still more complex example,

    // command line arguments may be stored in a separate area  
    int main(int numOfArgs, char *arguments[])
    { 
        static int i;   
        // stored in BSS 
    
        int (*fp)(int,char **) = main;  
        // points to code segment 
    
        static char *str[] = {"thisFileName","arg1", "arg2",0};
        // stored in initialized data segment
    
        while(*arguments)
            printf("\n %s",*arguments++);
    
        if(!i++)
            fp(3,str);
    }
    

    Hope this helps!

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

Sidebar

Related Questions

I've clearly done something quite silly here. Can anyone guide me? Here's how I
I'm clearly a newb, and I was wondering if anyone knows how I can
Can anyone explain the following immediate window behavior: Debug.Print mDb.DatabaseOptions Method arguments must be
can anyone please explain how this works (asz + 7) & ~7; It rounds
Can anyone help explain to me why the following is returning duplicate data? <?php
Can anyone tell me why this statement validates to YES when the textbox clearly
Can anyone explain the difference between the types mentioned above and some sample usage
Can anyone explain in plain English the difference between the two approaches? A link
Can anyone please explain what's the difference between the two kinds of getView() implementation?
Hopefully I can explain my issue clearly enough for others to understand, here we

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.