Thread: pushing of arguments into the stack?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    pushing of arguments into the stack?

    Code:
    int main()
    {
     int l =8;
    
     printf("%d\n%d\n",l,l++);
    
     return 0;
    }
    Output received
    A8 // But why.....
    B8

    1. how are the arguments pushed to the stack ? isn't this right to left?

    if its right to left then output A should be 9 right?

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    It's not necessary that the arguments will always be passed from right to left. It's compiler specific. So the result is actually undefined(vary from compiler to compiler).
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Quote Originally Posted by BEN10 View Post
    It's not necessary that the arguments will always be passed from right to left. It's compiler specific. So the result is actually undefined(vary from compiler to compiler).
    With an compiler that pushes the argument right to left, shouldn't the output be 9 and 8.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The result of l++ is 8.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    My question is why the operation l++ is not executed?

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sanddune008 View Post
    With an compiler that pushes the argument right to left, shouldn't the output be 9 and 8.
    Yeah. For such a compiler l++ will be 8 and then l incremented to 9.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sanddune008 View Post
    My question is why the operation l++ is not executed?
    What are you talking about? I compiled your code and it produces the correct output(as it is passing arguments right to left).
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> With an compiler that pushes the argument right to left, shouldn't the output be 9 and 8.

    That's why is called 'undefined behavior'. Consider this bit of code:

    Code:
    foo( bar++ );
    The compiler could break this down into:

    Code:
    push bar
    call foo
    increment bar
    - or perhaps -

    Code:
    push bar
    increment bar
    call foo
    Naturally then, had the code been:

    Code:
    foo( bar, bar++, bar );
    The compiler may well generate:

    Code:
    push bar
    push bar
    push bar
    call foo
    increment bar
    So in other words, it's all very unpredictable.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Quote Originally Posted by BEN10 View Post
    What are you talking about? I compiled your code and it produces the correct output(as it is passing arguments right to left).
    nope....i getting output as 8 and 8

    besides i am using Visual studio 6.0

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    thanks....Sebastiani

  11. #11
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Compile with warnings.

    In this case, the only thing that is guaranteed is that l will be incremented before entering the body of printf(), but parameter values the function is called with are undefined.

  12. #12
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    It is not advisable to increment or decrement the same variable in a single statement. Your title of this was a bit off key as well.

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> In this case, the only thing that is guaranteed is that l will be incremented before entering the body of printf()

    No. There is no guarantee here whatsoever.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sebastiani
    No. There is no guarantee here whatsoever.
    I think msh is correct: there is a sequence point before control enters the function, so it should be guaranteed "that l will be incremented before entering the body of printf()".

    EDIT:
    Ah, but I see that that is a moot point when in combination with the fact that there is undefined behaviour - "parameter values the function is called with are undefined" also means that l can have any value.
    Last edited by laserlight; 07-08-2009 at 08:03 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I think msh is correct: there is a sequence point before control enters the function, so it should be guaranteed "that l will be incremented before entering the body of printf()".

    Ok, I see what you mean now. The variable is first copied onto the stack, then incremented, and finally the function executes. Thanks for clarifying that.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM