Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22240

inline assembly in C++ function with return statement

$
0
0

Consider this code:

#include <cstdio>

int get_value() { asm("movl $254, %eax"); }

int main() { printf("%d\n", get_value()); }

Now if one compiles this code with g++ main.cpp, one gets a compiler warning (but the code still compiles):

main.cpp: In function ‘int get_value()’:
main.cpp:3:43: warning: no return statement in function returning non-void [-Wreturn-type]
    3 | int get_value() { asm("movl $254, %eax"); }
      |                                    

As this answer says that if a compiler generates a binary with the above code, all bets are off. (no return statement from a function with return type int)

Indeed, when one compiles this code with optimization turned on g++ -O3 main.cpp, this program immediately segfaults.

So my question is how can one return from inline assembly within a c++ function that is conformant with C++, and one doesn't get this warning, and the code works fine.


Viewing all articles
Browse latest Browse all 22240

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>