Determine a function's stack size.

This is a discussion on Determine a function's stack size. within the C Programming forums, part of the General Programming Boards category; Hi, Once a function is called it's parameters, return address etc, are pushed onto the stack, Can we determine programatically ...

  1. #1
    root koodoo's Avatar
    Join Date
    Oct 2005
    Location
    a small village faraway in the mountains
    Posts
    28

    Determine a function's stack size.

    Hi,

    Once a function is called it's parameters, return address etc, are pushed onto the stack, Can we determine programatically how much stack space a function uses? i.e. by wiring some code in the called function, or the code that calls the function, and not using some profiling tool.

    Thanks,
    regards,
    koodoo.

  2. #2
    root koodoo's Avatar
    Join Date
    Oct 2005
    Location
    a small village faraway in the mountains
    Posts
    28
    I searched in this forum and found this thread Want a C Function to get current thread's stack size
    I then wrote the following code. It compiles/executes without error, but I don't know whether it gives me the correct value or not.
    Suppose there's a function "testfunc", and we wish to find how much stackspace this fuction uses. I've written the following code for this.

    Code:
    #include <stdio.h>
    #include <stddef.h>
    ptrdiff_t testfunc (int arg1, int arg2, char *stackbase); 
    
    int main()
    {
    
    
    char *stackbase;
    printf("\nThe amount of stack space used by \"testfunc\" is : %ul bytes\n",testfunc(10, 5, stackbase));
    
    return 0;
    }
    
    ptrdiff_t testfunc (int arg1, int arg2, char *stackbase)
    {
    //.
    //.
    //all function processing goes here
    //.
    //.
    //.
    //.
    char temp;
    return stackbase - &temp;
    }
    First thing is that, will this code give me the desired result?
    Secondly, what is the best way to print a ptrdiff_t entity. It is a typedef I think defined in stddef.h as
    typedef long ptrdiff_t;
    so currently I'm just printing it as an unsigned long integer.

    regards,
    koodoo

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Except you didn't follow the code very well....
    Read it again - note that stackbase is NOT passed by value.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    root koodoo's Avatar
    Join Date
    Oct 2005
    Location
    a small village faraway in the mountains
    Posts
    28
    I read it and I even noticed that stackbase wasn't passed by value. But still made a mistake. What I wanted was something like this.

    Code:
    #include <stdio.h>
    #include <stddef.h>
    ptrdiff_t testfunc (int arg1, int arg2, char *stackbase); 
    
    int main()
    {
    
    
    char stackbase;
    printf("\nThe amount of stack space used by \"testfunc\" is : %ul bytes\n",testfunc(10, 5, &stackbase));
    
    return 0;
    }
    
    ptrdiff_t testfunc (int arg1, int arg2, char *stackbase)
    {
    //.
    //.
    //all function processing goes here
    //.
    //.
    //.
    //.
    char temp;
    return stackbase - &temp;
    }
    Is this correct?
    If this helped you, please take the time to rate the value of this post by clicking here.

    Statutory Warning : Beware of C... it's highly addictive.

    --------------------------------------------------------------
    Registered Linux User #388419
    --------------------------------------------------------------

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Better, but you still need to call a function to work out how much space you needed.

    There is no relationship between the order in which you declare local variables, and the relative positions of them on the stack.

    Or there's this way
    Code:
    void foo ( int a ) {
        int c = 2;
        printf("hello &#37;d %d\n",a,c);
    }
    
    _foo:
    	pushl	%ebp
    	movl	%esp, %ebp
    	subl	$24, %esp
    	movl	$2, -4(%ebp)
    	movl	-4(%ebp), %eax
    	movl	%eax, 8(%esp)
    	movl	8(%ebp), %eax
    	movl	%eax, 4(%esp)
    	movl	$LC0, (%esp)
    	call	_printf
    	leave
    	ret
    Use "gcc -S" to compile your code, then look for the size adjustment for %esp.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    root koodoo's Avatar
    Join Date
    Oct 2005
    Location
    a small village faraway in the mountains
    Posts
    28
    Thanks, I'm still very new to understanding the disassembled output. But I'll study and try to understand it.
    Thanks for all the help
    If this helped you, please take the time to rate the value of this post by clicking here.

    Statutory Warning : Beware of C... it's highly addictive.

    --------------------------------------------------------------
    Registered Linux User #388419
    --------------------------------------------------------------

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Since the return value of testfunc() is nonnegative, you should be using size_t instead of ptrdiff_t. This is especially true since ptrdiff_t is not actually guaranteed to be able to hold the difference between two pointers (even though the name would lead you to think otherwise), but size_t is guaranteed to be able to hold the size of any object, including an array, so it is guaranteed to be able to hold a nonnegative difference between two pointers (to the same array).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stringy Sums
    By bumfluff in forum C++ Programming
    Replies: 14
    Last Post: 05-15-2006, 01:52 AM
  2. Stack Size
    By siavoshkc in forum C++ Programming
    Replies: 15
    Last Post: 03-01-2006, 10:39 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 10:27 AM
  4. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 01:56 AM
  5. HEap and stack, I'm confused
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-31-2002, 09:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21