Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Memories (http://www.velocityreviews.com/forums/t438341-memories.html)

ranjeet.gupta@gmail.com 06-10-2005 04:13 AM

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


sunny 06-10-2005 04:23 AM

Re: Memories
 
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..


sunny 06-10-2005 04:26 AM

Re: Memories
 
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.


CBFalconer 06-10-2005 05:00 AM

Re: Memories
 
ranjeet.gupta@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



ranjeet.gupta@gmail.com 06-10-2005 05:32 AM

Re: Memories
 


CBFalconer wrote:
> ranjeet.gupta@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



sunny 06-10-2005 06:53 AM

Re: Memories
 
chek this link :
http://www.dirac.org/linux/gdb/02-Me..._The_Stack.php


Richard Bos 06-10-2005 07:19 AM

Re: Memories
 
"sunny" <sunnynnus@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

pete 06-10-2005 07:58 AM

Re: Memories
 
ranjeet.gupta@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

Keith Thompson 06-10-2005 08:01 AM

Re: Memories
 
ranjeet.gupta@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) kst-u@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.

ranjeet.gupta@gmail.com 06-10-2005 08:12 AM

Re: Memories
 


Keith Thompson wrote:
> ranjeet.gupta@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) kst-u@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.




All times are GMT. The time now is 07:08 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57