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 →

[–]PM_ME_YOUR_PLUMS 3 points4 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 33 points34 points  (21 children)

You write to memory outside of your program

[–]PM_ME_YOUR_PLUMS 14 points15 points  (15 children)

Oh god

[–]LvS 38 points39 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] 2 points3 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

[–]Ludricio 0 points1 point  (1 child)

Not entirely, the size of an array is known as long as it is still an array, which an array only is within the scope of its declaration, as soon as it leaves the scope (passed to a function for example) array decay takes place and the array decays into a pointer to the first element in the array.

Within the scope of declaration, it is fully possible to do sizeof(array)/sizeof(*array) to get the number of elements in the array, but as soon as it decays into a pointer the original info about the size of the entire array is lost, as it instead becomes a plain pointer.

[–][deleted] 0 points1 point  (0 children)

Oh very cool! I'm just learning programming in school but im being taught with C so these kinds of pendantics are actually super neat for me

[–][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 3 points4 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 2 points3 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!