Memories

R

ranjeet.gupta

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
 
S

sunny

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..
 
S

sunny

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.
 
C

CBFalconer

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.
 
R

ranjeet.gupta

CBFalconer said:
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
 
K

Keith Thompson

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. :cool:} 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.
 
R

ranjeet.gupta

Keith said:
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. :cool:} 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
 
R

Russell Shaw

Keith said:
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;
 
C

Class that cannot be inherited ... C++

Keith said:
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.
 
K

Keith Thompson

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.
 
K

Keith Thompson

Russell Shaw said:
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.
 
C

CBFalconer

Russell said:
Keith Thompson wrote:
.... snip ...


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.
 
G

Grumble

CBFalconer said:
Please don't post system specific code without plainly identifying it
as such. Some innocent might take you seriously.

Think of the children!

:)
 
G

Gordon Burditt

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
 
K

Keith Thompson

Class that cannot be inherited ... C++ said:
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.
 
M

Malcolm

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.
 
P

Paul Mesken

CBFalconer wrote:

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top