473,378 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Where are the program variables stored in memory(text, data or stacksegments)

kr
Hi All,
Suppose I consider a sample program as given below:-

#include<stdio.h>
#include<stdlib.h>
int i;
int main()
{
char *test(int i);
char *tmp = NULL;
i = 10;
tmp = test(i);
printf("%s\n", test(i));
free(tmp);
return 0;
}
char *test(int i)
{
char *str = (char*)malloc(20);
sprintf(str, "i value is: %d", i);
return str;
}

This program contains several variables (global, local and dynamically
allocated variables).
How to figure out which variable is stored in which segment viz. text,
data, stack or heap? Is there any tool to get an idea about it?

Thanks and Regards,
kr.
Nov 20 '07 #1
7 2094
kr schrieb:
[Snip]
This program contains several variables (global, local and dynamically
allocated variables).
I could tell you, but it looks far too much like a homework question.
How to figure out which variable is stored in which segment viz. text,
data, stack or heap? Is there any tool to get an idea about it?
Try nm.

Greetings,
Johannes

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <47**********************@news.sunrise.ch>
Nov 20 '07 #2
In article
<69**********************************@e23g2000prf. googlegroups.com>, kr
<ra*************@gmail.comwrote on Tuesday 20 Nov 2007 8:17 pm:
Hi All,
Suppose I consider a sample program as given below:-

#include<stdio.h>
#include<stdlib.h>
int i;
int main()
{
char *test(int i);
char *tmp = NULL;
i = 10;
tmp = test(i);
printf("%s\n", test(i));
free(tmp);
return 0;
}
char *test(int i)
{
char *str = (char*)malloc(20);
sprintf(str, "i value is: %d", i);
return str;
}

This program contains several variables (global, local and dynamically
allocated variables).
How to figure out which variable is stored in which segment viz. text,
data, stack or heap? Is there any tool to get an idea about it?
Standard C does not define segments or stack or heap, though they may be
ubiquitous.

Some architectures need a stack to be simulated on a heap while others
may lack the heap altogether. There are also executable formats which
define few or no segments at all, while the meaning and use of any
segments may vary subtly depending on the operating and the compiler in
question.

It is best to ask such platform specific questions in a group for your
platform or compiler. For example, if you are using Windows you might
want to try <news:comp.os.ms-windows.programmer.win32or
<news:comp.unix.programmerfor an UNIX system and so on.

Nov 20 '07 #3
kr wrote On 11/20/07 09:47,:
Hi All,
Suppose I consider a sample program as given below:-
[...]

This program contains several variables (global, local and dynamically
allocated variables).
How to figure out which variable is stored in which segment viz. text,
data, stack or heap? Is there any tool to get an idea about it?
Others have mentioned that the C language has no notion
of "segment," although some specific implementations might --
so your question isn't about C, but about Megahard C or
DrillAir C or LikeWater C or whatever implementations you
care about, and the answers may be different for each.

Another point I haven't seen mentioned yet: What about
variables that are stored in CPU registers, or variables
that sometimes reside in registers and sometimes in memory,
or variables that are optimized out of existence?

--
Er*********@sun.com
Nov 20 '07 #4

"kr" <ra*************@gmail.comwrote in message
news:69**********************************@e23g2000 prf.googlegroups.com...
Hi All,
Suppose I consider a sample program as given below:-

#include<stdio.h>
#include<stdlib.h>
int i;
int main()
{
char *test(int i);
char *tmp = NULL;
i = 10;
tmp = test(i);
printf("%s\n", test(i));
free(tmp);
return 0;
}
char *test(int i)
{
char *str = (char*)malloc(20);
sprintf(str, "i value is: %d", i);
return str;
}

This program contains several variables (global, local and dynamically
allocated variables).
How to figure out which variable is stored in which segment viz. text,
data, stack or heap? Is there any tool to get an idea about it?
local variables go on the stack, malloc() allocates memory from the heap,
and global variables go in a special area set up on program start up.

The exact details vary from system to system. For instance sometimes the
stack will grow upwards, sometimes downwards. Often constant global data is
segregated from variables.

Note also that this scheme isn't used by absolutely every C compiler, though
it will almost certainly be the way your system works.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Nov 20 '07 #5
In article <1195574401.179806@news1nwk>,
Eric Sosman <Er*********@Sun.COMwrote:
Another point I haven't seen mentioned yet: What about [...]
variables that are optimized out of existence?
They're stored on the heap. Very efficiently.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 20 '07 #6
Malcolm McLean wrote:
local variables go on the stack, malloc() allocates memory from the
heap, and global variables go in a special area set up on program start up.
IMHO its dangerous to start your post with such a definitive statement,
even though you acknowledge it to be wrong later on. Unless you were
being ironic?
Nov 21 '07 #7
Richard Tobin wrote:
In article <1195574401.179806@news1nwk>,
Eric Sosman <Er*********@Sun.COMwrote:
> Another point I haven't seen mentioned yet: What about [...]
variables that are optimized out of existence?

They're stored on the heap. Very efficiently.
Ha! Well said!

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 21 '07 #8

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

Similar topics

5
by: tgiles | last post by:
Hi, all. I'm YAPN (Yet Another Python Newbie), started messing with it last night and so far, so good. Documentation exellent and the people seem friendly enough ;) Ok, I started playing...
3
by: Claudio Grondi | last post by:
Is there an already available script/tool able to extract records and generate proper HTML code out of the data stored in the Wikipedia SQL data base? e.g. converting all occurences of ] to <a...
2
by: ajay | last post by:
Hi I need to read data from flat files in memory and execute SQL query on the in-memory data. I do not have an option of using a database and I am using c# .Net to build my application. Does...
7
by: Ben Kim | last post by:
Hello all, I am coming from another development environment (Clarion for Windows) and they offer a special In-Memory driver which acts just like a database driver. What would be the appropriate...
2
by: Anonieko | last post by:
Hello ASPNET guru's, What is a clean way to go around the problem of displaying a GridView templated column where data can contain Single Quote ( ' )? I maybe too naive, but this is of course...
1
by: jamna | last post by:
Sir i am really in trouble becouse i have a flash template where all text should come from a XML file in to flash's Dynamic text tool and i want to apply CSS also on it but i am totally new in flash...
5
by: psbasha | last post by:
Hi , I will get the 50 values.Here I have tow scenarios. Case -1 ) 50 Values may be same data types 10,20,30,50 ....... Case-2) Different data types 10.0,20.0,'Yes',1
12
by: Mark | last post by:
Hi I'm looking to create a colapsable list like what we see in a news reader or on a forum. - Basically, any number of levels up to 10 (i.e. more than whats likely to be needed) - Able to...
11
by: jgoodnight | last post by:
Hi, I have a form with three combo boxes: Area, Sub-Area, and Factor. I've set up the Sub-Area combo box to list only those areas that are part of the selected area. I've set up the Factor combo...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.