What the...

This is a discussion on What the... within the C++ Programming forums, part of the General Programming Boards category; Ok, this is just plain odd. There's some creepy stuff going on around here... I thought that this would make ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    Question What the...

    Ok, this is just plain odd. There's some creepy stuff going on around here...
    I thought that this would make my program crash for sure:

    Code:
    int array[2];
    cin>>array[5];
    cout<<array[5];
    Right? But it works perfectly. I input a number, and it outputs it. I compile it, 0 errors and 0 warnings. WTF? I'm 99% sure that if I were to run this do before a week ago, it would of crashed. What's going on here? Here's the really weird part: it works fine until I try to access an element in the array above 33. Yeah, that's right -- I try to output array[34], and it crashes like it should. Anything below or at 33 works fine. If I change the array to hold more than 2 numbers, then I can it doesn't crash with larger numbers. I'm using MSVC. Can anyone tell me what's happening here?
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    It is undefined behaviour, which means that it might not actually crash.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    It is undefined behaviour, which means that it might not actually crash.
    That's nonsense! It always would of crashed on something like this. Unless I've completly gone insane.

    Ok, how do explain why it works, then? huh? huh?
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    It wouldn't have always crashed. Perhaps usually, though.
    Away.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    As I sad, it works perfectly. Plus, no errors or warnings? Something's wrong. I've ran it multiple times, never crashes. I think I've just entered the Twilight Zone. Do do do do do do do do...
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  6. #6
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    There shouldn't be any errors or warnings. Bounds checking is left to the programmer in C/C++, not the compiler. If you want the compiler to check your bounds for you, use Basic (lol)
    Away.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by funkydude9
    As I sad, it works perfectly. Plus, no errors or warnings? Something's wrong. I've ran it multiple times, never crashes. I think I've just entered the Twilight Zone. Do do do do do do do do...
    Most likely, whatever you're corrupting on the stack isn't important. Because the stack is the same every time you run it, you get the same results.

    And no, you will never get errors or warnings. It's a well-formed program, it's just accessing memory on the stack that it shouldn't.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    But should it work? I mean, it output's the same number I input.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by funkydude9
    But should it work? I mean, it output's the same number I input.
    As stated, it's undefined. It could give you the output you want, it may crash. It may do nothing at all. It's undefined.

    It may work until you throw something else on the stack that changes the contents of that address, at which point it will crash, or perhaps continue on merrily.

    Again, as stated, boundry checking is left entirely up to the programmer. You can make a pointer try and point at any space of memory you like. You can even then try and dereference it or change it. However, if it's not something you've allocated specificly, you have no idea of knowing what will happen, because it's not yours to use.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    It's undefined. Meaning that it might work, or it might not. You shouldn't do it, because while it (might) work (in this case) (on your computer), it might not work, in another case, on my computer.

    You're writing over other stuff on the stack, which may or may not be empty memory - depends on the compiler and such. Then you're reading it. As long as nothing has changed it, you should get the same values... but it's not a good idea. It's a lot like using a pointer that hasn't been initialized.
    Away.

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Ok, ok...it's just that it seems as though it's been not crashing at all recently, but a while ago I'm pretty sure it would of crashed all the time. And you still haven't explained why doing something like array[100] crashes but not array[10].

    As stated, it's undefined. It could give you the output you want, it may crash. It may do nothing at all. It's undefined.
    If it's undefined the chances of me getting the same input should be the same of winning the lottery.
    Last edited by funkydude9; 08-10-2003 at 06:47 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  12. #12
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by funkydude9
    Ok, ok...it's just that it seems as though it's been not crashing at all recently, but a while ago I'm pretty sure it would of crashed all the time. And you still haven't explained why doing something like array[100] crashes but not array[10].



    If it's undefined the chances of me getting the same input should be the same of winning the lottery.
    array[100] could be overloading the stack.....

    Why are you making a big deal out of this? Sheesh, it's undefined....it's the same as telling an idiot to sit in the corner in a circle room. It's funny, but it's nothing to get excited about.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >And you still haven't explained why doing something like array[100] crashes but not array[10].
    You probably exceeded the bounds of safe memory. array[10] may be a noncritical location that you can access without immediately damaging anything, but further along the line, say array[100] could be being relied on and any changes will cause unpredictable behavior right away.

    >If it's undefined the chances of me getting the same input should be the same of winning the lottery.
    Undefined means you have no idea what will happen. It could work fine 100 times and then break the 101st, or it could break every time. In this case, it really depends on how your program is allocated memory and how you misuse that memory and how your system reacts to that misuse at the time.
    My best code is written with the delete key.

  14. #14
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by funkydude9

    If it's undefined the chances of me getting the same input should be the same of winning the lottery.
    Not at all. Undefined only means the standard doesn't say what should happen. When you run the program multiple times, your stack is the same each time so you get the same behavior. You'll probably get *consistent* results, as long as you use the same code and compiler.

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    As everyone has said, its undefined. If that snippet of code is all your program is, then chances are, the program will never actually use that block of memory, so you can (read: should not) do whatever to it without a crash. As your program grows in complexity, however, the chances that you'll actually overwrite and corrupt something that is vital to the program's execution increase significantly. If you do corrupt a part of the stack, then most likely, your program will crash.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21