Jump to content

  • Log In with Google      Sign In   
  • Create Account

What int the heck is wrong with my string class?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Mike   Members   -  Reputation: 140

Like
Likes
Like

Posted 06 November 1999 - 01:06 PM

Rather than post the whole class here, I've uploaded the code (and VC++ 6.0 project and workspace) to mike010.hypermart.net/source.zip .

For some reason the class cuases the simple program I included with the source (I mean real simple program. It has no more than a main.). The problem occures whenever I try to do the following:

code:

delete [] m_chrString;

Yes, m_chrString is an array of type char that was create using:

code:

m_chrString = new char[ length ];

This is basic stuff, and it has me rether agrivated that I cannot find the fault im my code. Please do not respond with a cratique of my class, just tell me why its crashing the program.


Sponsor:

#2 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 03 November 1999 - 01:12 PM

I haven't downloaded your source, but I think I know what your problem is.

Change this:

m_chrString = new char[ length ];

to this:

m_chrString = new char[ length + 1 ];

Remember, you must allocate one more than the length to accomodate the null terminator at the end of the character array.

Hope this helps!

Josh


#3 Mike   Members   -  Reputation: 140

Like
Likes
Like

Posted 03 November 1999 - 02:23 PM

Excuse me for a moment while I go beat my head into a wall. Thanks, you were right.

#4 Xai   Crossbones+   -  Reputation: 1459

Like
Likes
Like

Posted 06 November 1999 - 03:45 AM

Of course the alloc with length+1 was right, but here's my problem with that answer. What does that have to do with the delete call? delete [], does NOT in any way care anything about the contents of your string, nor the NULL terminator, it just looks before the address of the array and checks how much was allocated, then it deletes that much. I don't see how fixing your alloc error would fix your delete problem. Perhaps I'm ignoring the fact that maybe your program allocated two strings in a row and therefore the alloc error cause the first string to store it's NULL terminator over the control information stored in front of the second string (which in turn would crash delete). Oh well....guess I'll never know for sure.

#5 Splat   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 November 1999 - 07:48 AM

Xai: You're probably right. Whenever you write past the allocated space you get, in Microsoft terminology, "undefined" results. As in you could be fine, the program could crash.

Or the whole computer could ignite in flames (only with the Microsoft™ Self-Destruct kit, sold seperately)

- Splat


#6 Niels   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 November 1999 - 01:06 PM

Apropos garbage collection !!





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS