r/shittyprogramming • • Feb 19 '15

TIL that the string "Segmentation fault" is located at the very beginning of memory in C

Try it for yourself:

#include <stdio.h>

int main() {
    char* myString = 0;
    printf("%s\n", myString);
}
448 Upvotes

27 comments sorted by

183

u/Bone008 Feb 20 '15

I tried changing the value around and it seems that "Segmentation fault" is actually stored in many places in memory.

Just think of all the additional RAM we could have if we could use all that memory for something useful instead. I mean, why does that string have to be everywhere?

I swear, it's a conspiracy of all the websites that sell more downloadable RAM so they can make more money. We have been tricked all this time!

43

u/supergnawer Feb 20 '15

It's still usable. Whenever you need strings like "segment", or "fault", or "segmentation fault", you can reuse this string for no memory cost. Like, doing output at low-memory conditions.

Why it's in many places, is probably because many conditions cause this error, and it was important to keep it aligned to memory blocks to output effectively.

2

u/[deleted] Feb 22 '15

Well, you're close. But look at this:

#include <stdio.h>

int main(int argc, char ** argv) {
    char * msg = (void*) 1;
    printf("%c\n", msg);
    return 139;
}

You'll see that the value stored at most uninitialized memory locations is "zsh: segmentation fault ./progname".

In your code, changing the 0 to a 1 would print the same thing, which if it were a string would not be the case; It would print "egmentation fault" if you were right.

Thus we can only conclude that what appears to be a string is actually a single char value.

3

u/fwilson42 Feb 22 '15

No, the string "Segmentation fault" can only be printed as a string. Printing it as a character will cause the "segmentation fault" error. Note the difference in capitalization.

1

u/[deleted] Feb 22 '15

Oh weird, that must be a windows thing. I'll have to test this further.

Thanks for the tip

36

u/JustGetMeIn Feb 20 '15

Guys, this is awesome! Try to print byte 1 and it will also print Segmentation fault. Does anyone have a link for this new compression method (a whole sentence in one byte)? I could use this at work.

14

u/brunokim Feb 20 '15

It's a common compression trick, the OS actually maps several bytes to this string. An easy way to make the OS do that for you is storing the data in the filename and putting a single byte in the file. Which byte is up to you, though.

16

u/TheSlimyDog Feb 20 '15

For a while, I though this was how the computer actually handled this stuff and it made absolutely no sense. You do good /r/shittyprogramming.

25

u/saltyboyscouts Feb 20 '15

On clang I actually get "(null)"

2

u/redditthinks Feb 20 '15

Same on Pelles C. Interestingly, accessing address 1 causes a crash.

1

u/[deleted] May 05 '15

I get the same with gcc.

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix

11

u/cdvma Feb 19 '15

Hahahaha brilliant!

11

u/paincoats Feb 20 '15 edited Feb 20 '15

guys help i cant run it

~% cat < reddit.sh | sh
sh: line 3: syntax error near unexpected token `('
sh: line 3: `int main() {'

edit: oh wait got it

[1]    9469 segmentation fault    cat

28

u/[deleted] Feb 20 '15

lmao took me longer than it should have

10

u/PM_ME_YOUR_SUBARU Feb 20 '15

Huh, same. I didn't see the subreddit name and I was trying to figure out why this would be useful. NULL = 0 so trying to print a something that's null...

2

u/xiipaoc Feb 20 '15

Try /r/ProgrammerHumor. I just cracked up for, like, a whole minute!

4

u/IvantheDugtrio Feb 20 '15

lol Cygwin with GCC doesn't return any errors.

16

u/Fsmv Feb 20 '15

You have to run it. Remember, this isn't an error, its just showing what's at address 0.

3

u/IvantheDugtrio Feb 20 '15

I did run it and it printed nothing followed by a newline.

7

u/brunokim Feb 20 '15

Add some comments, sometimes this fixes the problem.

3

u/fenek212 Feb 20 '15

Are you sure you made it char* instead of char?

2

u/IvantheDugtrio Feb 20 '15

Yeah I did. I copied the code, added a comment, still nothing when I run it. It could be a protective measure in Cygwin.

2

u/until0 Feb 20 '15

Did you try to ask it nicely and run it again? I'd be very surprised if that doesn't actually fix your issue.

1

u/Kai-Isakaru Apr 10 '15

Holy crap I'm stupid.

0

u/[deleted] Feb 20 '15

[deleted]

49

u/fwilson42 Feb 20 '15

Thanks for reminding me, I do need to change my headlight fluid!

3

u/scorcher24 Feb 20 '15

This this one. I love this brand.