473,769 Members | 6,286 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

where do the automatic variables go ?

In the following code:

int i = 5; ---it goes to .data segment
int j; ---it goes to bss segment
int main()
{
int c;
int i = 5; ---stack
int j[5] = new int[5]; ----heap
c = i*2; ----goes to .text segment

}
My question is : When the object file is created there are text, data
and bss segments etc...but there is notthing like stack and heap
segment, what happens to these automatic variables ?

I hope I am making sense.....
Siddharth
Aug 9 '08
25 2668
sidd wrote:
Richard Heathfield <r...@see.sig.i nvalidwrote:
>Keith Thompson said:
>>sidd <siddharthku... @gmail.comwrite s:

[...]

Thanks for the wonderful links and explanations, but I guess my
question still remains unanswered. My question was that when the code
is compiled and converted to a.out, what happens to the automatic
variables, in other words can someone elaborate more about the
generation of stack frames. I understand that whenever a function is
called, a stack frame gets generated but how are the details of that
routine laid out in the a.out file. In other words where do the
automatic variables part of the routine lie in the a.out format.

This really is the wrong place to ask. ("Antoninus Twink" knows
this perfectly well; he's a troll.)

Your question isn't about the C language, which is what this
newsgroup discusses. Your question is about the behavior of
programs under Linux. If you post to a Linux newsgroup, you'll
find lots of experts who can answer your question better than
anyone here can, and others who will gleefully correct any
errors the first round of experts might make.

In *this* group, however, such errors will not be corrected,
partly because many of us don't even read Mr Twink's articles any
more, and partly because many of us respect the topicality
conventions of the group so we're not going to start talking about
executable image formats just because someone asks.

To the OP: Given that "Antoninus Twink" has a track record of
being badly wrong on technical issues, it would be in your own
best interest to get a response from a system expert in a group
full of experts in that same system. The trouble with Twink is
that, unless you already know the subject yourself very well
indeed, it isn't always easy to tell whether he's right or wrong.
And since you had to ask the question, you don't know the subject
very well indeed, right? So you have two choices - believe a
response given by someone known to be technically unreliable, or
ask the question in a group where the subject of the question is
topical, so that the experts can apply the group correction
mechanism appropriately.

Thanks all for your suggestions and answers, but I guess I am still
not getting the explanation I am looking for so I will go ahead
and post this on one of the linux forums
If you just bothered to read the responses you did get, and quoted
above, you will find out why this is the wrong place to ask. And
you want more than just a linux group, you want one that deals with
your actual machine. Linux runs on many widely differing
computers.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Aug 10 '08 #21
In article <H%************ *****@newsfe01. iad>,
Chris M. Thomasson <no@spam.invali dwrote:
....
>100% stack-based malloc is possible, even in the presence of
Um, er, you just used the "S-word" in public. Please go and wash your
mouth out with soap.

I expect Mr. Keith to come along any moment now (almost certainly within
the hour) to state that C has no stack. So, therefore, you can't
possibly have written something in C that uses a stack.

It must have been in some other, irrelevant, "C-like" language.
Maybe AWK? Maybe Perl?

Aug 10 '08 #22
"Chris M. Thomasson" wrote:
"Antoninus Twink" <no****@nospam. invalidwrote in message
>sidd wrote:
>>int main()
{
int c;
int i = 5; ---stack
int j[5] = new int[5]; ----heap
c = i*2; ----goes to .text segment
}

This is a misleading dichotomy. The new line will almost certainly
generate instructions too, which will be placed in the .text
segment: new needs to call malloc() to get its memory, which will
indeed be taken from the heap.

[...]

Umm. NO, because I have created a 100% conforming malloc impl for
an embedded system with no heap:

http://groups.google.com/group/comp....7314c3f55495ac
Twink is a pure troll. Please don't encourage him/her/it.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

Aug 10 '08 #23
On 10 Aug 2008 at 12:15, Chris M. Thomasson wrote:
You act as if the stack will always grow downwards; why?
>Meanwhile, the heap starts growing upwards from above the data segment.

You seem to suggest that platforms are forced to do such a thing; why?
I suggest no such thing. I simply point out that that is what happens on
Linux/x86, which is what the OP asked about.

Aug 10 '08 #24
sidd wrote:
Thanks for the wonderful links and explanations, but I guess my
question still remains unanswered. My question was that when the code
is compiled and converted to a.out, what happens to the automatic
variables, in other words can someone elaborate more about the
generation of stack frames. I understand that whenever a function is
called, a stack frame gets generated but how are the details of that
routine laid out in the a.out file. In other words where do the
automatic variables part of the routine lie in the a.out format.
<OT>
Automatic variables do not exist in the executable file, in the sense
that you seem to be meaning. They are created on the stack at runtime
as needed, assuming the compiler doesn't put them into registers or
completely optimize them away. Globals and statics can be put in a
fixed part of the executable, since exactly one copy of them will exist
for the entire duration of the program, but that is not true of
automatic variables. Think about recursion in particular; each instance
of the function needs its own copy of any automatic variables.

How a stack frame is created will vary depending on the particular
processor and perhaps OS that you are using. The best thing you can do
is direct your compiler to produce unoptimized assembly code instead of
an executable (e.g. gcc -S -O0) and try to understand the output. On
every system I'm aware of, each function will have a standard "prologue"
at the beginning and "epilogue" at the end. These are what set up and
destroy the stack frame, among other things. If the compiler needs
space on the stack for automatic variables, you will see a small
variation in those two sections to account for the extra space needed.
</OT>

S
Aug 10 '08 #25
Antoninus Twink wrote, On 10/08/08 14:29:
On 10 Aug 2008 at 12:15, Chris M. Thomasson wrote:
>You act as if the stack will always grow downwards; why?
>>Meanwhile, the heap starts growing upwards from above the data segment.
You seem to suggest that platforms are forced to do such a thing; why?

I suggest no such thing. I simply point out that that is what happens on
Linux/x86, which is what the OP asked about.
The OP asked about Linux, not necessarily Linux on an x86, and I have a
desktop Linux box where the stack grows up.
--
Flash Gordon
Aug 10 '08 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
5048
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
7
37399
by: S. A. Hussain | last post by:
Where Global variables created in STACK or HEAP in C/C++? ve##tolimitsyahoocom, delete ##
13
2124
by: Thomas Zhu | last post by:
Hello, I know the difference between the two definations. But I do not know where are they in the memory. would someone tell me ? char s={"good", "morning"}; // at stack? char *t = {"good", "morning"}; // at heap? thanks in advance.
6
1718
by: main() | last post by:
I'm a newbie. These questions arose out of my curiosity, Please pardon me if this questions sound silly. 1. Why are the automatic variables are left uninitialized by default( as i understand only global and static variables are initialized to zero) ? What prevents a complier in doing so? If there are performance issues , why are global and static variables initialized to zero ? 2. In the following code:
58
4684
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is the overhead of automatic variable sized arrays, I suspect it is smaller than that of malloc), although I'm not too worried about it. I was thinking that, with C99's variable length arrays, malloc shouldn't be needed most of the time. But I'm...
7
1921
by: william | last post by:
My question is: Specific memory block where my pointer pointing to changed strangely, seemingly that no statement changed it. Here are two examples I got: ***********1***************** I was about to read from a floppy image and build a tree for all the directories and files. My question is only about a small portion where I had debugging problem, and I marked the place below at two places using "<======================"(you can try to...
4
1705
by: acw | last post by:
I am trying to understand how IE handles automatic tag variables. I know that IE will create a global variable each time it finds a tag with its name or id attribute set. If you have more than one tag with the same name or id (yes this is bad) on a page then that global variable goes from being a tag reference to being a collection of references. In most cases if you remove the duplicate tag the global var becomes a tag reference...
0
9587
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.