   new delete then new again
  I''m making a class that uses a string that I want to be able to change throughout the course of a game (its a text-box). I thought I saw something about it when I first started, but I can''t remember. I want to do this:   //pBmp is a function argumentif(m_pstrBitmap != NULL)   delete m_pstrBitmap;   m_pstrBitmap = new char[strlen(pBmp)];strcpy(m_pstrBitmap, pBmp);Is this a bad idea/practice?   /*I use DirectX 8.1 and C++ (Microsoft Visual C++ 6.0 Professional edition)*/ 
quote:Original post by Programmer16   m_pstrBitmap = new char[strlen(pBmp)];strcpy(m_pstrBitmap, pBmp);It's bad practice to not allocate for the terminating NULL.   [Edit]: spelling mistakeKami no Itte ga ore ni zettai naru!   [edited by - tangentz on February 18, 2004 12:36:34 PM];  Thanks! I always forget to do that.   /*I use DirectX 8.1 and C++ (Microsoft Visual C++ 6.0 Professional edition)*/ ;quote:if(m_pstrBitmap != NULL)  delete m_pstrBitmap;   the "if(...)" ain't needed here. delete works alright with null pointers.   edit: however, if m_pstrBitmap is a char[], then you should use delete[].   [edited by - Jolle on February 18, 2004 12:51:10 PM];  Also, if you allocated with new[], use delete[].Other than that, there''s no problem with reusing the samepointer.   Kami no Itte ga ore ni zettai naru! ;  If you use scalar delete on array new, yes, it''s very bad.delete[] m_pstrBitmap;And C++ does have a standard string type, you know. You can even get a const char* out of it when necessary.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  Alright, thanks guys!   I don't know how to use string, and didn't really thought it mattered. I like using data types instead of classes. I was planning on making a class of my own though (already have one made, but its old (made it 1 1/2 years ago))./*I use DirectX 8.1 and C++ (Microsoft Visual C++ 6.0 Professional edition)*/   [edited by - Programmer16 on February 18, 2004 12:56:57 PM] [edited by - Programmer16 on February 18, 2004 12:58:25 PM];  why would you want to make one of your own when a perfectly good one is provided for you?and why avoid objects in a language which is designed for them?if ya gonna do that then you might as well use C and not C++ ;  Making a string class on your own is fine for learning, but in practical applications, std::string should usually be used over some string class you made yourself.  And a class is   a data type.  Not a built-in   data type, true.  But then again, std::string is close enough to being built in, since it is part of the standard.  I highly recommend you reconsider using std::string.But I won''t hate you if you don''t.  I''ll just give you funny looks.  ;  It won''t bite you in this case, but you should always set a pointer to 0 after it''s deleted.Consider:byte *something = new byte[700];// code that uses somethingdelete [] something;// something = 0; // Naw, I don''t *really* need this!// other code that makes you forget you already deleted somethingdelete [] something; // Double free, very bad.  Setting your pointer to zero immediately after deletion removes the threat of a double free in most cases.  Of course this is a simplistic example. 