Is it safe to return a struct in C or C++?

后端 未结 12 1886
北海茫月
北海茫月 2020-12-04 07:55

What I understand is that this shouldn\'t be done, but I believe I\'ve seen examples that do something like this (note code is not necessarily syntactically correct but the

12条回答
  •  庸人自扰
    2020-12-04 08:42

    Let's add a second part to the question: Does this vary by compiler?

    Indeed it does, as I discovered to my pain: http://sourceforge.net/p/mingw-w64/mailman/message/33176880/

    I was using gcc on win32 (MinGW) to call COM interfaces that returned structs. Turns out that MS does it differently to GNU and so my (gcc) program crashed with a smashed stack.

    It could be that MS might have the higher ground here - but all I care about is ABI compatibility between MS and GNU for building on Windows.

    If it does, then what is the behavior for the latest versions of compilers for desktops: gcc, g++ and Visual Studio

    You can find some messages on a Wine mailing list about how MS seems to do it.

提交回复
热议问题