Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-06-2008, 03:25 PM
restor
 
Posts: n/a
Stack Overflow

Hi,
How does C++ address the issue of the stack overflow?
It seams that the following function counting the depth of a binary
tree:

size_t depth( Node * node ) {
if( node ) {
size_t l = depth( node->left );
size_t r = depth( node->right );
return 1 + max( l, r );
}
else {
return 0;
}
}

may cause an undefined behavior (and most likely crash in this case)
even though, the tree data was correct, and data fit into the system's
memory, but the stack couldn't fit all the local variables, that were
put there recursively during the algorithm execution.

Can the behavior of any program be undefined because we never know if
we caused the stack overflow or not?

Regards,
&rzej

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #2 (permalink)  
Old 08-06-2008, 08:58 PM
Razvan Cojocaru
 
Posts: n/a
Re: Stack Overflow

> How does C++ address the issue of the stack overflow?

It doesn't.


> Can the behavior of any program be undefined because we never know if
> we caused the stack overflow or not?


Most certainly.


--
Razvan Cojocaru
KeyID: 1024D/04CA34DE

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #3 (permalink)  
Old 08-06-2008, 08:58 PM
Jiang
 
Posts: n/a
Re: Stack Overflow

On Aug 6, 11:25 pm, restor <akrze...@interia.pl> wrote:
> Hi,
> How does C++ address the issue of the stack overflow?


C++ does not address the issue of stack allocation and
overflow, since C++ does not know what is stack.

It is quite common that most of the implementations
will use stack as automatic storage, but C++ does/can
not say more about this, because we do not have an
standardized C++ ABI.

> It seams that the following function counting the depth of a binary
> tree:
>
> size_t depth( Node * node ) {
> if( node ) {
> size_t l = depth( node->left );
> size_t r = depth( node->right );
> return 1 + max( l, r );
> }
> else {
> return 0;
> }
>
> }
>
> may cause an undefined behavior (and most likely crash in this case)
> even though, the tree data was correct, and data fit into the system's
> memory, but the stack couldn't fit all the local variables, that were
> put there recursively during the algorithm execution.



The above code is well-formed according to the C++ standard.
But since the resource for underlying platform is limited,
a well-formed program can crash.

The C++ standard committee added Annex B for this kind of QoI
(Quality of Implementation) issues. However, the quantities
listed are only guidelines and do not determine compliance
at all.

> Can the behavior of any program be undefined because we never know if
> we caused the stack overflow or not?


Since every implementation should document the minimal
size for automatic storage, you should be able to calculate
when the stack overflow occurs.

BTW, for the following popular implementations:

* MS visual C++: check /F (Set Stack Size) option.
* GCC: g++ -Wl,--stack,stacksize -o app.exe app.cpp
(replace stacksize with your desired size)

HTH

Jiang


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #4 (permalink)  
Old 08-06-2008, 11:41 PM
restor
 
Posts: n/a
Re: Stack Overflow

> C++ does not address the issue of stack allocation and
> overflow, since C++ does not know what is stack.


You are right. I didn't realize this, but indeed the standard doesn't
define the notion of stack. What is interesting is that it _does_
define stack unwinding.

> The C++ standard committee added Annex B for this kind of QoI
> (Quality of Implementation) issues. However, the quantities
> listed are only guidelines and do not determine compliance
> at all.


> Since every implementation should document the minimal
> size for automatic storage, you should be able to calculate
> when the stack overflow occurs.


But the standard doesn't list anything like "minimal size for
automatic storage" in Appendix B. Does it mean a standard-conformant
compiler may not give me this number?

I never considered this problem when writing code in my work, but I
wonder how the state-of-the-art implementations of STL algorithms
handle the problem of potential shortage of automatic storage.

Regards,
&rzej

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #5 (permalink)  
Old 08-07-2008, 11:55 AM
Pete Becker
 
Posts: n/a
Re: Stack Overflow

On 2008-08-06 12:41:28 -0400, restor <akrzemi1@interia.pl> said:

>> C++ does not address the issue of stack allocation and
>> overflow, since C++ does not know what is stack.

>
> You are right. I didn't realize this, but indeed the standard doesn't
> define the notion of stack. What is interesting is that it _does_
> define stack unwinding.


Yes, but that's unwinding the call stack. That's an abstract notion,
distinct from the stack where auto objects are usually allocated.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #6 (permalink)  
Old 08-07-2008, 11:55 AM
Mathias Gaunard
 
Posts: n/a
Re: Stack Overflow

On 6 août, 16:25, restor <akrze...@interia.pl> wrote:
> Hi,
> How does C++ address the issue of the stack overflow?


It doesn't.
An OS like Linux handles this on a per-process policy, I think, with
per-user max limits. Thanks to the MMU, it can actually grow as
needed.
When the limit is reached, a signal is sent. The default is no limit,
but that's not the default in most linux distributions.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #7 (permalink)  
Old 08-07-2008, 12:02 PM
Jiang
 
Posts: n/a
Re: Stack Overflow

On Aug 7, 7:41 am, restor <akrze...@interia.pl> wrote:

> I never considered this problem when writing code in my work, but I
> wonder how the state-of-the-art implementations of STL algorithms
> handle the problem of potential shortage of automatic storage.



Good point, but unfortunately I do not know STL's design policies for
this issue. Maybe P.J. Plauger or Pete Becker can give you the points
if they want.

But generally speaking, the problem can be prevented systematically.
For your above recursive example, you can

1. Estimate your object size and calculate the maximal recursive
count. If it does not meet your needs, try to specify a new stack
size.

2. Also you can set a maximal count for your default stack size.

3. Or, replace auto variables with dynamical/pool allocated objects.

4. Remove the recursion from your program. Recursion is elegant
if used judiciously, however, usually we do not need it at all.


Regards,

Jiang


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #8 (permalink)  
Old 08-07-2008, 12:02 PM
Jiang
 
Posts: n/a
Re: Stack Overflow

On Aug 7, 7:41 am, restor <akrze...@interia.pl> wrote:
> > C++ does not address the issue of stack allocation and
> > overflow, since C++ does not know what is stack.

>


[...]

> What is interesting is that it _does_ define stack unwinding.
>


Please note here "stack unwinding" means (15.2/p3):

The process of calling destructors for automatic objects
constructed on the path from a try block to a
throw-expression is called “stack unwinding.”

It shows me that just like std::stack, it has nothing to do with
the implementation's stack storage.
The term "stack unwinding" is not separatable.

Regards,

Jiang


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #9 (permalink)  
Old 08-07-2008, 04:58 PM
restor
 
Posts: n/a
Re: Stack Overflow

> But generally speaking, the problem can be prevented systematically.
> For your above recursive example, you can


I have just realized that it is not the recursion that is the root of
the problem.
The following function may also trigger the overflow of automatic
storage:

char encryptChar( char ch ){
char ans = ch << 2;
return ans;
}

Because automatic storage resource was already full and the tiny char
just made it.

Regards,
&rzej

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #10 (permalink)  
Old 08-07-2008, 04:58 PM
Pete Becker
 
Posts: n/a
Re: Stack Overflow

On 2008-08-07 01:02:28 -0400, Jiang <goo.mail01@yahoo.com> said:

> On Aug 7, 7:41 am, restor <akrze...@interia.pl> wrote:
>
>> I never considered this problem when writing code in my work, but I
>> wonder how the state-of-the-art implementations of STL algorithms
>> handle the problem of potential shortage of automatic storage.

>
>
> Good point, but unfortunately I do not know STL's design policies for
> this issue. Maybe P.J. Plauger or Pete Becker can give you the points
> if they want.


Not much to say. Calling a function uses stack space, and if you run
out of stack, your program crashes. So don't do that. Library code in
general doesn't try to check available stack space.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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



All times are GMT. The time now is 12:14 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Free Advertising | Personal Loans | WoW Gold | Credit Cards | Mortgage



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114