I often face the problem of memory over-write. This problem occurs when i program with pressure of deadline, however this problem consumes much more time to diagnose. While working with my last code fragment i wasted many minutes just to figure out what is happening. I worked with a structure as follows:
#define TREEBLOCKWIDTH 2
struct inode{
public:
int keys[TREEBLOCKWIDTH];
int refnodeids[TREEBLOCKWIDTH + 1];
char* refnodenames[TREEBLOCKWIDTH + 1];
unsigned int elements;
bool isleaf;
};
Now in my code i come to know that i am updating a value in keys and the value is also changing in refnodeids. Now we look inside. The values in memory are as follows:
keys[0] keys[1] refnodeids[0] refnodeids[1] refnodenames[0] refnodenames[1] refnodenames[2] elements isleaf
Now try to access keys[2] and check what happens. Index 2 is out of bound for array keys, but C++ compiler will not complain for this, instead it will give you the value of refnodeids[0]. I like to call it silent array index out of bound because we are using the wrong index still code will execute and will make problem later on if left undiagnosed.
No comments:
Post a Comment