Print Page | Close Window

Stack usage on recursive call

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: General Discussion
Forum Description: Topics Related to Visual C++ MFC Development in General
URL: http://forum.codejock.com/forum_posts.asp?TID=15746
Printed Date: 21 May 2024 at 5:50am
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: Stack usage on recursive call
Posted By: znakeeye
Subject: Stack usage on recursive call
Date 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! :(
 
Sample program: http://forum.codejock.com/uploads/20091203_064021_StackBomb.zip - uploads/20091203_064021_StackBomb.zip



Replies:
Posted By: mgampi
Date Posted: 30 November 2009 at 4:44am
Additional stack checking code???

-------------
Martin

Product: Xtreme Toolkit v 19.0.0, new Projects v 19.1.0
Platform: Windows 10 v 1909 (64bit)
Language: VC++ 2017


Posted By: znakeeye
Date 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.


Posted By: znakeeye
Date Posted: 03 December 2009 at 6:41am
Please see attached sample. It demonstrates this.
 
It's totally sick! :(


Posted By: jimmy
Date 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



Posted By: znakeeye
Date 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?


Posted By: jimmy
Date 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





Posted By: jimmy
Date Posted: 03 December 2009 at 10:23am
See also
#pragma check_stack([ {on | off}] )
#pragma check_stack{+ | –}

  Jimmy



Posted By: znakeeye
Date Posted: 04 December 2009 at 5:35am
Thanks, but these have no effect in debug-mode, as it seems :/



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net