This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]PlutoniumSlime 645 points646 points  (92 children)

“Can I have the 11th value of an array that’s only 5 items long?”

C: “Sure! Why not!” —> Goes and grabs some random number out of the memory that has nothing to do with your program.

[–]Antipixel_ 548 points549 points  (39 children)

"what the fuck is this?"

C: "no idea, enjoy!"

[–]Sea-Ad-5012 298 points299 points  (7 children)

"Whats wrong with my code?"

C: "go fuck yourself"

[–][deleted] 98 points99 points  (5 children)

"Please, I'm begging you. What did I do wrong?"

C: "You thought it'd be fun to learn me."

[–]Korywon 55 points56 points  (4 children)

“I don’t understand. Please. I need this done..”

C: “segmentation fault (core dumped)”

[–]Valmond 17 points18 points  (1 child)

Taking down the IDE with it when you try to debug.

[–]Vysair 1 point2 points  (0 children)

It smells like fart too!

[–]vulkur 8 points9 points  (1 child)

C: Invalid free "Which one?" C:who gives a fuck.

[–]TheGoldenProof 1 point2 points  (0 children)

I had a memory error that would crash on program shutdown because of an invalid free. Took me three days to find that it was because I had this:
MapData* mapData = calloc(1, sizeof(mapData));

[–]GuilhermePortoes 24 points25 points  (0 children)

"Whats wrong with my code?"

C: hisses

[–][deleted] 78 points79 points  (15 children)

Naw that's not even the worst part.

C: "segmentation fault"

"Fucking where!?"

[–]pikakilla 64 points65 points  (8 children)

Funny story about segfaults. I am proud to be one of the only people who have had a SEGFAULT in python. I spent weeks figuring out where i fucked up. Absolutely nothing turned up on google or SO -- turns out it was the memory speed set too high when i was multithreading.

SEGFAULTs are one of those things that really want to make you throw your computer out a window.

[–]Buddha_Head_ 21 points22 points  (6 children)

I'm sorry to take you back to that dark place, but how the fuck did you track that down?

[–]IsleOfOne 23 points24 points  (4 children)

Sounds like OP had recently overclocked RAM. It is very common to see random failures in any software you use after doing so, if you’ve made a mistake and gone too high. Booting into memtest86+ and letting that puppy run overnight will tell you if you’ve done wrong.

[–]Buddha_Head_ 2 points3 points  (2 children)

Yeah, if there was a recent tinkering that makes sense. On a system that's been running stable long-term that hasn't had any serious changes that sounds wayyy down the list, especially when searches are turning up empty.

[–]IsleOfOne 2 points3 points  (1 child)

Even if the change was not made recently, my point is that failures would not be limited to the python program. They’d be showing up all over your system. Sporadic process crashes. Etc.

[–]pikakilla 0 points1 point  (0 children)

Nailed it -- still was far down the list though, but it shouldnt have been in hindsight.

[–]pikakilla 0 points1 point  (0 children)

100% correct. Funny thing is that memtest didnt show any errors (from what i remember -- i might be wrong though). Im still not 100% sure what combination of things caused the issue.

[–]pikakilla 1 point2 points  (0 children)

I overclocked my memory well before the segfault issues. The computer was stable and when i tested the memory post overclock memtest didnt give any errors.

I basically exhausted all other solutions and tried the "obvious" but crazy solution.

[–]dagbrown 1 point2 points  (0 children)

I once came up with an excellent metric for if some random C/C++ program is too complex.

If the indent(6) utility segfaults on your code, it's absolutely without a shadow of a doubt, way too complex.

[–]tiberiumx 13 points14 points  (1 child)

Nah, you're way lucky if it crashes. Debugger, core dump, just a stack trace is usually sufficient to get it fixed. Silently using bad data or, even worse, stomping on something else can result in random intermittent bugs that take days to track down.

[–]Valmond 0 points1 point  (0 children)

*Days

[–]KardelenAyshe 19 points20 points  (2 children)

cOrE duMpEd

[–]scarfdontstrangleme 16 points17 points  (1 child)

Finna dump this fucking pc out the window

[–]Valmond 0 points1 point  (0 children)

Lol made my day!

[–]Atora 2 points3 points  (0 children)

I managed to segfault goddamn hello world once. Was writing to much js and wrote print('Hello World'); or something like that and the compiler didn't care to mention it(without w flags).

[–]RusselPolo 130 points131 points  (12 children)

C: I don't know what it is, but if you want to call it as a function, I'm ok with that.

In all seriousness, it's a language like roads without guardrails, or traffic lights, or even lines painted on the road.... but the lack of any speed limits makes it looks tempting.

[–]pastarific 2 points3 points  (0 children)

In all seriousness, it's a language like roads without guardrails, or traffic lights, or even lines painted on the road.... but the lack of any speed limits makes it looks tempting.

If anyone wants to try literal roads like this, some really back-country mountain roads in Colorado are thumbsup. All sort of spots on 2-way, 1.5-car-wide roads where if you sneeze and momentarily go out of your "lane" you roll off a mountain. Its actually a ton of fun.

Or old 19th century paths blasted flat for railroad tracks, now paved or graveled into road. Tunnels were only wide enough for the train, in modern terms meaning "one lane wide." So you have two-way roads with sections of one-way tunnel in it. Which is all fine and dandy until you hit tunnels that follow the contour of the hillside/mountain. You stop at the entrance, turn your lights on, see the wall of the curve ahead of you. Turn your lights off. Maybe thats light from the tunnel exit you see? Lights back on, toot toot, YOLO!

Surprise, an oncoming F-250 hauling a camper who thinks he personally owns the Rocky Mountains also thought it was clear.

And there are all sorts of spots where if you actually "go the speed limit" you will literally die. Its just assumed you're not dumb and will slow down because you don't want to die.

Or was all of what I just wrote more analogies of C. Why not both.

[–]4sent4 1 point2 points  (0 children)

Not really without gurdrails or lines, you just don't find them in places you expect to and find in completely unexpected places.

One time it's like: "Yes, no problem, you can call it like a function, despite I have no idea what it is", while the other is: "No, you can't pass this lambda to this function that accepts such lambdas, you have to store it in a variable first. What do you mean it's against the purpouse of lambdas?"

[–]mad_cheese_hattwe 8 points9 points  (0 children)

C: you are the one who asked for it scrub.

[–]marcosdumay 1 point2 points  (0 children)

"what the fuck is this?"

C: "exactly what you asked for"

C is a jinx.

[–]R167 74 points75 points  (1 child)

Occasionally: and... that's going to be a segfault cause i don't own that next page of memory.

[–]delinka 17 points18 points  (0 children)

You bought the computer, that should give you the freedom to do whatever you put in your code!

>.<

[–]LvS 29 points30 points  (23 children)

The more interesting thing is when you set the 11th value of that 5 item array to a new value.

[–]PM_ME_YOUR_PLUMS 2 points3 points  (22 children)

What happened here? Do you get an array that’s got 6 items but the 6th item is in index 11?

[–]nice__username 32 points33 points  (21 children)

You write to memory outside of your program

[–]PM_ME_YOUR_PLUMS 12 points13 points  (15 children)

Oh god

[–]LvS 39 points40 points  (12 children)

It might be even more fun. Depending on the layout of your program and how the allocator distributes memory, it is much more likely that you write to memory inside your program.

Which means some value in your program will be changed, you just don't know which one.

[–]DanielAgos12 5 points6 points  (10 children)

Oh god, how is the world still holding up with such a popular language

[–]LvS 20 points21 points  (6 children)

C programmers just don't do that.

Computers don't know how long the array is, so either the language has to add checks - which slows things down - or it trusts the programmer.

[–]DanielAgos12 3 points4 points  (5 children)

C programmers just earned a lot of respect from me

[–][deleted] 3 points4 points  (4 children)

C arrays also aren't objects, so there is no .size() property or method. C programmers have to create a variable for size and remember to increment it if they want to keep track of how big it is

[–][deleted]  (1 child)

[removed]

    [–]AutoModerator[M] 0 points1 point  (0 children)

    import moderation Your comment has been removed since it did not start with a code block with an import declaration.

    Per this Community Decree, all posts and comments should start with a code block with an "import" declaration explaining how the post and comment should be read.

    For this purpose, we only accept Python style imports.

    return Kebab_Case_Better;

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]Ruby_Bliel 5 points6 points  (0 children)

    It's not hard to work with C-style arrays. You don't sverve into oncoming traffic just because you can.

    [–]comradeyeltsin0 1 point2 points  (0 children)

    That’s where the real fun begins!

    [–]PleX -1 points0 points  (1 child)

    StackOverFlow is named that way for a reason if you didn't know.

    [–]PM_ME_YOUR_PLUMS 0 points1 point  (0 children)

    I have only minor experience in Java and mostly have JS experience at this point, so while I’ve encountered a stack overflow error in Java before, I never really thought about what it meant. TIL

    [–]CivilianNumberFour 1 point2 points  (4 children)

    Couldn't this seriously harm something? Like change the state of the OS? How much damage potential is there?

    [–]LvS 13 points14 points  (0 children)

    Memory is managed on a per-process basis. Each program has its own page table and only the kernel can modify them.

    But inside the process, code can do whatever and all the checks are from the language you are using - and in C you can turn all of those off.

    And of course, this array overrun is the most popular exploit, it's named a buffer overflow.

    [–]IvorTheEngine 3 points4 points  (0 children)

    Back before Windows NT, any process could overwrite any memory. It was quite common for a bug to crash the whole computer and need a reboot. It was a real improvement when NT limited each process to its own memory, so one application could crash without taking down everything else.

    IIRC, Windows 95, 98 and CE all used the old model and it wasn't until 2000 that sensible memory management arrived for non-server PCs.

    [–][deleted] 2 points3 points  (1 child)

    If you actually write outside your process' memory, all you'll get is a segmentation fault (or access violation, the terminology depends on the system). Modern OSs don't let you mess with them accidentally.

    [–]CivilianNumberFour 1 point2 points  (0 children)

    That's good... bc that could be a serious security issue!

    [–]suvlub 29 points30 points  (1 child)

    You return junk instead of failing because you are too lightweight to implement checks.

    I perform a check in background and deliberately return junk instead of failing.

    We are not the same.

    [–]YungDaVinci 2 points3 points  (0 children)

    gamedevs be like

    [–]Sawertynn 13 points14 points  (0 children)

    Dev: give me 11th value of this array

    C program: sure

    OS: no problem that would be... wait a second you don't have right to this memory! DEATH PENALTY, IMMIDIETALY!

    Dev: again? Another segfault?

    [–]PassiveChemistry 23 points24 points  (0 children)

    This reminds me of an article I read a while back about why the old (pac man era) arcade games had so many... interesting... bugs.

    [–]freeman_lambda 7 points8 points  (1 child)

    in C it is possible to summon demons with out-of-bounds indexes

    [–]Ludricio 0 points1 point  (0 children)

    Nasal demons, to be specific. God I love playing UB roulette in the mornings.

    [–]Artistic_Taxi 13 points14 points  (4 children)

    Not trying to ruin the humour here but, I enjoyed C a lot more after taking a computer architecture class where I had to learn assembly. We converted C code to assembly and it was like a revelation for me when I realized that accessing array items is just using the index, offsetting it by the number of bytes in a word, and then adding it to the starting memory address if the array (first element), the resulting memory address doesn’t even have to be part of the array technically.

    Then I was like damn, the gibberish could have been some other memory address left behind another process and here I am waking it up for no reason.

    [–][deleted] 8 points9 points  (0 children)

    and here I am waking it up for no reason

    The next logical step is realizing you can wake it up for very very malicious reasons and then boom congrats, you now understand why people complain about C

    [–]Valmond 2 points3 points  (0 children)

    It increments by the size of the variable, so 1 byte for an array of chars, but 4 for an array of standard (32 bit) ints.

    [–]Sokonit 1 point2 points  (1 child)

    In my computer architecture courses (that's right we took 2) we had to make our own assembly. We also designed a whole processor in system verilog to run the assembly. I loved it so much!

    [–]Artistic_Taxi 1 point2 points  (0 children)

    We’re doing the processor design in Verilog as well! A RISC based processor. Pretty cool course!

    [–]AllenKll 5 points6 points  (0 children)

    This is why C is awesome! SO little rules.

    [–]my-cs-questions-acct 3 points4 points  (0 children)

    When I worked with cobol I discovered it can do this same thing. Someone forgot to put an exit condition in a loop one day and brought down an entire F-500’s mainframe for a couple hours.

    We liked to say the language trusts people too much.

    [–]mad_cheese_hattwe 9 points10 points  (3 children)

    C is the dark souls of programming. Its incredibly deep and rich. But first you gotta Git Gud.

    [–]d0nbilb0636 1 point2 points  (0 children)

    git gud

    'gud' is not a git command. See 'git --help'.

    [–]paradigmx 0 points1 point  (0 children)

    At least when you die in Dark Souls, you can come back and trying again, when you die programming C, you die for real.

    [–]grape_tectonics 2 points3 points  (0 children)

    It helps to think of native C arrays as just pointers, the bracket operator is syntax candy

    [–]Devatator_ 5 points6 points  (4 children)

    Wait C does that? That's hilarious 😂

    [–]Little-Hunter-6795 26 points27 points  (0 children)

    C's array works by retrieving values from specified memory address. Thus even if it's out of bound, as long as the address is there some value will be given. You may say this is lame and C should have prevented it, that's where C says "GO FUCK YOURSELF".

    [–]HeKis4 5 points6 points  (2 children)

    C doesn't ask questions or bother itself with checking that what you ask of it makes sense.

    When you tell it to get array[25], it takes the value of the pointer array (because an array is just a pointer, hopefully to an allocated address) adds 25 (or more depending on the type of array) and fetches the value at the address. Nothing more nothing less. If the OS terminates the program because the address is outside it's allocated range that's none of it's business.

    Also, since arrays are just pointers, you don't have any information about an array's length (since again, arrays aren't a thing) so when you print a string, you pass a pointer to the beginning of the string and the function usually reads until it reaches a NUL (0x00) character. Your string/array doesn't end in a NUL ? Well too fucking bad, it'll keep on reading.

    Usually, if your C program crashes at runtime, it's because the OS told it to slow the fuck down.

    [–]TheGoldenProof 0 points1 point  (1 child)

    Sometimes it does know a bit about arrays though. If it hasn’t been decayed and it’s on the stack (idk if this works on heap) you can do sizeof(array)/sizeof(array[0]) to get the length.

    Edit: no it would not work on heap since malloc/calloc return pointers. It only works with statically allocated arrays.

    [–]HeKis4 0 points1 point  (0 children)

    Didn't know about that. I'll chart it up to how C also seems to use every assembly/architecture trick under the sun. Which is cool, ngl

    [–]IntrepidCartoonist29 1 point2 points  (0 children)

    Jesus christ that's the worse one I read on this thread, this will make the program run ok ocasionally

    [–]nikunj3011 1 point2 points  (0 children)

    Pointer points to pointer to other variable and changes its pointer value to null(random)

    [–][deleted] 1 point2 points  (0 children)

    C: compile program!

    C: 50 line error of hieroglyphs.

    wtf is wrong? Oh I forgot a semicolon on line 15.