CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2014
    Posts
    2

    Exclamation Strange compiler behavior

    Hi Everybody!
    I would be most grateful for the explanation of a very strange compiler behavior I experienced.

    This is what I basically had in my code:

    class Facade
    {
    public:
    // some static method used to traverse over a static collection attribute
    static CollectionElement* getByProperty(/* … */)
    {
    CollectionElement* elem = 0; // default return value
    // Loop through the collection of elements in order to return a specific element
    // …
    // Return the element when you find it!
    //return elem; // <---- this is the line of code I initially omit to write!

    }
    private:
    static Collection myCollection; // some static collection attribute
    };
    So I forgot to return a pointer to some collection element from my static method (I did have a warning)!
    Then I accessed the returned pointer from the calling code and realized that not only did not my application crash but also the pointer was pointing to the valid data obtained whilst looping through the collection !!!

    Can anyone explain how this was possible?

    Best Regards,
    Branko

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Strange compiler behavior

    Quote Originally Posted by brankok View Post
    Then I accessed the returned pointer from the calling code and realized that not only did not my application crash but also the pointer was pointing to the valid data obtained whilst looping through the collection !!!

    Can anyone explain how this was possible?
    I'm pretty sure that not returning a value from a function (other than main) that has a return value is undefined behavior.
    Probably the compiler created the machine code in such a way that the stack location that holds the function's return value was used in the function to store the element pointer in the loop.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Mar 2014
    Posts
    2

    Re: Strange compiler behavior

    That’s the only reasonable explanation, I know, but still strange to me, I didn’t expect that the same stack location holds the function’s return value and the local variable.

  4. #4
    Join Date
    Jul 2013
    Posts
    576

    Re: Strange compiler behavior

    Quote Originally Posted by brankok View Post
    That’s the only reasonable explanation, I know, but still strange to me, I didn’t expect that the same stack location holds the function’s return value and the local variable.
    For performance reasons compilers often try to keep variables in registers as much as possible and also use registers to pass function return values back to the calling code.

    The function returns a CollectionElement* pointer so its not unlikely the compiler decides to hold elem in the return register. Especially if elem is the only local variable of the CollectionElement* type it's a very likely return candidate. It's rational to keep it in the return register all the time because it saves an assignment upon return. With this scenario the return statement wouldn't add any code even if it were present - and that's why it works without it.

    You can find out what actually happens by looking at the compiler assembly output.
    Last edited by razzle; March 24th, 2014 at 11:05 PM.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Strange compiler behavior

    If you write a bug, then "anything could happen"... including getting the exact behaviour you were expecting/intending to get.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured