Newbie Question about Memory in C++

A

adsci

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
 
S

Scott McPhillips [MVP]

adsci said:
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).
 
D

Dave Rahardja

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

terminator

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

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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

terminator

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.

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

bjeremy

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

adsci

adsci said:
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.
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top