LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-22-2004, 04:30 PM   #1
beginner16
Member
 
Registered: Nov 2003
Posts: 33

Rep: Reputation: 15
Question about Stack


hi

Are in C language automatic variables on the stack actually stored in that area of memory,or does memory that belongs to stack only contain pointers to adresses(outside of stack area) where these automatic variables actually reside?

What if automatic variable is an array (char x[4]="hiya") ?
Is on stack stored only a pointer,but actual value ("hiya") is stored in some other place outside of stack?

Is memory to where pointers point allocated at run-time and resides somewhere on the Heap?
If so,how can pointer declared as automatic variable exist on the stack?

thank you
 
Old 04-22-2004, 05:26 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Quote:
Are in C language automatic variables on the stack actually stored in that area of memory,or does memory that belongs to stack only contain pointers to adresses(outside of stack area) where these automatic variables actually reside?
Automatic variables are really stored on the stack. Once the stack frame is popped, they may be randomly overwritten. Therefore the following is a very bad idea:

Code:
int* foo() {
        int i = 5;
        return &i; /* don't do this! */
}
foo() returns a pointer to somewhere in the stack. :P

Quote:
What if automatic variable is an array (char x[4]="hiya") ?
Is on stack stored only a pointer,but actual value ("hiya") is stored in some other place outside of stack?
In this case "hiya" is stored in what I think is called the data segment (someone correct me here) when your program loads. Then, when it gets to your declaration (which btw should be char foo[5] = "hiya" or char foo[] = "hiya" because of the '\0' at the end), it puts 5 bytes on the stack and copies "hiya" into them. There isn't actually a separate pointer made in this case. This is why you can't reassign foo to point somewhere alse like you could with a char*.

Quote:
Is memory to where pointers point allocated at run-time and resides somewhere on the Heap?
When you declare a pointer, no heap memory is allocated. The pointer is uninitialized and may point somewhere random. You need to allocate memory for it yourself, usually with malloc() and friends. Alternatively, you can set it to the address of some variable that you already have, like "int *p; p = &foo;" The pointer itself exists on the stack. It simply holds an address. You can actually print out this address with the %p escape to printf:

Code:
#include <stdio.h>

int main()
{
        int *foo;
        int bar;
        bar = 5;
        foo = &bar;
        printf("foo contains %p\n", foo);
        return 0;
}
The important idea here is that when you declare a pointer, in no way shape or form does that imply there is memory somewhere that holds a thing to which the pointer is supposed to point.
 
Old 04-22-2004, 11:33 PM   #3
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
aluser answer is correct, but Id to add that if you want to see the "proof"
compile your programs with gcc -S foo.c and view the foo.s file that is created
from your C program. -S outputs the assembly code created from your .C file.
It will show you exactly what happens to your variables.
 
Old 04-24-2004, 11:41 AM   #4
beginner16
Member
 
Registered: Nov 2003
Posts: 33

Original Poster
Rep: Reputation: 15
thank you for your help
 
  


Reply



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
stack problem C eagle683 Programming 3 05-17-2005 04:52 PM
stack gapping?? true_atlantis Fedora 0 10-28-2004 01:47 PM
stack in chapter 6 alaios Linux From Scratch 1 03-29-2004 08:11 AM
Stack trace ust Linux - General 0 02-27-2004 02:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:48 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration