473,406 Members | 2,439 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,406 software developers and data experts.

Memories

Dear All.

As far as i know that Then memeory is divided into the three segements

1. Heap;
2. Stack;
3. Data segmnet;

But I am not getting the exact picture of the diffrence and the
behaviour
of these, Can any one shed there knowledge on this.

And why we get the stack over flow ? As if I write a simple
code

int main () {

int (*main_ptr) (void);
main_ptr = main;
printf("%s\n", Hi);
(*main_ptr)();
return 0;

}

Now what are the steps to be considerd while writing the code so that I
may not get the stack over flow (To check there may not be stack over
flow)

Thnaks In Advance
Regards
Ranjeet

Nov 14 '05 #1
22 1422
a breezer regarding your first question

stack segement is used by the local variables and the function calls to
store formal parameters. take recursion for example, it has an inherent
requirement for a stack data-structure to store function calls in the
order that they are called and then return from each one of them in a
reverse order. Each function call uses an area on the stack segment.

heap is the place from where the program dynamically allocate memory to
different variables.

data segment contains common data storage requirement of a program..

Nov 14 '05 #2
to the second question ...

each program has a resource limit. Each program can only use a limited
amount of memory. In your code fragment, you are calling funtion "main"
recursively, without having a "return" condition from the recursion. As
a result , the program keeps pushing the function calls to the stack
till it ultimately results in a stack overfolw.

Nov 14 '05 #3
ra***********@gmail.com wrote:

As far as i know that Then memeory is divided into the three segements

1. Heap;
2. Stack;
3. Data segmnet;

But I am not getting the exact picture of the diffrence and the


You need both a spelling checker and to read the C standard. When
you do, please tell us where you find heap, stack, or data segment
mentioned.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #4


CBFalconer wrote:
ra***********@gmail.com wrote:

As far as i know that Then memeory is divided into the three segements

1. Heap;
2. Stack;
3. Data segmnet;

But I am not getting the exact picture of the diffrence and the
You need both a spelling checker and to read the C standard. When
you do, please tell us where you find heap, stack, or data segment
mentioned.


I will really do the spell check on my statements, So that it may
not be the concern for others in near future, I will really
follow your advice, Presently, I just want to know about the memories
may be off topic to you, but really i want to know how my code
behaves. (To Try To Catch The Flow)

I am really sorry my native is not english but nevertheless I am
trying my level best to present and put my querries at the best
in English

Thanking you
Ranjeet
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


Nov 14 '05 #5
"sunny" <su*******@gmail.com> wrote:

[ Learn to post, dammit! Google's braindeadness is no excuse for you to
strive for the same quality of quoting. ]
chek this link :
http://www.dirac.org/linux/gdb/02-Me..._The_Stack.php


Or rather, do not, since it has nothing to do with C, everything with
Linux, and tells you not a whit about other OSes - and is therefore
useless for halfway dependable C programming.

Richard
Nov 14 '05 #7
ra***********@gmail.com wrote:

Dear All.

As far as i know that Then memeory is divided into the three segements
Maybe your memory is.
1. Heap;
2. Stack;
3. Data segmnet;


That's got nothing to do with C.

--
pete
Nov 14 '05 #8
ra***********@gmail.com writes:
Dear All.

As far as i know that Then memeory is divided into the three segements

1. Heap;
2. Stack;
3. Data segmnet;
That may be true in some implementations, but none of these are C
terms. In some implementations, malloc() and friends allocate memory
from the "heap", local variables are on the "stack", and static and
global variables are in the "data segment" -- but others may do things
differently.

[...]
And why we get the stack over flow ? As if I write a simple
code

int main () {

int (*main_ptr) (void);
main_ptr = main;
printf("%s\n", Hi);
(*main_ptr)();
return 0;

}

Now what are the steps to be considerd while writing the code so that I
may not get the stack over flow (To check there may not be stack over
flow)


Well, you'll never get a stack overflow with that program, because it
won't compile. 8-)} You want Hi to be a string literal, not an
identifier. Also, there's not much point in using a function pointer;
you can just call main directly. Here's a corrected version:

#include <stdio.h> /* necessary for printf */
int main(void)
{
printf("Hi\n");
return main();
}

If the compiler performs tail-recursion optimization, transforming the
recursive call to a loop, this will just print "Hi" forever with no
stack overflow. Otherwise, it will almost certainly run out of
memory.

There's no good way in C to determine how much memory is available
before doing a function call. Just avoid infinite recursion and hope
there's enough space.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #9


Keith Thompson wrote:
ra***********@gmail.com writes:
Dear All.

As far as i know that Then memeory is divided into the three segements

1. Heap;
2. Stack;
3. Data segmnet;
That may be true in some implementations, but none of these are C
terms. In some implementations, malloc() and friends allocate memory
from the "heap", local variables are on the "stack", and static and
global variables are in the "data segment" -- but others may do things
differently.

[...]
And why we get the stack over flow ? As if I write a simple
code

int main () {

int (*main_ptr) (void);
main_ptr = main;
printf("%s\n", Hi);
(*main_ptr)();
return 0;

}

Now what are the steps to be considerd while writing the code so that I
may not get the stack over flow (To check there may not be stack over
flow)


Well, you'll never get a stack overflow with that program, because it
won't compile. 8-)} You want Hi to be a string literal, not an
identifier. Also, there's not much point in using a function pointer;
you can just call main directly. Here's a corrected version:

#include <stdio.h> /* necessary for printf */
int main(void)
{
printf("Hi\n");
return main();
}

If the compiler performs tail-recursion optimization, transforming the
recursive call to a loop, this will just print "Hi" forever with no
stack overflow.


I just checked your above code... but still it says the stack over
flow,

Otherwise, it will almost certainly run out of memory.

There's no good way in C to determine how much memory is available
before doing a function call. Just avoid infinite recursion and hope
there's enough space.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Nov 14 '05 #10
Keith Thompson wrote:
ra***********@gmail.com writes:
Dear All.

As far as i know that Then memeory is divided into the three segements

1. Heap;
2. Stack;
3. Data segmnet;

That may be true in some implementations, but none of these are C
terms. In some implementations, malloc() and friends allocate memory
from the "heap", local variables are on the "stack", and static and
global variables are in the "data segment" -- but others may do things
differently.

[...]

....
There's no good way in C to determine how much memory is available
before doing a function call. Just avoid infinite recursion and hope
there's enough space.


size_t freespace = alloca(0) - sbrk(0) + 1;
Nov 14 '05 #11


Keith Thompson wrote:
ra***********@gmail.com writes:
Dear All.

As far as i know that Then memeory is divided into the three segements

1. Heap;
2. Stack;
3. Data segmnet;


That may be true in some implementations, but none of these are C
terms. In some implementations, malloc() and friends allocate memory
from the "heap", local variables are on the "stack", and static and
global variables are in the "data segment" -- but others may do things
differently.

hi as u r writing that static and global will be in data segment , data
segment is the part which is copied if an application is all ready in
memory .
eg. if internate exploreer is allready open and we have clicked it once
more a new code copy will not be there only the data segment part will
be cpoied in memory and same codesegment will be used .
is it correct? it is .

now
i have program a application in which i have use a global static
variable.
if the data segment is holding the static and global variables so that
segment should be copied to other segment (as a new instance of that
process play) but what is happening that global static variable is
incrementing for the all three instance of that process are running.

Nov 14 '05 #12
ra***********@gmail.com writes:
Keith Thompson wrote:

[...]
#include <stdio.h> /* necessary for printf */
int main(void)
{
printf("Hi\n");
return main();
}

If the compiler performs tail-recursion optimization, transforming the
recursive call to a loop, this will just print "Hi" forever with no
stack overflow.


I just checked your above code... but still it says the stack over
flow,


Read what I wrote above again. Apparently your compiler doesn't
perform the optimization.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #13
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
Keith Thompson wrote:

[...]
There's no good way in C to determine how much memory is available
before doing a function call. Just avoid infinite recursion and hope
there's enough space.


size_t freespace = alloca(0) - sbrk(0) + 1;


The word "good" was intended to exclude non-portable code.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #14
Russell Shaw wrote:
Keith Thompson wrote:
.... snip ...
There's no good way in C to determine how much memory is available
before doing a function call. Just avoid infinite recursion and
hope there's enough space.


size_t freespace = alloca(0) - sbrk(0) + 1;


None of those calls are portable, and even if they exist for their
usual (non-standardized) purposes there is no guarantee they
cooperate in that manner. Please don't post system specific code
without plainly identifying it as such. Some innocent might take
you seriously.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #15
CBFalconer wrote:
Please don't post system specific code without plainly identifying it
as such. Some innocent might take you seriously.


Think of the children!

:-)
Nov 14 '05 #16
>As far as i know that Then memeory is divided into the three segements

1. Heap;
2. Stack;
3. Data segmnet;


Memory is divided into 4 segments, not usually all found
in the same machine at the same time:
1. Leased
2. Mortgaged
3. Fully Paid For
4. Stolen

Gordon L. Burditt
Nov 14 '05 #17
"Class that cannot be inherited ... C++" <er*******@gmail.com> writes:
Keith Thompson wrote:
ra***********@gmail.com writes:
> Dear All.
>
> As far as i know that Then memeory is divided into the three segements
>
> 1. Heap;
> 2. Stack;
> 3. Data segmnet;
That may be true in some implementations, but none of these are C
terms. In some implementations, malloc() and friends allocate memory
from the "heap", local variables are on the "stack", and static and
global variables are in the "data segment" -- but others may do things
differently.

hi as u r writing that static and global will be in data segment , data
segment is the part which is copied if an application is all ready in
memory .
eg. if internate exploreer is allready open and we have clicked it once
more a new code copy will not be there only the data segment part will
be cpoied in memory and same codesegment will be used .
is it correct? it is .


Please don't use abbreviations like "u" and "r"; take the time to
spell out the words. Thank you.
now
i have program a application in which i have use a global static
variable.
if the data segment is holding the static and global variables so that
segment should be copied to other segment (as a new instance of that
process play) but what is happening that global static variable is
incrementing for the all three instance of that process are running.


The behavior you're describing is system-specific. The C standard
doesn't even address the possibility of more than one program running
simultaneously.

If multiple copies of the same program are running simultaneously,
presumably they're not going to interfere with each other's variables
(if they did, they wouldn't be behaving in accordance with the
language standard). How this is done is up to the implementation.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #18

<ra***********@gmail.com> wrote

I will really do the spell check on my statements, So that it may
not be the concern for others in near future, I will really
follow your advice, Presently, I just want to know about the memories
may be off topic to you, but really i want to know how my code
behaves. (To Try To Catch The Flow)

You need to think of C as a portable assembler that runs on a virtual
machine. However you don't know exactly how this virtual machine is
designed. You know it accesses memory through addresses, but you don't know
how many bits it takes to hold an address. You know it has at least a few
kilobytes of memory installed, but you don't know exactly how much, and it
might be several terabytes. You know that all the memory is identical to
you, the C programmer, though physically it may be held on different chips
with different access speeds.

So you can indeed think of this conceptual machine as having a "stack", a
"heap" and a "data segment". You might also want to divide the "data
segment" into read-only and read/write memory.

Local variables go on the stack. When a function returns, the local
variables are removed from the stack. When another function is called, that
space is reused for the new function to work in.
malloc() allocates memory from the heap. Once memory is taken from the heap,
it is marked as "used" in some way, and is yours to do what you want with.
When you call free(0 the meory is returned to the heap and might be used by
the next call to malloc(), or maybe by a different program running on the
same machine.
The data segment is a fixed-size chunk of memory that hold all the globals
and static local variables. It persists throughout function calls, and
unlike memory from the heap its size is fixed for the life of the program.

However it is important to realise that this virtual machine is only a rough
representation of reality. Some posters here might say that it is such a
rough representation that in fact it is not even an aid to understanding.
Nov 14 '05 #19
On 9 Jun 2005 22:32:44 -0700, ra***********@gmail.com wrote:
CBFalconer wrote:
You need both a spelling checker and to read the C standard. When
you do, please tell us where you find heap, stack, or data segment
mentioned.


<snip>
I am really sorry my native is not english but nevertheless I am
trying my level best to present and put my querries at the best
in English


Don't worry about your English Ranjeet. Some might make an issue out
of it but it's good enough for the (tolerant) majority of us. English
is not my native language either and there are many more like that
here.

Besides : I don't even believe that English is required to post to
this newsgroup (although it will maximize the response, of course :-)

Nov 14 '05 #20
Paul Mesken wrote:
On 9 Jun 2005 22:32:44 -0700, ra***********@gmail.com wrote:
CBFalconer wrote:

You need both a spelling checker and to read the C standard. When
you do, please tell us where you find heap, stack, or data segment
mentioned.


<snip>
I am really sorry my native is not english but nevertheless I am
trying my level best to present and put my querries at the best
in English


Don't worry about your English Ranjeet. Some might make an issue out
of it but it's good enough for the (tolerant) majority of us. English
is not my native language either and there are many more like that
here.


You may notice I didn't criticize his English, just the obviously
careless spelling and typos. A spelling checker will catch most of
those. Just looking twice will do the same, if not better.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #21
Warning: The following is at best marginally topical. I'm posting
this because it's relevant to the way people post to this newsgroup.

CBFalconer <cb********@yahoo.com> writes:
[...]
You may notice I didn't criticize his English, just the obviously
careless spelling and typos. A spelling checker will catch most of
those.
Agreed.
Just looking twice will do the same, if not better.


Not necessarily. I've found that some people are much better at
spelling words correctly, and at recognizing misspelled words, than
others -- and that the ability doesn't correlate well with general
intelligence or even facility with language. I happen to have the
knack myself, and I rarely use spell checkers. (Most of my
misspellings are typos; if I take the time to re-read what I've
written, I catch most or all of them.) Other people, as smart as I
am, have as much difficulty with correct spelling as I have with
legible handwriting.

I would imagine that coming to English from another language with more
consistent spelling (i.e., from any language other than English) just
makes things even more difficult.

I encourage everyone to make reasonable efforts to use correct
spelling and grammar, but I'm not going to complain about errors too
loudly unless they actually make an article significantly difficult to
read. (The "u" and "r" abbreviations fall into this category.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #22
On Sat, 11 Jun 2005 02:08:41 GMT, Keith Thompson
<ks***@mib.org> wrote:
Warning: The following is at best marginally topical. I'm posting
this because it's relevant to the way people post to this newsgroup.

CBFalconer <cb********@yahoo.com> writes:
[...]
You may notice I didn't criticize his English, just the obviously
careless spelling and typos. A spelling checker will catch most of
those.
Agreed.
Just looking twice will do the same, if not better.


Not necessarily. I've found that some people are much better at
spelling words correctly, and at recognizing misspelled words, than
others -- and that the ability doesn't correlate well with general
intelligence or even facility with language.


Most people are bad at noticing their own mistakes, unlees they leave it
for a long time before checking, because they know what they meant to
write. I do check emails and Usenet posts, but often find when I see
them come back (quoted or from a news server) that I've made typos which
I just couldn't see when I originally checked.
I happen to have the
knack myself, and I rarely use spell checkers.
I don't use spell checkers, I find that they don't catch my worst
mistakes ('thinkos', where the word typed is a real word but not the
correct one, it's amazing how many books get into print with 'that' and
'than' mixed up) and they distract me from the meaning with words which
are perfectly valid but not in the checker's dictionary.
(Most of my misspellings are typos; if I take the time to re-read what
I've written, I catch most or all of them.) Other people, as smart as
I am, have as much difficulty with correct spelling as I have with
legible handwriting.
I have several friends who are dyslexic. Let no one dare to say that
they aren't as 'smart' or 'intelligent' as me...
I would imagine that coming to English from another language with more
consistent spelling (i.e., from any language other than English) just
makes things even more difficult.
Is English provably the most inconsistent language for spelling? I can
believe it, I just haven't seen proof.
I encourage everyone to make reasonable efforts to use correct
spelling and grammar, but I'm not going to complain about errors too
loudly unless they actually make an article significantly difficult to
read. (The "u" and "r" abbreviations fall into this category.)


Indeed. If the writer appears to be saying something different from
what they mean (as in the recent example) then a friendly suggestion on
the lines of "I'm not sure what you mean here, in English we would say X
to mean ..." may be in order, and most people writing in a language in
which they are not completely fluent will be glad of the help.

(Personally I find it hard to read most sentences which end with a
preposition, but usually I don't say anything unless it really doesn't
make sense to me.)

Chris C
Nov 14 '05 #23

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

Similar topics

5
by: anonymous | last post by:
with X Y separate dual-bus super sub , maybe reverse by freq Y is super-bus X or Y maybe set to internal base ( default ) X@ X! Y@ Y! ( top of stack X@ y@ --> TOS ) >X >X >Y Y> intelligent...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
0
by: motoman | last post by:
i have the following code (see below), i run my validation and get unexpected results. Public ID: null System ID: file:///C:/DATA/Sandpit/Eclipse/workspace/dtdValidation/memory.xml Line number:...
5
by: Matthew | last post by:
Recently, I began to write a program that demostrates memory representation as a network of concepts. AI is a hobby of mine and I just began to throw the program together. I didn't use the best...
0
by: Robert Jarrett | last post by:
Trying to resolve what seems a complicated issue (to me); will simplify description of issue w/ an analogy: A -- 5 tables - Memories, engrams, P (persons), L (locations), and T (things). All start...
2
by: volker_nitschke | last post by:
Hi, I want to develop a program, that transmits several classes between two processes. It should be built after the guidelines of the iso/osi-reference model and work with a shared memory. At...
2
by: Yasin Cepeci | last post by:
Server Error in '/' Application. -------------------------------------------------------------------------------- Access to the path...
3
by: manu1001 | last post by:
I need to allocate memory of a few 100's of KB, using DOS on an Intel x86 processor. There is a way around using TurboC compiler specific features(messy). But can it be done with ANSI C? (The...
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: 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: 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,...
0
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...
0
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...

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.