Static variables memory allocation


 
Thread Tools Search this Thread
Top Forums Programming Static variables memory allocation
# 1  
Old 06-07-2005
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
# 2  
Old 06-07-2005
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  
Old 06-07-2005
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

Then edit myfile.s and see what you get. Only do this with a small program to start with until you learn to read the output.
# 4  
Old 06-08-2005
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  
Old 06-08-2005
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  
Old 06-10-2005
static variable

Quote:
Originally Posted by nathanmca
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
:- Static variable defines the scope of the variable.
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  
Old 06-14-2005
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

memory allocation for string in C

hi in the following code, how the memory is allocated for a1 which holds the values of a2 after cpy function call. #include <stdio.h> #include <string.h> void cpy(char* d, const char* s){ while(*d++=*s++); } main(){ char* a1; char* a2="done"; cpy(a1,a2); ... (3 Replies)
Discussion started by: mprakasheee
3 Replies

2. Shell Programming and Scripting

memory allocation to a variable

hello all.. i'm a beginner in shell scripting. I need to know what is really happening when we are creating a variable in shell scripting? how memory is allocated for that variable? (3 Replies)
Discussion started by: aarathy
3 Replies

3. Programming

Memory allocation in C

Hi Experts I need some help in static memory allocation in C. I have a program in which I declared 2 variables, one char array and one integer. I was little surprised to see the addresses of the variables. First: int x; char a; printf("%u %u\n', &x, a); I got the addresses displayed... (2 Replies)
Discussion started by: unx_freak
2 Replies

4. Programming

dynamic allocation vs static allocation in c

i wrote a tiny version of tail command using a large buffer statically allocated but, in a second time, i found another version in which i use a bidimensional array dynamically allocated. here is the first version /*my tiny tail, it prints the last 5 line of a file */ #include<stdio.h>... (4 Replies)
Discussion started by: lucasclaus
4 Replies

5. Programming

memory allocation in subroutine

Hi everyone, I'm not new to C programming, but I'm having question regarding the memory allocation of a pointer variable which, for instance, will be declared in main(), but its memory will be allocated in subroutine. To clearify my question, I provide a small working example: #include... (1 Reply)
Discussion started by: MIB_Maik
1 Replies

6. Programming

Is there a problem with the memory allocation???

I have a scenario like the client has to search for the active server.There will be many servers.But not all server are active.And at a time not more than one server will be active. The client will be in active state always i.e, it should always search for an active server until it gets one.I... (1 Reply)
Discussion started by: vigneshinbox
1 Replies

7. Programming

Memory allocation problem

I have a program that will fetch some particular lines and store it in a buffer for further operations.The code which is given below works but with some errors.I couldn't trace out the error.Can anybody help on this plz?? #include <stdio.h> #include <stdlib.h> #include<string.h> #define... (1 Reply)
Discussion started by: vigneshinbox
1 Replies

8. Programming

Dynamic memory allocation

Hi, I am trying to process line by line of a file. But I should not be allocating static allocation for reading the contents of the file. The memory should be dynamically allocated. The confusion here is how do I determine the size of each line, put it into a buffer with the memory allocated... (11 Replies)
Discussion started by: naan
11 Replies

9. HP-UX

HP-UX memory usage allocation

Hi all, I have a HP-UX Server with 4 gigabytes of physical RAM. When I use the 'Glance' utility to see what my memory utilization is, my memory usage shows up maxed out at 99%. I shut off all the known processes that I'm running on that box and the memory utilization is still at 78% (with Swap... (3 Replies)
Discussion started by: dehuang83
3 Replies

10. UNIX for Dummies Questions & Answers

memory allocation

I would like to know how I could allocate some more memory to a process. Please note that I am not the root user. (1 Reply)
Discussion started by: sagar
1 Replies
Login or Register to Ask a Question