473,770 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

standard way to determine depth in stack

I can't see to easily find this on google or in a newsgroup

Is there a standard function/macro/whatever you can call and determine
the distance in a C program how deep one is in the C call stack from
main.

Something along the lines:

int main()
{
int i = stackdepth();
f2()
}

here i == 0.

void f2()
{
int i = stackdepth();
}

here i == 1

and so on.

I'm interested in nonstandard ways (if a standard way does not
suffice), or any crazy ideas.

Feb 21 '07
13 3843
Jacob navia wrote:
>
You can do it as follows:
2)
Now, you can write

long long stackdepth(void )
{
int dummy;
long long top = (long long)stacktop;
long long bottom = (long long)&dummy;
Ignoring the unnecessary conversion, this will give weird results on a
64 bit system if any of the pointer bit patterns represent negative long
longs.
long long result = top - bottom;
if (result < 0)
result = -result;
return result;
}
One other thing I missed the first time: this will go horribly wrong in
a threaded application where each thread uses its own stack - a classic
case of subtracting two unrelated pointers giving undefined behaviour.

--
Ian Collins.
Feb 22 '07 #11
You can do it as follows:
1) In the main function, get the address of the first local variable:

void *stacktop;

int main(void)
{
int dummy;

stacktop = (void *)&dummy;
.....

}

2)
Now, you can write

long long stackdepth(void )
{
int dummy;
long long top = (long long)stacktop;
long long bottom = (long long)&dummy;

long long result = top - bottom;
if (result < 0)
result = -result;
return result;

}
Why would an implementation that does use a stack need to store the
first local variable of the current function on the top of that stack?
That seems to me to be a pretty big assumption in your code that you
failed to mention to the OP. While that may be the case in your
environment there is certainly no reason that it has to be implemented
in such a way as long as I can access the variable while it is in
scope I could just as easily put it last, or anywhere else I want for
that matter.
>
3)
Please ignore all stupid remarks like "this is not portable".
This is obviously not portable to any machine. It requires a
stack, and it supposes that an address fits in a long long.
....and that long long exists
4) Please ignore all stupid remarks like " this is undefined behavior
since you can't take the difference of two pointers that do not point to
the same object"
While he is at it why doesn't he ignore stupid responses to his
question that don't even answer what he was asking. While, in some
cases, this may give you the depth in bytes (or even possibly some
other unknown unit) it does not tell him in the units he asked about
pretty unabiguously when he stated "here i == 1."
Feb 22 '07 #12
On Feb 22, 11:48 am, "softwared...@g mail.com" <softwared...@g mail.com>
wrote:
>
Is there a standard function/macro/whatever you can call and determine
the distance in a C program how deep one is in the C call stack from
main.
You could put:

#define ENTERING_FUNCTI ON() (++depth)
#define RETURN(x) do { --depth; return x; } while(0)

size_t depth = 0;

and then write all your functions like:

void func()
{
ENTERING_FUNCTI ON();
foo, bar;
RETURN();
}

and then inspect the 'depth' variable when you need to.
This has added benefits, eg. you could define the macros
to write a complete call trace to a log file when needed,
etc. (Obviously it has some down-sides).

Feb 22 '07 #13
"so**********@g mail.com" wrote:
>
I can't see to easily find this on google or in a newsgroup

Is there a standard function/macro/whatever you can call and
determine the distance in a C program how deep one is in the C
call stack from main.
Certainly, and you can do it within purely standard C.

/* main.h */
extrn int stackdepth;
/* end main.h */

/* main.c */
#includes /* as needed */
#include main.h

int stackdepth;

int main(void) {
...
stackdepth++;
functioncall(pa rams);
stackdepth--
...
return 0;
}

Now if you use the above paradigm for all function calls, in all
modules, stackdepth will keeptrack of the nesting level. All
modules need to include main.h. Don't use longjump.

You might want to macroize the stackdepth++ and stackdepth--
operations, so you can easily eliminate them later without editing.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Feb 22 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

48
2736
by: Michael Sig Birkmose | last post by:
Hi everyone! Does anyone know, if it is possible to meassure the maximum stack usage of a C program throughout it's entire execution? -- Michael Birkmose
20
3503
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate "heap" or "stack" I'm curious to know the implemenations which dont have stack or heap.
13
25561
by: Ben R. Bolton | last post by:
The documentation indicates that the threads "default stack size" is 1MB. The work "default" implies that it can be changed. Is it possible to change the StackSize in .NET? If so how? Is it possible to determine the amount of memory used in the current stack? Any assistance would be appreciated. Ben
2
1756
by: Raider | last post by:
Operator new() must throw std::bad_alloc in no more dynamical memory left, but what standard says about stack? Is there a some way to handle stack overflow? Raider
6
2646
by: Daz | last post by:
Hi everyone! It is indeed, once again time for me to ask another stupid question. I have been searching around on the net for quite a while, and can't find anything that explains 'exactly' what a stack overflow is, or how it's detected. I know that it's leterally putting too many objects on the stack, but how many objects can you have on the stack, or do OSs only allow a certain number of recursive calls to functions?
7
2082
by: David T. Ashley | last post by:
Is there any portable method for identifying the caller of a function, its caller, and so on, at runtime, without using a debugger? Clearly, one can dissect the stack frame and trace it back. But this method isn't very portable (it requires a knowledge of the machine). Is there any method supported by the language or by standards? (Maybe, similar to the way variable-length argument lists are handled?)
22
2300
by: Chris Thomasson | last post by:
I am thinking about using this technique for all the "local" memory pools in a paticular multi-threaded allocator algorithm I invented. Some more info on that can be found here: http://groups.google.com/group/comp.arch/browse_frm/thread/24c40d42a04ee855 Anyway, here is the code snippet: #include <cstdio> #include <cstddef>
4
6355
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
Visual Studio 2005, C# WinForms application: Here’s the question: How can I increase the standard 1 MB stack size of the UI thread in a C# WinForms application? Here’s why I ask: I’ve inherited some code that at the view (User Interface) layer kicks off a background worker thread. At the service layer (think CAB service layer), there’s quite a lot of the following:
87
5569
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first place? I can see two definite disadvantages with this: 1) deeply nested recursive calls to a function (especially if it defines
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.