Memories

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

  1. Guest

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

  2. 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
    #2
    1. Advertising

  3. 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
    #3
  4. CBFalconer Guest

    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
     
    CBFalconer, Jun 10, 2005
    #4
  5. Guest

    CBFalconer wrote:
    > 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
     
    , Jun 10, 2005
    #5
  6. sunny Guest

  7. Richard Bos Guest

    "sunny" <> 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-Memory_Layout_And_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
     
    Richard Bos, Jun 10, 2005
    #7
  8. pete Guest

    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
     
    pete, Jun 10, 2005
    #8
  9. 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. :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.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jun 10, 2005
    #9
  10. Guest

    Keith Thompson wrote:
    > 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. :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
    > 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) <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.
     
    , Jun 10, 2005
    #10
  11. Russell Shaw Guest

    Keith Thompson wrote:
    > 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;
     
    Russell Shaw, Jun 10, 2005
    #11
  12. Keith Thompson wrote:
    > 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.
     
    Class that cannot be inherited ... C++, Jun 10, 2005
    #12
  13. 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) <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.
     
    Keith Thompson, Jun 10, 2005
    #13
  14. 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) <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.
     
    Keith Thompson, Jun 10, 2005
    #14
  15. CBFalconer Guest

    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
     
    CBFalconer, Jun 10, 2005
    #15
  16. Grumble Guest

    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!

    :)
     
    Grumble, Jun 10, 2005
    #16
  17. >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
     
    Gordon Burditt, Jun 10, 2005
    #17
  18. "Class that cannot be inherited ... C++" <> writes:
    > Keith Thompson wrote:
    >> 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) <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.
     
    Keith Thompson, Jun 10, 2005
    #18
  19. Malcolm Guest

    <> 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.
     
    Malcolm, Jun 10, 2005
    #19
  20. Paul Mesken Guest

    On 9 Jun 2005 22:32:44 -0700, 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 :)
     
    Paul Mesken, Jun 11, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fpgawizz

    modelsim - looking at memories

    fpgawizz, Apr 4, 2005, in forum: VHDL
    Replies:
    2
    Views:
    514
    fpgawizz
    Apr 4, 2005
  2. Marc Jeambrun

    Xilinx memories

    Marc Jeambrun, Aug 3, 2006, in forum: VHDL
    Replies:
    3
    Views:
    581
    Eric Smith
    Aug 14, 2006
  3. Replies:
    2
    Views:
    7,919
    Marcus Harnisch
    Oct 30, 2006
  4. Replies:
    2
    Views:
    439
    Jim Langston
    Jul 12, 2006
  5. RealInfo
    Replies:
    0
    Views:
    420
    RealInfo
    Apr 16, 2009
Loading...

Share This Page