Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Agincourt

DMA - Strings

This topic is 6032 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
I should think it would be simpler to do
  
String::String (char* msg) {
myLength = strlen(msg);
myString = new char[myLength+1];
strcpy (myString, msg);
}


-Neophyte

- Death awaits you all with nasty, big, pointy teeth. -

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Neophyte: damn you! j/k. cool.

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.

Share this post


Link to post
Share on other sites
Both your suggestions work the same way as what I''ve already got. And don''t C++ strings ignore the Null terminating character and instead use a seperate variable to keep track of size? at least that''s what my program is supposed to do.

Share this post


Link to post
Share on other sites
But to be truly C++ strings style, you need to provide the default instructor:

  
class String
{
public:
String();

private:
int m_nLength;
char * m_pStr;
};

String::String()
{
m_nLength = 0;
m_pStr = "";
}


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")'').

  
String &String::operator= (const char *cstr)
{
int c, n = 0;
while(cstr[n] != ''\0'')
++n;
char *tmp = new char[m_nLength + n];
for(c = 0; c < m_nLength; ++c)
// copy existing string

tmp[c] = m_pStr[c];
delete m_pStr;
for(c = 0; c < n; ++c)
// append new string

tmp[m_nLength + c] = cstr[c];
m_pStr = tmp;
return *this;
}


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.

Share this post


Link to post
Share on other sites
The only downside to Neophyte''s method is the overhead of calling the standard C library functions (plus you don''t want to be dependent on the C library unless absolutely necessary).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
append a "null" (i.e. ''0'') terminator. it''s the way "C" functions terminate a string.

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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!