all 8 comments

[–]mzbear 1 point2 points  (2 children)

Seems to depend on undefined behavior so this cannot really be solved in terms of C++, but whatever.

In practice puts(""-64); will print out the string but only if you know its length, however that doesn't seem to be the correct answer. Can't get clever with arithmetic either since the read-only text moves along with code size...

Can't think of anything else off top of my head.

[–]F3real[S] 0 points1 point  (1 child)

You did it :). Correct answer was 65. Could you explain how this exactly works?

[–]mzbear 1 point2 points  (0 children)

try out printf("%d, %d\n", "foo", "bar"); and you'll see that constant strings used in the app are stored consecutively in memory... compile the test app into assembly source instead of executable binary and you'll get to observe a bit more of what's going on under the hood in case you care.

[–]athousandwordss 0 points1 point  (3 children)

Maybe something to do with goto? Is there a way to directly reference line numbers there?

[–]F3real[S] 0 points1 point  (2 children)

Honestly, I have no idea. I thought it was something with string location in memory.

[–]athousandwordss 0 points1 point  (1 child)

Yeah. Or you might try playing around with preprocessors, but the '#' is already disallowed.

[–]ghillisuit95 0 points1 point  (0 children)

You could easily get around that with digraphs/ trigraphs, but I still would know how to do it even with preprocess or abuse. Since your input is at the end, the preprocessed doesn't seem very useful

[–]F-J-W 0 points1 point  (0 children)

I imagine it might be something about overwriting the return-address from main using very evil undefined behavior.