Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > General Discussion
  New Posts New Posts RSS Feed - Stack usage on recursive call
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Stack usage on recursive call

 Post Reply Post Reply
Author
Message
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post Topic: Stack usage on recursive call
    Posted: 29 November 2009 at 5:08pm
VS 2008 SP1.

Consider this function:

void foo(int x)
{
    foo(x + 1);
}
 
If I add a parameter "int y", I would expect the stack usage to increase by 4 bytes. However, in my case (in debug mode) it increases with 72 bytes.
 
Can somebody please explain this? 68 extra bytes is a LOT! :(
 
Back to Top
mgampi View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14 July 2003
Status: Offline
Points: 1201
Post Options Post Options   Thanks (0) Thanks(0)   Quote mgampi Quote  Post ReplyReply Direct Link To This Post Posted: 30 November 2009 at 4:44am
Additional stack checking code???
Martin

Product: Xtreme Toolkit v 22.1.0, new Projects v 24.0.0
Platform: Windows 10 v 22H2 (64bit)
Language: VC++ 2022
Back to Top
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post Posted: 01 December 2009 at 6:19am
Sure, but 68 bytes?! It makes it hard to debug my recursive function, since it blows the stack. 6 parameters yields 432 bytes, lol...
 
In Release-mode the recursive call is optimized away completely, so there is no need in rewriting my algorithm.
Back to Top
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post Posted: 03 December 2009 at 6:41am
Please see attached sample. It demonstrates this.
 
It's totally sick! :(
Back to Top
jimmy View Drop Down
Senior Member
Senior Member


Joined: 11 November 2003
Location: Austria
Status: Offline
Points: 515
Post Options Post Options   Thanks (0) Thanks(0)   Quote jimmy Quote  Post ReplyReply Direct Link To This Post Posted: 03 December 2009 at 8:10am
Hi,

Every parameter need 4 Byte.
CALL need 4 Byte.
Save EBP need 4 Byte.
Save some other register need 3*4 Byte.

"Full runtime check" need 160 Byte. You can disable it.

if i have only 1 parameter it need 108 Byte.

i calculate
4 Byte        parameter
4 Byte        call
4 Byte        save ebp
4 byte        save this pointer
8 byte        for local variables
---------
24 Byte       

108 - 24 = 84 Byte too much ???
==================
i don't know more.

  Jimmy

Back to Top
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post Posted: 03 December 2009 at 9:09am

I think I'll disable runtime check in this particular cpp file. In optimized release-mode the stack usage is 0 bytes (!).

Do you know a #pragma to disable runtime check?
Back to Top
jimmy View Drop Down
Senior Member
Senior Member


Joined: 11 November 2003
Location: Austria
Status: Offline
Points: 515
Post Options Post Options   Thanks (0) Thanks(0)   Quote jimmy Quote  Post ReplyReply Direct Link To This Post Posted: 03 December 2009 at 10:17am
Hi,

Start Visual Studio, press F1 search #pragma , see runtime_checks and click it.
Than you see this
#pragma runtime_checks( "", off )
.
.
.
#pragma runtime_checks( "", restore )

Jimmy



Back to Top
jimmy View Drop Down
Senior Member
Senior Member


Joined: 11 November 2003
Location: Austria
Status: Offline
Points: 515
Post Options Post Options   Thanks (0) Thanks(0)   Quote jimmy Quote  Post ReplyReply Direct Link To This Post Posted: 03 December 2009 at 10:23am
See also
#pragma check_stack([ {on | off}] )
#pragma check_stack{+ | –}

  Jimmy

Back to Top
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post Posted: 04 December 2009 at 5:35am
Thanks, but these have no effect in debug-mode, as it seems :/
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.125 seconds.