Thread: Stack underflow

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    Stack underflow

    Hi, I'm trying to get some insight on what could be causing a stack underflow error. I have some code that looks like

    Code:
    void * func1(a_struct s)
    {
    
     .....
    
     printf("s = 0x%p.\n", s);
     printf("&s = 0x%p.\n", &s);
    
     foo = func2(s);
    
     .....
    
     return foo;
    }
    
    void * func2(a_struct s)
    {
     printf("s = 0x%p.\n", s);
     printf("&s = 0x%p.\n", &s);
    
     ......
    
    return foo;
    }
    func1 prints out 's' just fine but in func2 I get a stack underflow when trying to print s or &s. I can tell it's an underflow from looking at the assembly. A store instruction is getting called to store a register val just before the start of the stack. This happens with any kind of reference to 's' in func2.

    Anyone have any idea what could be going on?
    Last edited by homer_3; 09-02-2009 at 08:18 AM. Reason: typo

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You've removed any code that could be affecting the values.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to post exact code which produces the problem (not something full of .... which has in all likelyhood snipped the problem).

    You also need to say which OS/Compiler you're using, and be specific (not something vague like Microsoft/Microsoft)
    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.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    printf("s = 0x%p.\n", s);
    printf("&s = 0x%p.\n", &s);
    %p expects a void*, but you pass it a struct and a pointer to struct. the pointer to struct might not cause problems (but use (void*)&s just to be sure), but the struct probably would. the variable arguments might be being pushed onto the stack and then maybe the stack's getting messed up because printf doesn't know what's passed to it. guessing.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well since they cut out all useful code a_struct could very well be a void *

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    yeah, but then &s would be a void**

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM