Thread: Writing my own string class.

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    80

    Writing my own string class.

    I have a few questions about C-style strings. If I wanna change the string contained in my class as it is now I must make the pointer data point to another C-style string, but if I do this alot there will be plenty of static strings in memory. The problem with this is that, if I one day decide to make a chat program (or any other program in need of input) and my string keeps pointing to new data everytime I input something there will be a hugh waste of memory, wont it? I know about dynamic memory allocation using malloc/free or new/delete but still I will never know exactly how much input the program might receive.

    Other situations I will have the same trouble is if I want to concatenate two strings or copy strings, then I will have to set dato to point elsewhere but the old information will still be in memory.

    What I really want to know is if there is a good way to save memory in this case? My very unfinished string class looks as follows:
    Code:
    class Str
    {
        private:
    
        char* data;
        int length;
    
        void set(char* text)
        {
            data = text;
            length = getlen();
        }
    
        int getlen()
        {
            if (data == NULL)
                return 0;
    
            int i = 0;
    
            while (*data++ != '\0')
                i++;
    
            return i;
        }
    
        public:
    
        Str()
        {
            data = NULL;
            length = 0;
        }
    
        Str(char* text)
        {
            set(text);
        }
    
        void operator=(char* text)
        {
            set(text);
        }
    
        int len()
        {
            return length;
        }
    };
    Last edited by antex; 10-27-2006 at 07:05 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What I really want to know is if there is a good way to save memory in this case?
    I would recommend rewriting your class to begin with. Rather than storing pointers to memory owned by something else, your string class should own the string it stores. This makes life easier for you, and you can save memory with reference counting.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    Ok I see, but if I let it own the string, how can I let it have a dynamic size? I mean, there's no way to know the number of chars before the string have already been given as input. I don't feel like making like this:
    Code:
    char data[256];
    Or similar because that would waste memory. Any suggestions are very welcome. I'm not very good at c++ so I would appreciate detailed expalnations

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You allocate it on the heap using new[].
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> if I one day decide to make a chat program (or any other program in need of input)
    It is a good idea to think of these types of issues when designing a class, however, I would most likely use an existing string class like std::string instead of hand-made one if I was going to actually use it in a real application.

    >> how can I let it have a dynamic size?
    When you use new [], you specify the size. If you are initializing your string class from a C style string, you already know the size of that string, so you use that as the basis for how many chars to allocate. Another option is to allocate a certain amount of space to start, and when you assign a string that is larger than the currently allocated size, you re-allocate and copy to the new memory location. In that case perhaps you can double the amount of memory allocated each time you need more memory, which is a common allocation technique in a class like vector.

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    String is so complex class that you will probably never make so perfect string class as the standard one is.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by maxorator
    String is so complex class that you will probably never make so perfect string class as the standard one is.
    The std::string class (which is actually a specialisation of std::basic_string) is far from perfect.

    A basic principle of effective class design is to keep its interface minimal, and provide support for one basic concept (i.e. the thing that the class does) and that translates into minimising the number of redundant member functions (i.e. avoid multiple member functions doing the similar things). As a rule of thumb, that usually means most classes will have well less than 100 distinct member functions.

    Against this sort of rough guideline, std::string is actually viewed with some embarrassment by members of the C++ standard committee. It has well over 100 member function names, including overloading --- and several of those member functions are templates.

    The std::string class was designed by committee and it shows. Very few applications will ever need to use the full range of capabilities of the string class (string manipulation, substring extraction, searching, behaviour as a basic container, etc etc). For several capabilities, there are several different member functions that do the same thing in slightly different ways --- making it difficult to pick which way you do even simple tasks.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >you will probably never make so perfect string class as the standard one is.
    Bwahahahaha! The standard string class is an exercise in awful design practices. It tries to do too much, satisfy too many parties, and fails miserably at everything. It's better than rolling your own, but "perfect" is not an adjective that comes to mind when I think of std::string.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. 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
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM