![]() |
| | #1 |
| Registered User Join Date: Jun 2008
Posts: 11
| 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! |
| avaldi is offline | |
| | #2 |
| Registered User Join Date: Mar 2007
Posts: 335
| 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.
__________________ home page (new layout) |
| scwizzo is offline | |
| | #3 |
| Registered User Join Date: Jun 2008
Posts: 11
| 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. |
| avaldi is offline | |
| | #4 | |
| Registered User Join Date: Mar 2007
Posts: 335
| Quote:
Although, overloading the computer with too much to think about is different than giving it one extra 5th number
__________________ home page (new layout) | |
| scwizzo is offline | |
| | #5 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| 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. |
| Daved is offline | |
| | #6 | |
| Registered User Join Date: Jul 2003
Posts: 111
| Quote:
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. | |
| whoie is offline | |
| | #7 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| 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. |
| matsp is offline | |
| | #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. |
| avaldi is offline | |
![]() |
| Tags |
| arrays, newbie question |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 2d array question | gmanUK | C Programming | 2 | 04-21-2006 12:20 PM |
| accessing user entered array elements by index? | richdb | C Programming | 10 | 04-08-2006 11:10 AM |
| question about multidimensional arrays | richdb | C Programming | 22 | 02-26-2006 09:51 AM |
| [question]Analyzing data in a two-dimensional array | burbose | C Programming | 2 | 06-13-2005 07:31 AM |
| Object array. Probelm accessing methods | Bean | C++ Programming | 4 | 05-03-2004 11:22 AM |