Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie Question about Memory in C++

Reply
Thread Tools

Newbie Question about Memory in C++

 
 
adsci
Guest
Posts: n/a
 
      01-26-2007
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
 
Reply With Quote
 
 
 
 
Scott McPhillips [MVP]
Guest
Posts: n/a
 
      01-27-2007
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]

 
Reply With Quote
 
 
 
 
Dave Rahardja
Guest
Posts: n/a
 
      01-27-2007
On Fri, 26 Jan 2007 14:48:22 +0100, adsci <(E-Mail Removed)> 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.
 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      01-28-2007


On Jan 26, 4:48 pm, adsci <(E-Mail Removed)> 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.

 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-29-2007
On Jan 28, 10:43 am, "terminator" <(E-Mail Removed)> wrote:
> On Jan 26, 4:48 pm, adsci <(E-Mail Removed)> 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

 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      01-29-2007


On Jan 29, 10:59 am, "Erik Wikström" <(E-Mail Removed)>
wrote:
> On Jan 28, 10:43 am, "terminator" <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On Jan 26, 4:48 pm, adsci <(E-Mail Removed)> 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.

 
Reply With Quote
 
bjeremy
Guest
Posts: n/a
 
      01-29-2007


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

 
Reply With Quote
 
adsci
Guest
Posts: n/a
 
      01-29-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
Newbie question: Array memory address jose luis fernandez diaz C++ 10 11-12-2004 08:37 PM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM
Newbie Memory question... whoamI Digital Photography 0 11-16-2003 12:49 AM
Newbie router memory question FranticInFresno Cisco 1 07-23-2003 04:29 AM



Advertisments