Thread: Strings

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    32

    Strings

    I am trying to create a dynamic string array in C++ that will read in words from a file. The length of the words and how many words will belong in the array is unknown.

    In the class XYZ, there is the following var char *str[];
    Code:
    XYZ obj;
    ifstream in;
    
    .....
    .....
    
    while (!in.eof())
       in >> obj.str[i++]>> ws;
    This isnt working. I know that char *tmp will let me store a word of any length (of course with the use of new) but if I want to make an array of words, I cant do it.

    thanx

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Unless you need to implement this as part of an exercise I would recommend the use of a vector of string objects to store all the words from the file. As it is, since you don't know how many words you are going to read in, you would need to continually reallocate your str member as the number of words increased, this is beyond any allocation for the words themselves. A better way would probably be a linked list of words. To allocate and store the words themselves you are going to need a fixed sized temp buffer to store the words into and you can then determine the length of the word and therefore how much space to allocate in your current node.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    32
    Well I was originally reading in the words and storing them into a linked list, but I thought that this might take up alot of memory because the file it is going to read has 1000's of words.

    Then I have another question. I am not to sure which way is the proper way of using new and delete i.e to make sure I am releasing ALL the memory I am using. This is of great importance to me.


    Code:
    SomeClass::SomeFnct()
    {
       FOO obj;
    
       obj.str = new char; //should this be here or in the loop
    
       infile.open("somefile.txt");   
    
       if (!infile())
      {
         cout << "unable to open file";
      else
     {
    
       while (!infile.eof())
       {
         // obj.str = new char;
          infile >> obj.str >> ws;
          makeLink(obj);//for the linked list
          delete obj.str; //would I need to release the memory here or at the end of the function
        } //while
      
          ..
          ..
          ..
      }//else
    
      infile.close();
       // delete obj.str;
    }

    Thank you to all that replies

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    use new and delete to work with dynamic memory for a given object and new [] and delete [] to work with dynamic memory for an array of objects. Routinely you allocate memory for a given object/array of objects just in the nick of time, you also routinely delete memory as soon as you are done with it. If you are using classes the allocation frequently occurs in the constructor and the release frequently occurs in the destructor.

    If you are using an array of Cstyle strings, then you will basically have:

    char array[#ofStrings][#sizeOfLongestString + 1];

    memory for array can be allocated on the stack on loading the program into memory, or on the heap, during the program as needed. Unless all the strings are of the same size you will be "wasting" memory in each of the strings that are shorter than the longest, so there is often no way of getting around the wasting of memory concern. Generally, unless you know memory is likely to be a major issue with your program I'd use whichever you are most comfortable with: an array of C style strings, a list of C style strings, or a vector of STL strings, and disregard the concern for memory wasting---at least until you get to the advanced stage of your program writing careeer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM