char*'s again!

Started by
7 comments, last by Jason2Jason 21 years, 3 months ago
Hey, i asked a question a few months back about char*''s and if i was using them correctly, which it turned out i was. Any way, i was making a window wrapper class, and i need to declear a char* to hold the classname string. It was then i realised (after a few compile and run time errors) that you cant do thing like that. Heres what i did:
  
class wndwrapper
{
public:
    ...
    ~wndwrapper();
    void init_wnd(char* ClassName);
    ...

private:
    ...
    char* clsname;
    ...
};

void wndwrapper::init_wnd(char* ClassName)
{
    clsname = new char[sizeof ClassName];

    strcpy(clsname, ClassName);

//do stuff

}
wndwrapper::~wndwrapper()
{
    delete clsname;
}

  
Thats the basics of what i''m doing with the char. The class name is passed to the init function, that copies that to the ''clsname'' char*. For some reason i keep getting errors that say: "Error, cant "read" memory address 0xblablabla". This happens when the program shuts down, obviously when the destroy function tries to read from clsname. It doesn''t happen at the begginning because i use the first classname string which is in the function decleration. Basicly i''m asking how can i initial a char* string, and copy from another string, the correct way? If you need more info, just ask. thx, -J
Advertisement
1) Use strlen() to get the length of a null-terminated string:

strlen(ClassName);

2) There''s a special method to delete dynamically allocated arrays:

delete [] clsname;
--------------------------{ Arena: Resurrection, my text-based RPG/Mortal Kombat-like game }{ A Look Into The World Of Arena II, a collection of short stories about characters in Arena II }
The sizeof operator returns the size of the type. It knows nothing about NULL terminated strings. You can simply use the string class (in the header in the std namespace) to make it easier. Or, you can use the strlen function (remember to add 1 character for the NULL terminator).

Edit: someone posted while the server was giving me 10 errors .



[edited by - Null and Void on January 23, 2003 2:38:11 PM]
sizeof(ClassName) doesn't return the length of the string. It returns the size of whatever type ClassName is (in this case - size of a char *, which is 4). So, you're allocating 4 bytes and trying to copy the entire string, which could be considerably longer than 4 bytes.

Try "new char[strlen(ClassName)]" or just use strdup(), which will do the allocation and copy in one function call -

clsname = strdup(ClassName);

Or, you could use a string type and just use assignment -

string clsname;
...
clsname = ClassName;

When you need to pass clsname as a "char *", use -

clsname.c_str()

If you're unfamiliar with the STL, ignore that last option.


Having said all of that, it could be that you have a bug somewhere else in your code, as well. Maybe unintentionally changing the value of clsname.


EDIT: Well, aren't we all johnny-on-the-spot!


[edited by - Dave Hunt on January 23, 2003 2:39:16 PM]
clsname = new char[sizeof ClassName];

On that line you''re most likely allocating only 4 bytes, as that''s the size of a pointer on most 32 bit machines.

If what you''re trying to do is make a copy of a string, use strlen to get the length of the string.

Or, of course, make all the interfaces to your code work with std::string, and then only use C style strings internally.
quote:Original post by Null and Void
Or, you can use the strlen function (remember to add 1 character for the NULL terminator).


So it would be: "new char[strlen(ClassName)+1];" ?

I''d rather not use too many external things such as the std libarary, and CString, because I need this game to run as fast as possible, unless it doesnt have much of a impact, then I might use it. Does it?

-J
Oops! Yes, I forgot the +1!

As to speed, the STL string class isn''t too bad depending on how much of it you use. For simple assignments and copies, you won''t notice the difference. CString is an MFC class and would mean linking to the MFC libraries. You probably don''t want to do that just to get a string class.

I''d stick with the plain old char* if you don''t need any string manipulation/searching functions.

  char* copy_string(const char* s){   return s ? strcpy(new char[strlen(s) + 1], s) : 0;}char* s = copy_string("foo");// Do stuff with s.delete [] s;  



Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
OK, my program''s fine now, untill i add in the message loop. Hmm, i could have something to do with the wrapper class, i might post a question about that later.

Thanks for the help guys!

-J

Pre-Post EDIT: Yup, it was the simplest of errors in the getmessage part of the code :-/. Well, learn from mistakes!

This topic is closed to new replies.

Advertisement