I'm writing a String class, but I find I'm having trouble allocating memory. It contains two member variables, m_Length (int; the length of the string, not including the null at the end) and m_Char (* char to the contained string)
Upon debugging, I find that the same memory location is set to m_Char everytime for every instance of the class. Obviously this is a huge problem because every string I create points to the same string.
This is the function I created to allocate memory
void zString::Alloc(const unsigned int & Bytes)
{
if (m_Length) delete [] m_Char;
if (Bytes)
{
m_Char = new char[Bytes+1];
for(int i=0; i<=Bytes + 1; i++)
m_Char[ i ] = '\0';
}
m_Length = Bytes;
}
Why aren't you using, at the very least, the basic <string.h> functions in your class or even functions such as memcpy() when copying one buffer to another?
I can only answer your question with a simple, "because I feel like it"
That is not the way to approach programming in C++. We will give you answers based on the experience that we have here, which is not based on "feel" but on what works and is used by professionals. When another C++ programmer looks at your code, they will also wonder why you didn't use the library routines. It isn't just myself that will say this.
Is there any real advantage to coding it that way?
If you are talking about using the standard library, yes. It is already written for you, it is standard, it is as efficient as possible, it has no bugs, and other C++ programmers can understand the code much more quickly, since the functions used are standard. If I took a quick glance, I have no idea if those loops you wrote are correct. However, if I saw a simple call to strlen() or memcpy(), then this is fully understandable to myself and other programmers what those functions do.
Also, I read "Sams teach yourself c++" book. It nevered discusses std::string,
Throw the book away. It is either outdated (pre 1998), or it is a piece of junk. It isn't anything other than those two options.
Just these changes alone is all that I needed to get this to work. But I would never use this, since again, std::string is there so that you don't need any of this code.
Allright, so the problem is caused by not setting m_Char to NULL. I don't get why that caused a problem considering every constructor calls Alloc and Alloc set's m_Char to a new block of memory.
Allright, so the problem is caused by not setting m_Char to NULL. I don't get why that caused a problem considering every constructor calls Alloc and Alloc set's m_Char to a new block of memory.
Please look carefully at your Alloc code, and see what I changed it to. The very first line in Alloc is in error:
Code:
if (m_Length)
delete [] m_Char;
You set m_Length to a value, and then you are attempting to call delete on an invalid pointer. m_Char has a garbage value when the constructor was called, and calling delete [] on this value leads to unpredictable results. That is why setting it to NULL in the member-initialization list ensures that calling delete will not have any effect (calling delete on a NULL pointer is valid).
This is why std::string was developed, so you don't need to write code like this and cause bugs to occur.
"C++ Primer Plus" - Lippman
"Accelerated C++" - Koenig & Moo
"The C++ Standard Library" - Josuttis
"The C++ Programming Language 3rd edition" - Stroustrup
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.