Thread: Overflowing string... any easy way to fix it?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Overflowing string... any easy way to fix it?

    I've been working on a class, and I recently (finally) figured out exactly what was wrong with a certain line of code. My string class variation I'm working on doesn't have enough spaces to hold the new string. The problem is it is a flexible array size, and when it overflows it crashes as a result of some special code in the original class I'm converting from.

    Does anyone know an easy way to change the size of a string? I don't mind destroying the character array and starting a new one.

    Code:
    void ooint::intfcat(const char * Int1)
    {
      char Int2[1024];
      strcpy(Int2, Int1);
      strcat(Int2,CString);
      strcpy(CString,Int2);
    }
    Thats the member function, and it overflows with this if I use oostring instead of a character array (oostring is a string class, I'm making a variation of it that acts as an integer, but has some weird manipulations made easier.):
    Code:
    100 1001 index out of range: 5 string: 31001
    Assertion failed: 0 <= k && k < strlen(CString), file c:\program files\microsoft
     visual studio\myprojects\oostring.cpp, line 103
    The idea of the original string class was to make it so that it could expand when needed, but it isn't doing that here. Can anyone help me? The source is attatched, and basically all of it is intact at the moment with oostring renamed as ooint.

    Thanks so much! If you want more details on this, see the older thread: http://cboard.cprogramming.com/showt...threadid=29228

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    void ooint::intfcat(const char * Int1)
    {
      char Int2[1024];
      strcpy(Int2, Int1);
      strcat(Int2,CString);
      char *newCString = new char[strlen(CString) + strlen(Int2) + 1];
      strcpy(newCString,Int2);
       
      // delete[] CString; <-- Was it dynamically allocated? What type is it?
      CString = newCString;
    }

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Talking

    Heh, thanks! I kept trying to use the code in the operator overloads... that works perfectly! Fast response, too

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM