Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C VC9.0 os memory
Hi All,
I found that static memory can be allocated at compile time, as the size the object/variable going to take is predefined.
 
My question is "how it[compiler] knows which memory location will be assign to the application by OS,[while launching the apps.] so that all the static [int x] variable could allocate memory"?
 
e.g.
executable name: run.exe
==========================
code
========
void main()
{
int x;//Say 'x' assigned to->0xFFAB12 memory location while compiling
}
now restart the machine
 
and run run.exe without compiling again
 
how it knows 0xFFAB12 address again will fall under the memory area alloted by OS to run.exe?
 

if I have some conceptual problem, please let me know.
 
thanks in advance
Posted 22-May-12 20:38pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

For this answer i have compiled the information from different sources but mainly from Q&A of Stackoverflow.com.
 
A process has 5 different areas of memory allocated
 
1. Code - text segment
2. Initialized data – .data segment
3. Uninitialized data – .bss segment
4. Heap
5. Stack
 
+--------------------------+
| |
| 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 |
| |
+--------------------------+
 

In your case the answer is -
void main()
{
int x;\\on the stack, destroyed when main() returns
static int i;  \\stored in BSS
}
 
More general sample (see What is stored on heap and what is stored on stack?[^]) -
char * str = "Text line 1";  /* 1 */
char * buf0 ;                         /* 2 */
 
int main(){
    char * str2 = "Text line 2" ;  /* 3 */
    static char * str3 = str;         /* 4 */
    char * buf1 ;                     /* 5 */
    buf0 = malloc(BUFSIZ);            /* 6 */
    buf1 = malloc(BUFSIZ);            
 
    return 0;
} 
 
1.This is neither allocated on the stack NOR on the heap. Instead it's allocated as static data, and put into it's own memory segment on most modern machines. The actual string is also being allocated as static data, and put into a read-only segment in right-thinking machines.
 
2. Is simply a static allocated pointer; room for one address, in static data.
 
3. Has the pointer allocated on the stack, and will be effectively deallocated when main returns. The string, since it's a constant, is allocated in static data space along with the other strings.
 
4. Is actually allocated exactly like at 2. The static keyword tells you that it's not to be allocated on the stack.
 
5. But buf1 is on the stack, and
 
6. The malloc'ed buffer space is on the heap.
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
v2
Comments
shankha2010 at 23-May-12 8:32am
   
http://www.arjay.bc.ca/Modula-2/Text/Ch12/Ch12.4.html
thanks for this link :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 410
1 Abhinav S 260
2 Nirav Prabtani 252
3 Andreas Gieriet 155
4 Dave Kreskowiak 155
0 OriginalGriff 7,545
1 Sergey Alexandrovich Kryukov 6,757
2 Maciej Los 3,909
3 Peter Leow 3,693
4 CHill60 2,712


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 23 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100