Is behavior of += intentional for int?

C

Carl Banks

Yes, indeed, and it's quite possible to write code like this in Fortran
that produces exactly this result -- an integer constant gets modified.

This used to get big yucks when we gathered around the keypunch, before we
had the Obfuscated C Contest to amuse us.

Many Fortran compilers these days pass scalars by value by default.

Recently I had to compile a cicra-1972 Fortran program where I had to
disable a lot of recent Fortran innovations (like from 1977) to get it
to build properly. It turned out that it did try to modify a constant
somewhere, but then it segfaulted because the compiler put that
constant in read-only memory.

(Whoever wrote the program originally was a smart cookie, BTW, it was
super versatile for its time.)


Carl Banks
 
D

Dennis Lee Bieber

Yes, indeed, and it's quite possible to write code like this in Fortran
that produces exactly this result -- an integer constant gets modified.
Been a tad harder in the last quarter century... Most decent
compilers now put literals into a page of memory that is flagged as
read-only.

But more ancient compilers, yes...

program main
integer i
call mutant(1)
i = 1
print i
stop

subroutine mutant(j)
integer j
j = j + 1
return
end

Will happily change the "constant" 1 into a 2, and the print
statement (granted, that ancient a compiler probably requires the
full-up write() with format) will display that "2"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top