Here's a good one.
-
:wtf: Okay here's a problem I am having: I create a pointer to a char like so: char *test = new char; then I pass that pointer to a class member function which fills it with data from a file. Heres the code for that: bool FileManager::ReadString(LPSTR StringData) { char SingleChar; bool done = false; while (done != true) { if(ReadData(&SingleChar, 1)==2) { return true; } switch (SingleChar) { case 10: case 13: { done=true; }break; default: { *StringData = SingleChar; StringData++; }break; } } *StringData = 0; StringData++; return true; } Then afterwards in the function that called the member function I do a: delete test; When it runs this piece of code I get: Debug Error! Program: c:\JD's Projects\FileManager\Debug\FileManager.exe DAMAGE: after Normal block (#38) at 0x00851FC0. Any Ideas? thanks, JD
-
:wtf: Okay here's a problem I am having: I create a pointer to a char like so: char *test = new char; then I pass that pointer to a class member function which fills it with data from a file. Heres the code for that: bool FileManager::ReadString(LPSTR StringData) { char SingleChar; bool done = false; while (done != true) { if(ReadData(&SingleChar, 1)==2) { return true; } switch (SingleChar) { case 10: case 13: { done=true; }break; default: { *StringData = SingleChar; StringData++; }break; } } *StringData = 0; StringData++; return true; } Then afterwards in the function that called the member function I do a: delete test; When it runs this piece of code I get: Debug Error! Program: c:\JD's Projects\FileManager\Debug\FileManager.exe DAMAGE: after Normal block (#38) at 0x00851FC0. Any Ideas? thanks, JD
Your test variable is a pointer to a SINGLE char value. If you want to store an array of chars i.e. a string, you will have to declare an array: char* pArray = new char[NumChars + 1]; where NumChars is the number of characters you want to store. If you don't know in advance how many you want to store, pick a large constant value. Note the extra one added to accommodate the null-terminator at the end of the string. This is a style issue, you may wish to include the null-terminator in the NumChars count. Now when you use your pointer arithmetic: StringData++; you will be using valid allocated memory. Remember that when you come to deallocate an array, the syntax is as follows: delete []pArray; // Note the square brackets On a related note, you might want to look at the standard library's basic_string<> template (STL). use MSDN - it can simpify string handling a great deal and is a good way to learn about templates. Hope that helps.