Thread: the order in which function arguments are put onto stack

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    the order in which function arguments are put onto stack

    Promble solved! Thanks to salem!

    I wrote the following code to test the order in which function arguments are put onto the stack, and I got wired result:
    Code:
    void print_char(char c1, char c2)
    {
            printf("%p %p\n", &c1, &c2);
    }
    
    void print_float(float f1, float f2)
    {
            printf("%p %p\n", &f1, &f2);
    }
    
    int main(int argc, char **argv)
    {
            print_char('c', 'd');
            print_float(1.2, 2.3);
    
            return 0;
    }
    the result is:
    0xbfcafef4 0xbfcafef0
    0xbfcaff00 0xbfcaff04
    which means for function print_char, the first argument is put onto the stack first, while for function print_float, the second argument is put first, why??

    The code is run on 32-bit linux.
    Last edited by crs_zxf; 08-22-2009 at 08:37 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I get the same kind of result.

    From looking at the assembler, chars are first copied to internal local variables, and what you see are pointers to those hidden locals, not pointers to the actual parameters.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    Quote Originally Posted by Salem View Post
    I get the same kind of result.

    From looking at the assembler, chars are first copied to internal local variables, and what you see are pointers to those hidden locals, not pointers to the actual parameters.
    You're right, thank you so much, you remind me of a very good way to solve this problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM