LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-11-2005, 10:10 PM   #1
moadon
LQ Newbie
 
Registered: Apr 2005
Posts: 2

Rep: Reputation: 0
When is the user stack allocated?


Hi,

Can anyone tell me during which part of compiling, linking and executing a "C" program is the stack allocated? I am thinking that it is allocated when linux loads the executable image into memory but I am unsure. Also, where does the stack get allocated to? Is there one stack at the end of user memory that every process shares or does each process get its own stack allocated in its own address space -- say at the end of the data segment?

Any help would be greatly appreciated!!
 
Old 04-11-2005, 11:28 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
All of each processes memory is private to that process (baring use of mmap() or sys V shared memory, and excepting read-only and COWd pages which will appear to be private anyway...)

The rest of this is IIRC. The data segment is toward the bottom (low addresses) of your memory and the heap is above that, growing upwards. The stack is towards the top and grows downwards. It grows implicitly; that is, the process attempts to write to the page just below the limit of the current stack, causing a page fault, and the kernel allocates a page into that place before returning control to the process, just before the store which caused the fault. (This means the store sort of happens twice.)

You can get some more empirical information by printing addresses of variables you've put on the stack. e.g.

Code:
int foo;
int bar;
printf("address of foo is %p, address of bar is %p\n", &foo, &bar);
If the function is called recursively or you have several functions like this which call eachother, you should see the addresses grow downward.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference b/t Kernel stack and User stack hazzyb Linux - Software 2 09-29-2008 07:40 PM
Increasing hard-disk space allocated to user guam Linux - General 10 03-01-2005 07:31 AM
About allocated memory in C++ Ephracis Programming 2 12-28-2004 02:56 AM
allocated space of a directory Dave31836 Programming 8 10-04-2004 06:07 PM
UT2004 - Allocated Only 32M AGP! Phorem Linux - Games 0 04-16-2004 03:16 PM


All times are GMT -5. The time now is 10:09 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration