Discussion in 'C Programming' started by ranjeet.gupta, Jun 10, 2005.

  1. 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
    of these, Can any one shed there knowledge on this.

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

    int main () {

    int (*main_ptr) (void);
    main_ptr = main;
    printf("%s\n", Hi);
    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

    Thnaks In Advance
    ranjeet.gupta, Jun 10, 2005
    1. Advertisements

  2. ranjeet.gupta

    sunny Guest

    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, Jun 10, 2005
    1. Advertisements

  3. ranjeet.gupta

    sunny Guest

    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.
    sunny, Jun 10, 2005
  4. ranjeet.gupta

    CBFalconer Guest

    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
    CBFalconer, Jun 10, 2005
  5. 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.gupta, Jun 10, 2005
  6. ranjeet.gupta

    sunny Guest

  7. ranjeet.gupta

    Richard Bos Guest

    [ Learn to post, dammit! Google's braindeadness is no excuse for you to
    strive for the same quality of quoting. ]
    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 Bos, Jun 10, 2005
  8. ranjeet.gupta

    pete Guest

    Maybe your memory is.
    That's got nothing to do with C.
    pete, Jun 10, 2005
  9. 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

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

    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, Jun 10, 2005
  10. I just checked your above code... but still it says the stack over

    Otherwise, it will almost certainly run out of
    ranjeet.gupta, Jun 10, 2005
  11. ranjeet.gupta

    Russell Shaw Guest

    size_t freespace = alloca(0) - sbrk(0) + 1;
    Russell Shaw, Jun 10, 2005
  12. 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 .

    i have program a application in which i have use a global static
    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.
    Class that cannot be inherited ... C++, Jun 10, 2005
  13. Read what I wrote above again. Apparently your compiler doesn't
    perform the optimization.
    Keith Thompson, Jun 10, 2005
  14. The word "good" was intended to exclude non-portable code.
    Keith Thompson, Jun 10, 2005
  15. ranjeet.gupta

    CBFalconer Guest

    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.
    CBFalconer, Jun 10, 2005
  16. ranjeet.gupta

    Grumble Guest

    Think of the children!

    Grumble, Jun 10, 2005
  17. As far as i know that Then memeory is divided into the three segements
    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
    Gordon Burditt, Jun 10, 2005
  18. Please don't use abbreviations like "u" and "r"; take the time to
    spell out the words. Thank you.
    The behavior you're describing is system-specific. The C standard
    doesn't even address the possibility of more than one program running

    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, Jun 10, 2005
  19. ranjeet.gupta

    Malcolm Guest

    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.
    Malcolm, Jun 10, 2005
  20. ranjeet.gupta

    Paul Mesken Guest

    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

    Besides : I don't even believe that English is required to post to
    this newsgroup (although it will maximize the response, of course :)
    Paul Mesken, Jun 11, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.