Hey everyone,
First off, I used to use string - the type - all the time. That's what I learnt to use and what I used by habit. Now that I started working with the Win32 API, having gone there from GTK, I found myself forced to learn the intricacies of dynamically allocated strings.
In this program, there's a central area where I output various information to the user, by the means of creating strings at various points in the program using mainly strcat, and sending them off to a function designed to check the string, remove the oldest string from view and add the latest to the end.
I've been having troubles with my function that's meant to redo all the strings and prep them for moving. It's basically a for() loop, that deletes one string and copies the next into that space. I had to create my own class and throw a char* string in it because dynamic strings didn't cooperate with arrays of strings...
Either way, the problem comes up more or less as soon as the new string is to be allocated. I remove the old one using delete [], then chalk it up for new char [] according to the length of the new string (plus a few extra characters, just in case), and calls strcpy on the two. In theory. The entire program locks up and dies when meant to allocate the new string.
Here's the code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
int BumpDownTexts() {
for(int i = 0; i < MaxNumberOfLinesAllowed; i++) {
MainStaticStrings[i].stringtext = NULL;
delete [] MainStaticStrings[i].stringtext;
if(MainStaticStrings[i+1].stringtext == "") {
//Error handling. ish.
}
MainStaticStrings[i].stringtext = new char[strlen(MainStaticStrings[i+1].stringtext)+5]; //<--There!
strcpy(MainStaticStrings[i].stringtext, MainStaticStrings[i+1].stringtext);
}
}
|
In real code I've a ton of couts and pauses to let me identify what area the err's in, but I omitted them here.
What's messed up here, do you think? The funny thing is that this procedure works fine - once. I don't call this function for the first X (defined by MaxNumberOfLinesAllowed) strings, to allow the text output area to fill up before I sage anything, and it works just great. But when I empty and re-fill the strings, nothing cooperates. Program locks up at the very first string.
What could be my problem? I know the problem is in this part of the code, and virtually nothing else is touching the strings before this (well, they're being filled once, but they're emptied first). The class, here defined as MainStaticStrings, is simply a class with a char* thrown into it as its single variable.
Ideas? :(