Newbie Question about Memory in C++

Discussion in 'C++' started by adsci, Jan 26, 2007.

  1. adsci

    adsci Guest

    Hello there!

    ive got a question which can be surely answered easily, but i dont know
    the answer.

    If you allocate memory by new/delete on the heap theres always the
    possibility that the allocation failes and your programm dies horribly
    if you dont check for failed allocations (try/catch or new == NULL). okay.

    but whats with stack variables?
    even an

    int i;

    should fail if there are not sizeof(int) bytes available.
    even more a
    int intarray[99999999999];

    what actually happens if thats the case? how can you check for that?

    Thank You Very Much! and forgive me my newbie question ;(

    Regards
    Marc
     
    adsci, Jan 26, 2007
    #1
    1. Advertising

  2. adsci wrote:
    > Hello there!
    >
    > ive got a question which can be surely answered easily, but i dont know
    > the answer.
    >
    > If you allocate memory by new/delete on the heap theres always the
    > possibility that the allocation failes and your programm dies horribly
    > if you dont check for failed allocations (try/catch or new == NULL). okay.
    >
    > but whats with stack variables?
    > even an
    >
    > int i;
    >
    > should fail if there are not sizeof(int) bytes available.
    > even more a
    > int intarray[99999999999];
    >
    > what actually happens if thats the case? how can you check for that?
    >
    > Thank You Very Much! and forgive me my newbie question ;(


    What actually happens is your program dies a horrible death :)

    And, you can't check for that. It is a design error. There should be
    guidance available on how much stack space is available, and in some
    cases you can even define your program's stack size requirement (in
    various platform-specific ways).

    --
    Scott McPhillips [VC++ MVP]
     
    Scott McPhillips [MVP], Jan 27, 2007
    #2
    1. Advertising

  3. On Fri, 26 Jan 2007 14:48:22 +0100, adsci <> wrote:

    >Hello there!
    >
    >ive got a question which can be surely answered easily, but i dont know
    >the answer.
    >
    >If you allocate memory by new/delete on the heap theres always the
    >possibility that the allocation failes and your programm dies horribly
    >if you dont check for failed allocations (try/catch or new == NULL). okay.


    That's not entirely correct. new throws a std::bad_alloc exception when
    running out of memory, unless you use std::nothrow.

    >but whats with stack variables?
    >even an
    >
    >int i;
    >
    >should fail if there are not sizeof(int) bytes available.
    >even more a
    >int intarray[99999999999];


    That's right.

    >what actually happens if thats the case? how can you check for that?


    The standard does not address stack allocation concerns (in fact, it doesn't
    even require the existence of a stack!) You must check with your specific
    compiler's documentation as to the behavior.

    Most likely, the stack will grow such that you stomp on some data that you'll
    later want to use, which will cause a mysterious error further down the road;
    or you'll violate some virtual memory boundary. Or nothing at all. Or the
    universe will collapse. Who knows.

    -dr

    PS: Okay okay, the standard does mention "stack unwinding", but even that term
    doesn't refer to an actual data structure, only to a prescribed sequence of
    events.
     
    Dave Rahardja, Jan 27, 2007
    #3
  4. adsci

    terminator Guest

    On Jan 26, 4:48 pm, adsci <> wrote:
    > Hello there!
    >
    > ive got a question which can be surely answered easily, but i dont know
    > the answer.
    >
    > If you allocate memory by new/delete on the heap theres always the
    > possibility that the allocation failes and your programm dies horribly
    > if you dont check for failed allocations (try/catch or new == NULL). okay.
    >
    > but whats with stack variables?
    > even an
    >
    > int i;
    >
    > should fail if there are not sizeof(int) bytes available.
    > even more a
    > int intarray[99999999999];
    >
    > what actually happens if thats the case? how can you check for that?
    >
    > Thank You Very Much! and forgive me my newbie question ;(
    >
    > Regards
    > Marc


    for stack: if u r not concerned about portabality you can write an
    stack cheking function in assembly.
     
    terminator, Jan 28, 2007
    #4
  5. On Jan 28, 10:43 am, "terminator" <> wrote:
    > On Jan 26, 4:48 pm, adsci <> wrote:
    > > Hello there!

    >
    > > ive got a question which can be surely answered easily, but i dont know
    > > the answer.

    >
    > > If you allocate memory by new/delete on the heap theres always the
    > > possibility that the allocation failes and your programm dies horribly
    > > if you dont check for failed allocations (try/catch or new == NULL).. okay.

    >
    > > but whats with stack variables?
    > > even an

    >
    > > int i;

    >
    > > should fail if there are not sizeof(int) bytes available.
    > > even more a
    > > int intarray[99999999999];

    >
    > > what actually happens if thats the case? how can you check for that?

    >
    > for stack: if u r not concerned about portabality you can write an
    > stack cheking function in assembly.


    Just be aware that calling such a function might cause your program to
    die the horrible death you are talking about.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 29, 2007
    #5
  6. adsci

    terminator Guest

    On Jan 29, 10:59 am, "Erik Wikström" <>
    wrote:
    > On Jan 28, 10:43 am, "terminator" <> wrote:
    >
    >
    >
    >
    >
    > > On Jan 26, 4:48 pm, adsci <> wrote:
    > > > Hello there!

    >
    > > > ive got a question which can be surely answered easily, but i dont know
    > > > the answer.

    >
    > > > If you allocate memory by new/delete on the heap theres always the
    > > > possibility that the allocation failes and your programm dies horribly
    > > > if you dont check for failed allocations (try/catch or new == NULL). okay.

    >
    > > > but whats with stack variables?
    > > > even an

    >
    > > > int i;

    >
    > > > should fail if there are not sizeof(int) bytes available.
    > > > even more a
    > > > int intarray[99999999999];

    >
    > > > what actually happens if thats the case? how can you check for that?

    >
    > > for stack: if u r not concerned about portabality you can write an
    > > stack cheking function in assembly.Just be aware that calling such a function might cause your program to

    > die the horrible death you are talking about.
    >
    > --
    > Erik Wikström- Hide quoted text -- Show quoted text -


    death occurs when debugging but once you make sure of the performance
    the problem is solved for ever .after all some library functions where
    originally written in asm.
     
    terminator, Jan 29, 2007
    #6
  7. adsci

    bjeremy Guest


    >
    > should fail if there are not sizeof(int) bytes available.
    > even more a
    > int intarray[99999999999];
    >
    > what actually happens if thats the case? how can you check for that?
    >
    > Thank You Very Much! and forgive me my newbie question ;(
    >
    > Regards
    > Marc


    Stack Overflow Error... kiss your stack goodbye.
     
    bjeremy, Jan 29, 2007
    #7
  8. adsci

    adsci Guest

    adsci schrieb:
    > Hello there!
    >
    > ive got a question which can be surely answered easily, but i dont know
    > the answer.
    >
    > If you allocate memory by new/delete on the heap theres always the
    > possibility that the allocation failes and your programm dies horribly
    > if you dont check for failed allocations (try/catch or new == NULL). okay.
    >
    > but whats with stack variables?
    > even an
    >
    > int i;
    >
    > should fail if there are not sizeof(int) bytes available.
    > even more a
    > int intarray[99999999999];
    >
    > what actually happens if thats the case? how can you check for that?
    >
    > Thank You Very Much! and forgive me my newbie question ;(
    >
    > Regards
    > Marc



    Many Thanks to You all! it puzzled me many days.
     
    adsci, Jan 29, 2007
    #8
    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. jose luis fernandez diaz

    Newbie question: Array memory address

    jose luis fernandez diaz, Nov 5, 2004, in forum: C++
    Replies:
    10
    Views:
    654
    jose luis fernandez diaz
    Nov 12, 2004
  2. Oh Kyu Yoon

    Newbie question about memory management

    Oh Kyu Yoon, Dec 30, 2003, in forum: Python
    Replies:
    3
    Views:
    263
    Emile van Sebille
    Jan 3, 2004
  3. Replies:
    4
    Views:
    283
    Mike Wahler
    Jan 2, 2007
  4. Ben V.
    Replies:
    1
    Views:
    113
  5. Jerry C.
    Replies:
    8
    Views:
    250
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page