Array and Pointer Bugs FAQ

What is misaddressing memory?

Your program is trying to access or modify memory at a wrong address. For example, your program might have indexed off the end of an array or outside of a block of allocated memory. Or your code has used a pointer that contains an invalid or incorrect address. For reasons of efficiency, C/C++ lets you do these things!

Help! My computer crashes or hangs with a message about "illegal memory access", "access violation", "unmapped memory" or "bus error."

Your program is trying to access or modify memory that is not its property, or at an impossible address in the hardware. Easy to do with C/C++ pointers!

Some of my variables and strings are changing value all by themselves! Am I going crazy?

No, this is a classic symptom of misaddressing memory. Some part of your program is storing data on top of some of your variables, making them change value even though none of the offending code even mentions them.

My program crashes at a certain place, but I am positive that there is nothing wrong with that code - it doesn't even use any pointers or arrays!

Misaddressing memory can cause "time bomb" errors. Your program can trash something in memory, run apparently normally for awhile, and then go down in flames when it tries to use the trashed data. There might be no apparent relationship between the symptoms of the crash and where your program originally trashed memory.

My program works fine except when it reads "Barney Google" from the input file - this doesn't make any sense!

When my program crashes, it does something different every time! I thought computers were predictable!

If your program misaddresses memory with bad subscripts or wild pointers, you are reading garbage values or storing into random places. The NORMAL result is that your program's behavior is UNPREDICTABLE. All kinds of weird things can happen. Sometimes you happen to store your data into an unused area of memory and so your program appears to behave normally. But some other time, your wild pointer could hit something vital - part of your data, your program, or even part of the system. Then your program produces peculiar results, crashes, or runs amok. Nastiest is when the trashed memory is actual program code! Really nasty is when the trashing confuses the I/O system and garbles your hard disk!

OK, how did my program misaddress memory?

How can I find misaddressing bugs?