Dynamic C strings troubles

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? :(

Invert lines 5 and 6 or you'll create a memory leak.
You can't use the equality operator on C strings. PROTIP: If you want to know if a C string is empty, see if !string[0].
if(MainStaticStrings[i+1].stringtext == "")?
Shouldn't you be using strcmp?

new char[strlen(MainStaticStrings[i+1].stringtext)+5];
+ 5? Shouldn't that be + 1?
Invert lines 5 and 6 or you'll create a memory leak.
You can't use the equality operator on C strings. PROTIP: If you want to know if a C string is empty, see if !string[0].


Originally, I didn't even have both lines in there, just deleted the char straight away. I looked into it and did changes as per your advice, and I could suddenly run the function through. But I've no idea why. I simply added the = NULL, and changed the error handler around.

Also, I have used the equality operator before. Wrote an if() to check for a redraw message in my text output function. Worked perfectly. Will this cause trouble later on?

Shouldn't you be using strcmp?

I had my doubts about that function when I first read about it. Does it work better than !string[0]?

+ 5? Shouldn't that be + 1?

The +1 is to make sure I'm using the next string, not the current one. +5 is just an additional five characters that I've had a habit of adding just for the sake of, well, ad hoc force majeure and what have you.


Now for some reason, the first three or four strings in this function checks out properly, but it hangs after that. I'm sure the string is in proper order, and the length is being calculated OK and so on, but it nonetheless hangs on the fourth run of this function...
Last edited on
!string[0] will do. It's not the same as string=="", which is an address comparison.

C string are null terminated, so you only need one extra char, not five. If you're getting memory overwrites at the end of your strings, you have other issues that need sorting out.
I tried changing the +5 to +1, but that lead to the program locking up. Changing it back to +5 allowed the string to process properly again.

What does that signify? Is there some additional process I should go through to make sure that, well, there's no shit in the string I'm transferring to, that may muck stuff up?

Either way, my problems are steadily getting bigger. I tried disabling one of the primary functions from calling the text output function, which caused the program to lock up when the other primary function had called the function for the fifth time.
Problems seem to be based around me fiddling around with the text outputs, chars etcetera. Could I have inadvertently caused some freaky memory loss, infinite loops or stuff like that? Would it cause the program to just shut itself down, no error message, freezing or anything?
It means you have some kind of memory overwrite. This is probably being done in one of your strcpy calls. Sometimes being carefull isn't enough. Commercial code should rarely use strcpy, but use the safer strncpy instead.

You need to check each call and be sure that the destination is large enough to hold the source string and the null terminator. It's a real pain, I know.

The C++ string class was introduced to eliminate this precise problem.
Sounds good. I mean, sure, pain in the butt, but at least it's better than when it's constantly crashing and I have no idea why. Thanks a lot for all the help.
Topic archived. No new replies allowed.