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 <si************ @gmail.comwrite s:
On Aug 9, 11:34*pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>sidd said:
In the following code:
int i = 5; *---it goes to .data segment
int j; * * * *---it goes to bss segment

The C Standard doesn't guarantee this. Nor does it even require that
implementation s recognise the concept of .data or bss segments.
int main()
{
int c;
int i = 5; ---stack

The C Standard doesn't guarantee this. Nor does it even require that
implementation s recognise the concept of a (hardware) stack.
int j[5] = new int[5]; ----heap

In C, this is just a syntax error.

If you have questions about the C++ language, ask in comp.lang.c++.
If you have questions about the storage techniques used by your
implementation , ask in a group devoted to that implementation.

<snip>

Can someone please answer the question assuming that it was run on a
linux m/c and the executable was a.out.
I'm sure someone can.

Richard gave you a complete and correct answer with regard to C.
If you want a system-specific answer, you'll have to ask in a
system-specific newsgroup, probably one of the comp.os.linux.* groups

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 10 '08 #11
sidd <si************ @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.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 10 '08 #12
Keith Thompson <ks***@mib.orgw rites:
sidd <si************ @gmail.comwrite s:
>On Aug 9, 11:34Â*pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>>sidd said:
In the following code:

int i = 5; Â*---it goes to .data segment
int j; Â* Â* Â* Â*---it goes to bss segment

The C Standard doesn't guarantee this. Nor does it even require that
implementatio ns recognise the concept of .data or bss segments.

int main()
{
int c;
int i = 5; ---stack

The C Standard doesn't guarantee this. Nor does it even require that
implementatio ns recognise the concept of a (hardware) stack.

int j[5] = new int[5]; ----heap

In C, this is just a syntax error.

If you have questions about the C++ language, ask in comp.lang.c++.
If you have questions about the storage techniques used by your
implementatio n, ask in a group devoted to that implementation.

<snip>

Can someone please answer the question assuming that it was run on a
linux m/c and the executable was a.out.

I'm sure someone can.
Someone already did. Someone in this group who knew the subject.
>
Richard gave you a complete and correct answer with regard to C.
If you want a system-specific answer, you'll have to ask in a
system-specific newsgroup, probably one of the comp.os.linux.* groups
No. He didn't. Someone answered him very well here.
Aug 10 '08 #13
Keith Thompson said:
sidd <si************ @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.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 10 '08 #14
On Aug 10, 12:37*pm, 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.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999- Hide quoted text -

- Show quoted text -
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
Aug 10 '08 #15
On 10 Aug 2008 at 7:37, Richard Heathfield wrote:
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.
If there is a technical error in anything I post, then point it out. But
no, all you can do is sit at the sidelines slinging mud, isn't it?

To the OP: before deciding how seriously to take Mr "Heathfield "'s slurs
against me, you might like to know that he says even worse things about
the technical competence of Jacob Navia. Jacob is one of the group's
most helpful posters, and has also WRITTEN A C COMPILER for Windows. But
according to Heathfield, Jacob knows nothing about C. In that case I'm
proud to be tarred with the same brush as Jacob.

What makes Heathfield twist reality in this way? That's a question for a
psychologist, really, but it seems clear that he's an egomaniac who
wants to be in charge of this group, and force it to follow his own very
strict definition of topicality. Just look at this thread - the
executable output by a C compiler is apparently "off topic" in a C
newsgroup! It's just nonsense.

So, an egomaniac, and probably quite a big chunk of jealousy that his
arch-enemy Jacob has actually got off his fanny and done something
useful for the C community by writing and maintaining a free compiler
and IDE, whereas Heathfield for all his posturing and blustering has
done little more than tell a thousand newbies what the correct return
type of main() is. If you ever wonder what happens to schoolyard bullies
when they grow up, just look at Heathfield.

Aug 10 '08 #16
On 10 Aug 2008 at 7:41, sidd wrote:
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
The post by Jacob Navia that I linked to provides an excellent answer to
the question that you asked. So if you didn't get the information you
wanted out of it, then you'll need to phrase your question more
precisely. (Whether you post it here, or allow the group's bullies to
push you off somewhere else).

Aug 10 '08 #17
"Antoninus Twink" <no****@nospam. invalidwrote in message
news:sl******** ***********@nos pam.invalid...
On 9 Aug 2008 at 18:26, 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.
>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 ?

The kernel provides each process with a virtual address space, and takes
responsibility for mapping this to physical memory. When your C program
has been loaded and starts executing, the operating system
(executable-loader) has kindly set up this address space to look like
this:
highest address
=========
| |
| |
| |
| |
| |
| |
=========
| data |
| +bss |
=========
| text |
=========
address 0

As you program starts generating stack frames and automatic variables,
the stack grows downwards from high to low memory addresses.
[...]

You act as if the stack will always grow downwards; why?

http://groups.google.com/group/comp....q=stack+growth

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?

Aug 10 '08 #18
"Antoninus Twink" <no****@nospam. invalidwrote in message
news:sl******** ***********@nos pam.invalid...
On 9 Aug 2008 at 18:26, 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

Aug 10 '08 #19
"Chris M. Thomasson" <no@spam.invali dwrote in message
news:d_******** *********@newsf e01.iad...
"Antoninus Twink" <no****@nospam. invalidwrote in message
news:sl******** ***********@nos pam.invalid...
>On 9 Aug 2008 at 18:26, 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
100% stack-based malloc is possible, even in the presence of
multi-threading. If you want to discuss the algorithm I created, post over
on comp.programmin g.threads.

Aug 10 '08 #20

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
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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...
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.

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.