Can an invalid pointer clobber the new operator?
I have a problem with my code where it chokes on a "new" statement. This is what it looks like:
void String::set(char *cString)
/* This is a function in my own String class which is an
assignment of the c-style string pointed to by cString
to the String object. */
if(str != NULL)
delete str; // str is of type char*
int len = strlen(cString);
logfile << "about to allocate memory" << endl;
str = new char[len];
logfile << "memory allocated" << endl;
This has been working fine for months, but all of sudden I am getting a run-time error in the program at the same point every time I run it. I have not modified the String class for months. The program successfully calls this function about a hundred times before it reaches the point where it fails. On checking the log file, I find that the "about to allocate memory" message appears, but the "memory allocated" message is not reached.
I can't see how a call to "new char[n]" can suddenly fail. I'm guessing that an invalid pointer somewhere else in the program is responsible for this. Can an invalid pointer actually reach and screw up the instructions of an operator? If not, any other ideas? It's a relatively small program, and the program fails at the same point if I cut the data by a third , so I'm sure it's not a case of running out of memory. I am using DJGPP.
Thanks in advance,