Thread: accessing outside of an array?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    11

    Question accessing outside of an array?

    Hello everybody!

    I have a quick question to ask: What happens when you access outside of an array?

    I was reading my programming book today and saw this.

    "If you were to declare an array size of [4] but request array[5] the operation proceeds at the memory location where array[5] would be if it existed. The result is a piece of data outside the array is overwritten which can create bugs that are very hard to track down."

    Does this mean it only causes a bug inside the program and doesn't do any permanent damage to my computer? Such as "Hey! Array[5] would be located right where kernel32 is! *overwrite kernel*" or "Array[5]...nope don't see it...Might as well place it where the video card driver is located..."

    I made a typo in an example program and it tried to access outside of an array but it ended up just saying "Card dealer#3 has stopped working searching for solution..."

    Just wondering if thats all it does or if my computer is about to explode or something.

    Anyways, Thank you to anyone who takes the time to answer my question!
    I really appreciate it!

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Well... I don't know how accurate my description is going to be, but here is my thinking. The program and computer have a mini-brain fart so to speak. The program declares 4 spaces of memory, in a sense "for itself", using that term loosely. Then when the program tries to use a 5th space the computer goes "wait... you said you wanted 4!!!" and that's when the program either crashes, bugs, or does unexpected things. To answer the big question of permanent damage... no. A program is not going to overwrite a file, or driver without you specifically telling it to. It won't overwrite a file just to make space for a 5th slot.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    11

    Lightbulb

    Alright thanks!

    I was a little afraid that my computer might have been about to spontaneously combust or something else that would have been very bad.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by avaldi View Post
    Alright thanks!

    I was a little afraid that my computer might have been about to spontaneously combust or something else that would have been very bad.
    Nope. One of my professors for a C++ class said it was possible to crash a computer through overloading it with way too many arrays with way too many dimensions. What did I do.... I tried it... the computer ran for half a second, froze, and I had to do a hard shut down. Started back up and worked like it didn't happen.

    Although, overloading the computer with too much to think about is different than giving it one extra 5th number

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It's undefined behavior. People often joke that undefined behavior means anything can happen, but in reality, modern OSs do a pretty good job of protecting the rest of the memory space from bad programs.

    However, these kinds of mistakes are security vulnerabilities that can leave your program open to malicious attacks that would cause real harm.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by avaldi View Post
    Does this mean it only causes a bug inside the program and doesn't do any permanent damage to my computer? Such as "Hey! Array[5] would be located right where kernel32 is! *overwrite kernel*" or "Array[5]...nope don't see it...Might as well place it where the video card driver is located..."
    As it has already been stated, nowadays this isn't likely to happen at all. That's not to say that crazy stuff *won't* happen, but your not going to cause permanent damage anymore. At least, not to hardware. Security risks and data corruption are still real problems. IIRC, this error was one of the primary means of attack when I used to follow such things.

    That didn't used to be the case though. It's funny you spoke of the video card driver, because on the earlier IBM PC you could very well cause catastrophic damage if you were tinkering with the video and set the scan rate wrong by mistake. If you wrote to Bad Places(tm) through wild pointers or something, you could set the video chip scan rate to zero and the transformer that produced the voltage which drove the electrons to light up the phosphors on the monitor would be fed a constant voltage (not modulating to reduce heat). This, in turn, would make the transformer act as a resistor which would heat up considerably and burn the monitor out instantly. This did happen.

    On modern PC's, this type of stuff is a thing of the past. On other systems (particularly embedded) it is still real, but not common. IOW, don't sweat it when you are learning on your computer. You are going to make this mistake more than once and the OS will save you by catching a bad access. But, that's no license to get sloppy either.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As stated, in modern OS's (Windows, Linux, OS/2 2.x+, Solaris, VAX/VMS, Unix, etc), the OS is protected from user-mode applications (User mode applications would be your code that you wrote, along with almost every other application, such as the IDE/Text Editor you use to create the program, the web-browser, the compiler, the network services that let your computer talk to other computers, etc, etc). This is done through something called virtual memory mapping - it allows the same virtual address being used by different applications, making it much easier to deal with multiple applications. But it also allows the OS to set the availability of any piece of memory to be "user" or "kernel" mode. User-mode memory is available to the user-mode application. Kernel-mode memory is ONLY available when you have entered kernel mode (and there's a "guard" at the door to the kernel, so only "safe" code can enter the kernel, at least in a well-written OS). Think of it as a medieval town with a wall around the town itself. People can only enter through the appropriate gates of the wall [at least without tall ladders and the risk of being poured over with boiling oil and such].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    I'm just happy I don't have to worry about my computer imploding if I accessed outside of an array. The information here is pretty amazing, I really have to respect the amount of knowledge that everyone here has.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  2. accessing user entered array elements by index?
    By richdb in forum C Programming
    Replies: 10
    Last Post: 04-08-2006, 11:10 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. Object array. Probelm accessing methods
    By Bean in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2004, 11:22 AM

Tags for this Thread