The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 06-07-2005
Registered User
 

Join Date: Jun 2005
Location: Chennai
Posts: 3
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
Reply With Quote
Forum Sponsor
  #2  
Old 06-07-2005
yogesh_powar's Avatar
Registered User
 

Join Date: Jun 2005
Location: satara
Posts: 37
compile time

compilation has nothing to do with the allocation of the memory.It just convert the c code to machine code .(nothing else).
Reply With Quote
  #3  
Old 06-07-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,269
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.
Reply With Quote
  #4  
Old 06-07-2005
Registered User
 

Join Date: Jun 2005
Location: Chennai
Posts: 3
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
Reply With Quote
  #5  
Old 06-08-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,269
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.
Reply With Quote
  #6  
Old 06-10-2005
yogesh_powar's Avatar
Registered User
 

Join Date: Jun 2005
Location: satara
Posts: 37
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)
Reply With Quote
  #7  
Old 06-13-2005
Registered User
 

Join Date: Oct 2004
Posts: 235
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0