![]() |
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 |
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.. |
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. |
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 |
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 |
Re: Memories
|
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 |
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 |
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. |
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.