🔒 Locked
DMA - Strings
Advertisement
Original Post
I'm writing an implementation of C++ style strings for a class I'm taking, and I'm having a problem with my constructor. It looks like this -
String::String(char * msg)
{
myLength = 0;
while(msg[myLength] != '\0')
{
myLength++;
}
myString = new char[myLength];
for(int index = 0; index <= myLength; index++)
myString[index] = msg[index];
}
It's supposed to take a C-style string and convert it. It works most of the way, except sometimes it reads in an extra character of random memory. I'm not sure why it would work sometimes and not others. I assume the bug occurs in the constructor and not the printing because printing is supposedly the same as the last implementation (non Dynamic Memory Allocation). also, myString is a private data member declared as char *myString. if you can help that'd be great, if you need more info I'll check back real soon. thanks
Use the WriteCoolGame() function
Works every time
Edited by - Agincourt on November 20, 2001 8:30:37 PM
try this:
String::String(char *pMsg)
{
int iLength;
iLength = ((int)strlen(pMsg) + 1);
if ((this->myString = new char[iLength]) != NULL) {
// copy the string.
strcpy(this->myString, pMsg);
}
else this->myString = NULL; // unable to alloc. memory.
}
i.e.: you need to alllocate memory for the "null" terminator.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
String::String(char *pMsg)
{
int iLength;
iLength = ((int)strlen(pMsg) + 1);
if ((this->myString = new char[iLength]) != NULL) {
// copy the string.
strcpy(this->myString, pMsg);
}
else this->myString = NULL; // unable to alloc. memory.
}
i.e.: you need to alllocate memory for the "null" terminator.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
But to be truly C++ strings style, you need to provide the default instructor:
You''d then need to overload the assingment operator, since most often strings are initialized rather than "constructed" (ie more often ''string str = "hey!"'' than ''string str("hey")'').
This behaves more like the std::string class, but is horribly inefficient with its constant memory allocations and reallocations. So do the smart thing: whenever we exceed the capacity of the string, double its length.
Oh, and of course you can look at the source of the actual string class for inspiration.
|
You''d then need to overload the assingment operator, since most often strings are initialized rather than "constructed" (ie more often ''string str = "hey!"'' than ''string str("hey")'').
|
This behaves more like the std::string class, but is horribly inefficient with its constant memory allocations and reallocations. So do the smart thing: whenever we exceed the capacity of the string, double its length.
Oh, and of course you can look at the source of the actual string class for inspiration.
I have a default constructor and have overloaded just about every operator. My problem(I think) is that for some reason occasionaly myLength gets one too big or something, because when I print it out I get an extra garbage character on some of them. Why only some? I don''t get it. I hope I''m being clear
Use the WriteCoolGame() function
Works every time
Use the WriteCoolGame() function
Works every time
In the for loop, you copy myLength+1 characters. You say you
don''t want the termination null. Thus change the <= for a <
like this:
for(int index = 0; index < myLength; index++)
myString[index] = msg[index];
The last character in the myString array is now
myString[myLength-1], as it''s supposed to be. In fact,
you were writing on non-allocated space!
This problem is solved, but it will probably not solve your printing problem. Can you write your printing code?
don''t want the termination null. Thus change the <= for a <
like this:
for(int index = 0; index < myLength; index++)
myString[index] = msg[index];
The last character in the myString array is now
myString[myLength-1], as it''s supposed to be. In fact,
you were writing on non-allocated space!
This problem is solved, but it will probably not solve your printing problem. Can you write your printing code?
got it, kind of. I was reading in the null terminated character and so that did mess up my output operator. It works if I print only up to myLength-1. Now to overload my addition operators correctly. hmmm. that can be my own problem. Thanks for all the help guys
Use the WriteCoolGame() function
Works every time
Use the WriteCoolGame() function
Works every time
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement