PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Problem with a string literal

golan_trevize_x · 16 Jan 2003, 04:22 UTC
Hey guys,

I'm having an unusual error with a string literal in my code.

Assume the constructor for MyStringClass looks something like this:

Code:

MyStringClass::MyStringClass(char* cCharsIn) {

int i = strlen(cCharsIn);
...

}

Now, when I go to call this constructor:

Code:

MyStringClass* myString = new MyStringClass("hello");

Now, I've used lines like the one above throughout my program, but it's only failing on one of them.

It turns out that the constructor IS actually working, but the char* which should be received as the parameter turns out to be a strange pointer which is not pointing to a string at all. Thus, when I go to use strlen in the constructor, it crashes.

My guess is that I'm doing something screwy to the memory before I try and use this line, and that's why it's only failing this time. For some reason, the string literal is not getting instantiated? What do you guys think?

Thanks for your help!

Nathan
I would start by checking to make sure your stack is large enough. Check the linker settings to increase the stack size (and make sure you don't have some crazy array allocated on the stack like "char buf[1000][1000]". I would also be concerned that memory is being written out of bounds, thus corrupting the stack. Make sure you aren't jumping into a non-existant class to call your constructor. Like this:

CSomeClass* myclass;
myclass->DoSomething();

..where in DoSomething you have the line of code you mentioned that allocates the new instance of MyStringClass. Weird things can happen in that case. So if that line resides inside of another class, take a look at the value of "this" to make sure it points to a valid class. Same thing in your constructor. Make sure "this" is valid using the debugger.

Dan East
golan_trevize_x · 16 Jan 2003, 06:38 UTC
Good advice, Dan, thanks! I'll check some of this stuff out and try and find the problem.

I hate these kind of bugs, because I'm just not used to having them. This thing I'm making is probably the biggest single project I've ever done, and I'm more confident with Java than C++!

Nathan