![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| HP-UX memory usage allocation | dehuang83 | HP-UX | 3 | 06-02-2007 12:40 AM |
| HP-UX memory usage allocation | dehuang83 | UNIX for Dummies Questions & Answers | 1 | 05-02-2007 10:40 PM |
| tar: Memory allocation failed | gfhgfnhhn | UNIX for Dummies Questions & Answers | 1 | 03-05-2007 08:23 AM |
| threads and memory allocation | prankster | UNIX for Advanced & Expert Users | 3 | 12-14-2005 09:45 AM |
| memory allocation | sagar | UNIX for Dummies Questions & Answers | 1 | 01-05-2002 08:53 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Static variables memory allocation
Hi
I want to know when and where memory for static variables are allocated in a C program. If it allocates during compilation will memory be allocated for the variable "i" during compilation itself. int count(); int main(){ printf("%d", count()); return 0; } int count() { static int i; return ++i; } Thanks in advance Nathan |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
compile time
compilation has nothing to do with the allocation of the memory.It just convert the c code to machine code .(nothing else).
|
|
#3
|
|||
|
|||
|
The image file has "space" set aside for the variable - depending on your system architecture, the area is called a data segment or $TEXT or whatever.
If you want to see what is going on compile to assembly Code:
cc -S myfile.c |
|
#4
|
|||
|
|||
|
Hi Thanks for ur reply
My doubt is when the control goes to the function 'count' will then the memory be allocated to the static variable i. That means the memory is allocated at the runtime. But I have read in a book that the size of data segment is non modifiable during runtime. So where it stores the variable i and how it retains the value. Am I making sense? Correct me if I am wrong Regards Nathan |
|
#5
|
|||
|
|||
|
You are misunderstanding something. Or maybe I am. The data segment is DESCRIBED in the image file. The data segment is CREATED by calling the brk() function at runtime and then is built or copied into the memory allocated by brk().
Data in global static variables has memory set aside INSIDE the image (or .exe if you will) file. It actually takes up space in there. Local static variables are allocated in the data segment the FIRST time the function is called. Before then they are only described. This is because a function may never be called during execution (never comes into scope) so it local variables may not be needed, whereas global variables are considered to be ins scope for the whole program. How this is implemented is up to the hardware and the compiler, so it may vary. |
|
#6
|
||||
|
||||
|
static variable
Quote:
it has mainly two uses 1)Generally extern variable are made static so that function that are not in the file can not access it and function with in the file can access it. 2)Its scope remains till the end of the program i.e, they retains there values inbetweeen in the function call too. thus in your program if you call the subroutine second time the value of i will be 2.and not 1.(which would have been if it wasn't static) |
|
#7
|
|||
|
|||
|
Hi yogesh,
Still i am in doublt pleas emake it claer will the memory allocation is done at compile time or at run time for a static variable. ArunKumar |
|||
| Google The UNIX and Linux Forums |