Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does assembly code ignore const keyword?

Does assembly code when used(linked) in a c project ignore const keyword declared before C datatypes and functions?? And can it modify the contents of the datatypes??

like image 344
annunarcist Avatar asked Mar 09 '26 00:03

annunarcist


2 Answers

Does assembly code when used(linked) in a c project ignore const keyword declared before C datatypes and functions??

Yes, the const keyword is utterly ignored by assembly code.

And can it modify the contents of the datatypes??

If the compiler was able to place a const location in a read-only segment, then assembly code trying to modify it will cause a segmentation fault. Otherwise, unpredictable behavior will result, because the compiler may have optimized parts of the code, but not others, under the assumption that const locations were not modified.

like image 118
Pascal Cuoq Avatar answered Mar 11 '26 12:03

Pascal Cuoq


And can it modify the contents of the datatypes??

Maybe, maybe not. If the original object was declared const then the compiler might emit it into a read-only data segment, which would be loaded into a read-only memory page at runtime. Writing to that page, even from assembly, would trigger a runtime exception (access violation or segmentation fault).

You won't receive a compile-time error, but at runtime your program might crash or behave erratically.

like image 41
cdhowie Avatar answered Mar 11 '26 13:03

cdhowie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!