473,386 Members | 1,706 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,386 software developers and data experts.

Reg:Memory allocation

int x;
static int y;
int *p;
int main(void)
{
int i;
int j=9;
int *ptr;
char *str = "GOOGLE";
static int a;
int k[];
int q[]={1,2,3};
register int reg;
volatile int vol;
.......
.....
....
return 0;
}

According to my knowledge the variables declared in the above program
are stored as follows:
GLOBAL VAR's:
x,y are stored in INITIALISED PART OF DATA SEGMENT.
*p is stored in STACK SEGMENT.

LOCAL VAR's:
all variables are stored in STACK SEGMENT.

Am I correct? Please correct if i'm wrong.

Cheers & Regards,
Raghu.
HAPPY NEW YEAR-2008
Dec 29 '07 #1
15 1475
On Sat, 29 Dec 2007 00:48:39 -0600, raghu wrote
(in article
<9d**********************************@l6g2000prm.g ooglegroups.com>):
int x;
static int y;
int *p;
int main(void)
{
int i;
int j=9;
int *ptr;
char *str = "GOOGLE";
static int a;
int k[];
int q[]={1,2,3};
register int reg;
volatile int vol;
......
....
...
return 0;
}

According to my knowledge the variables declared in the above program
are stored as follows:
GLOBAL VAR's:
x,y are stored in INITIALISED PART OF DATA SEGMENT.
*p is stored in STACK SEGMENT.

LOCAL VAR's:
all variables are stored in STACK SEGMENT.

Am I correct? Please correct if i'm wrong.

Cheers & Regards,
Raghu.
HAPPY NEW YEAR-2008
You are making a lot of assumptions about the underlying hardware
architecture and the implementation details of a particular development
system. If you want to ask questions about such specifics, find an
appropriate newsgroup. Standard C makes no guarantees about "stack
segments" or "data segments".

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Dec 29 '07 #2
raghu wrote:
>
.... snip ...
>
According to my knowledge the variables declared in the above
program are stored as follows:
GLOBAL VAR's:
x,y are stored in INITIALISED PART OF DATA SEGMENT.
*p is stored in STACK SEGMENT.

LOCAL VAR's:
all variables are stored in STACK SEGMENT.

Am I correct? Please correct if i'm wrong.
Wrong. C has no such things as DATA SEGMENT or STACK SEGMENT. It
does have such things as automatic storage, static storage, dynamic
storage, which in turn have the features ascribed to them in the C
standard.

Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf(C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2(C99, txt)
<http://www.dinkumware.com/refxc.html (C-library}
<http://gcc.gnu.org/onlinedocs/ (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Dec 29 '07 #3
On Dec 29, 2:48*pm, raghu <raghujin...@gmail.comwrote:
int x;
static int y;
int *p;
int main(void)
{
int i;
int j=9;
int *ptr;
char *str = "GOOGLE";
static int a;
int k[];
int q[]={1,2,3};
register int reg;
volatile int vol;
......
....
...
return 0;

}

According to my knowledge the variables declared in the above program
are stored as follows:
GLOBAL VAR's:
x,y are stored in INITIALISED PART OF DATA SEGMENT.
*p is stored in STACK SEGMENT.

LOCAL VAR's:
all variables are stored in STACK SEGMENT.

Am I correct? Please correct if i'm wrong.

Cheers & Regards,
Raghu.
HAPPY NEW YEAR-2008
why the pointer p allocated in the stack segment?
as far as I am concerned, it should also be allocated in the Data
Segment.
Dec 29 '07 #4
On Fri, 28 Dec 2007 22:48:39 -0800, raghu wrote:

Snip list of variable declarations & definitions.
According to my knowledge the variables declared in the above program
are stored as follows:
The C standard doesn't say. Some platforms follow the model you suggest,
others don't. If you actually need to know, then you're outside the realm
of C programming. With regard to local variables, you're wrong with at
least one of of them, probably two.
Dec 29 '07 #5
On Fri, 28 Dec 2007 22:48:39 -0800 (PST)
raghu <ra*********@gmail.comwrote:

Hi Raghu.
int x;
static int y;
int *p;
int main(void)
{
int i;
int j=9;
int *ptr;
char *str = "GOOGLE";
static int a;
int k[];
int q[]={1,2,3};
register int reg;
volatile int vol;
......
....
...
return 0;
}

According to my knowledge the variables declared in the above program
are stored as follows:
GLOBAL VAR's:
x,y are stored in INITIALISED PART OF DATA SEGMENT.
*p is stored in STACK SEGMENT.

LOCAL VAR's:
all variables are stored in STACK SEGMENT.

Am I correct? Please correct if i'm wrong.
None of this matters for writing portable C programs - all that
matters, from the C point of view, is the lifetime of the object. There
are three possible object lifetimes in C - static storage duration,
which applies to file-scope objects and block-scope objects declared
with the static keyword, automatic storage duration, which applies to
all other objects declared at block scope, and dynamic storage
duration, which applies to objects created with malloc(), calloc() or
realloc().

Static storage duration means the object lives for as long as the
program does. Automatic storage duration means the object is created
when the program begins executing the block it's declared in, and ceases
to exist when the end of the block is reached. Dynamic storage
duration means the object is created at the time of the malloc(), calloc
() or realloc () call, and ceases to exist at the time of the
corresponding free().

Best regards!
Cheers & Regards,
Raghu.
HAPPY NEW YEAR-2008
Dec 29 '07 #6

Hi,
I did not get the answer what I wanted.

Cheers,
Raghu
HNY-2008


On Dec 30, 3:16 am, Rico Secada <coolz...@it.dkwrote:
On Fri, 28 Dec 2007 22:48:39 -0800 (PST)

raghu <raghujin...@gmail.comwrote:

Hi Raghu.
int x;
static int y;
int *p;
int main(void)
{
int i;
int j=9;
int *ptr;
char *str = "GOOGLE";
static int a;
int k[];
int q[]={1,2,3};
register int reg;
volatile int vol;
......
....
...
return 0;
}
According to my knowledge the variables declared in the above program
are stored as follows:
GLOBAL VAR's:
x,y are stored in INITIALISED PART OF DATA SEGMENT.
*p is stored in STACK SEGMENT.
LOCAL VAR's:
all variables are stored in STACK SEGMENT.
Am I correct? Please correct if i'm wrong.

None of this matters for writing portable C programs - all that
matters, from the C point of view, is the lifetime of the object. There
are three possible object lifetimes in C - static storage duration,
which applies to file-scope objects and block-scope objects declared
with the static keyword, automatic storage duration, which applies to
all other objects declared at block scope, and dynamic storage
duration, which applies to objects created with malloc(), calloc() or
realloc().

Static storage duration means the object lives for as long as the
program does. Automatic storage duration means the object is created
when the program begins executing the block it's declared in, and ceases
to exist when the end of the block is reached. Dynamic storage
duration means the object is created at the time of the malloc(), calloc
() or realloc () call, and ceases to exist at the time of the
corresponding free().

Best regards!
Cheers & Regards,
Raghu.
HAPPY NEW YEAR-2008


Dec 31 '07 #7
raghu <ra*********@gmail.comwrites:
Hi,
I did not get the answer what I wanted.
But you did get a correct answer.

And please don't top-post. See the following:

http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 31 '07 #8
In article <8b**********************************@e25g2000prg. googlegroups.com>,
raghu <ra*********@gmail.comwrote:
>Hi,
I did not get the answer what I wanted.
Well to be explicit then, the answer is "NO", C variables are NOT
stored in the locations you believe them to be stored in. One of the
other posters provided a long reply describing where C variables
-are- stored.

Parts of what you described might be true on a particular version
of a particular compiler when used on a particular version of
a specific operating system, but there are real C implementations
around in which -none- of what you described is true -- including
their not having a stack in a form you would recognize.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Dec 31 '07 #9
On Sun, 30 Dec 2007 19:51:46 -0800, raghu wrote:
Hi,
I did not get the answer what I wanted.
If you knew what answer you wanted, why did you ask?

Anyway lots of people answered your question correctly. You should read
those answers and make sure you understand them.
Dec 31 '07 #10
raghu wrote:
>
I did not get the answer what I wanted.
You top-posted. This makes it impossible to get civilized answers.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Jan 1 '08 #11
CBFalconer said:
raghu wrote:
>>
I did not get the answer what I wanted.

You top-posted.
Yet again, you're trying to enforce netiquette without observing it
yourself.
This makes it impossible to get civilized answers.
Rubbish. Top-posting certainly makes an article harder to read, but it
doesn't make it *impossible* to get civilised answers. That's simply
false.

--
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
Jan 1 '08 #12
William Pursell <bi**********@gmail.comwrites:
On Jan 1, 1:42 am, Keith Thompson <ks...@mib.orgwrote:
[...]
>The reader should recognize that stating that str "points to an array"
means that str is, for example, of type ``char (*)[7]''. In this case
(see above) it isn't, so the statement is incorrect.

I disagree. "points to an array" is a colloquialism that
has been in use for a long time that doesn't have the
strict technical meaning that this would ascribe to it.
I agree that it's common usage.
>Since C actually does have pointers to arrays, using the phrase
"pointer to an array" to mean something else is misleading. If you
have something that's actually of type ``char (*)[7]'', what are you
going to call it if you've already used "pointer to an array" to mean
something else?

I agree that "pointer to an array" should be interpreted
to mean "of type char (*)[n]". I think that "points
to an array" does not have the same meaning.
Hmm. I have trouble with the idea that "pointer to an array" and
"points to an array" have inconsistent meanings.
Examining
the usage of the phrase shows that to be the case.
Personally, I will try to avoid using the phrase
in the future, but I think it is important to recognize
that it is often used in the non-pedantically-correct
fashion.
I agree that's important to recognize the usage, but I'd replace
"non-pedantically-correct" with "incorrect". (Yeah, I'm a pedant;
I don't think that's a bad thing.)

[...]
I don't like this usage, but that is clearly
how C programmers interpret the phrase "points
to an array".
Yes, and I'm trying to correct the error. (Actually, I seem to be
trying to pound it into the ground.)
>(On the other hand, you can correctly say that str "points to a
string", since the standard specifically defines that phrase and
there's no ambiguity. This is because an array is a data type, but a
string is not.)

So the standard clarifies the meaning of the phrase
"points to a string",
Yes. Actually, the standard introduces and defines the phrase.
but leaves the phrase "points
to an array" ambiguous.
No, as far as the standard is concerned there's no ambiguity at all.
When someone like Chris
Torek uses the phrase "points to an array" to describe
an object of type 'char *', I take that to be a
fairly reasonable indicator that competent programmers
use the phrase in that way. (Please note that
I am not simply being argumentative here. I consider
Keith, Chris, and Richard to be extremely valuable
sources of information, and I greatly respect your
input to this group. I'm simply trying to decide
whether or not the phrase "points to an array"
should ever be used in polite company.)
Note that Chris corrected himself elsethread.

Saying that a pointer to the first element of an array "points to the
array" isn't a cardinal sin, and the meaning is *usually* clear enough
from context. And actual pointers to arrays are relatively rare. But
it really is incorrect.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 1 '08 #13
In article <fl*********@news3.newsguy.com>,
Chris Torek <no****@torek.netwrote:
>In article <9d**********************************@l6g2000prm.g ooglegroups.com>
raghu <ra*********@gmail.comwrote:
>>int q[]={1,2,3};

The array named "q" is initialized, so its size is computed from
the initializers. It has automatic duration. This declaration is
valid in C99, which allows initializers for automatic-duration
arrays, but not in C89, which does not. If you have a C89 compiler,
a diagnostic is required (of course a single diagnostic for both
the error for the array k and the error for the array q suffices,
although a good compiler will produce two separate diagnostics).
Are you sure? I thought aggregate types with constant initializers
were allowed even with automatic duration, and gcc -ansi -pedantic
doesn't complain about it:
--------
dj3vande@buttons:~/clc (0) $ cat array-init.c
int main(void)
{
int q[]={1,2,3};

/*returns 0 (success) if things act as I expect, and 1
(system-specified interpretation, will show up when my
shell prompt displays the exit status) otherwise
*/
return !(sizeof q == 3*sizeof(int));
}
dj3vande@buttons:~/clc (0) $ gcc -W -Wall -ansi -pedantic -O array-init.c
dj3vande@buttons:~/clc (0) $ ./a.out
dj3vande@buttons:~/clc (0) $
--------

(If I'm remembering correctly, I asked about the corresponding change
for struct initialization a few weeks ago, and the response was that
non-constant initialization for objects with aggregate types and
non-static storage duration was the new feature in C99.)
dave

Jan 4 '08 #14
>>In article <9d**********************************@l6g2000prm.g ooglegroups.com>
>>raghu <ra*********@gmail.comwrote:
>>>int q[]={1,2,3};
>In article <fl*********@news3.newsguy.com>,
Chris Torek <no****@torek.netwrote:
>>The array named "q" is initialized, so its size is computed from
the initializers. It has automatic duration. This declaration is
valid in C99, which allows initializers for automatic-duration
arrays, but not in C89, which does not. ...
In article <fl**********@rumours.uwaterloo.ca>,
<dj******@csclub.uwaterloo.ca.invalidwrote:
>Are you sure?
No. My C89 standard is in a box somewhere, and now I think my claim
above was incorrect. In particular:
>(If I'm remembering correctly, I asked about the corresponding change
for struct initialization a few weeks ago, and the response was that
non-constant initialization for objects with aggregate types and
non-static storage duration was the new feature in C99.)
I now think this is correct -- the C89 limitation is that all the
initializers for any automatic aggregate must be constants.

(I think PCC, one of the pre-C89 "K&R" compilers, did not accept
any kind of automatic aggregate initializers, even if all the
elements were constant.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jan 13 '08 #15
Chris Torek wrote:
>>In article <9d**********************************@l6g2000prm.g ooglegroups.com>
raghu <ra*********@gmail.comwrote:
int q[]={1,2,3};
>In article <fl*********@news3.newsguy.com>,
Chris Torek <no****@torek.netwrote:
>>The array named "q" is initialized, so its size is computed from
the initializers. It has automatic duration. This declaration is
valid in C99, which allows initializers for automatic-duration
arrays, but not in C89, which does not. ...

In article <fl**********@rumours.uwaterloo.ca>,
<dj******@csclub.uwaterloo.ca.invalidwrote:
>Are you sure?

No. ...
Good grief. I am shocked, shocked ...
Jan 14 '08 #16

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

Similar topics

5
by: James Butler | last post by:
Running a CLI script that fopen's a file, parses the lines into an array, checks the array entries against a few regular expression qualifiers (i.e. !eregi("bot",$entry)) and dump the good entries...
8
by: Praveen Kumar Madis | last post by:
Hi Can anybody tell me, where memory will be allocated for Function parameters and return value of a function. Are these stored on stack area, or data area, or any where else. Reg Praveen
11
by: Sushil | last post by:
Hi Gurus I've tried to come up with a small logical example of my problem. The problem is platform specific (MIPS) which I understand should not be discussed here. So here goes my example: ...
7
by: Harsh_forC | last post by:
hello folks, when i'm using 'System' funtion in TC, perror function is outputting the following error msg..." Not enough memory" wat does it mean? how to get rid of this situaiton? plz help me...
13
by: Samshayam | last post by:
I have come across the application of placement new in memory mapped i/o in a number of books.I am not able to understand it completely, may be becaues of my lack of knowledge with memory mapped...
10
by: John Salerno | last post by:
Is there a way to 'install' and use Python on a memory stick, just as you would on any computer? I use Windows, and I know the installation does things with the registry, so probably I couldn't use...
39
by: Ravi | last post by:
Can you all please suggest a program which tell us the range of memry addresses occupied by the given c program?
9
by: Joshua | last post by:
I posted this originally in the csharp group, but I think that may be the wrong group. This seems more appropriate: I'm running into an issue with a memory leak in an Asp.Net web page. In the...
2
by: shariquehabib | last post by:
Hi All, I m creating one exe using C programing and i m facing one issue in this. I defined an array which size was 1024 (like SortOnPrimarySol )Now i want to increase this size to more than this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.