#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    11
    Rep Power
    0

    How to dynamically store strings in a variable


    I am getting results from some s/w as a list of strings. The total number of these strings are in millions. I am checking each of these string and if some conditions are met, I want to store that string in a variable ('listOfStrings'). The total number of strings will be in couple of hundreds, sometimes more or less. There is no way I can guess this number.

    "avvvaa"
    "sssv"
    "ssssfsf"
    "aaasfsf"
    "vvvsfsfs"


    char **listOfStrings;

    I was wondering, is there a way read the above strings one at a time and if the conditions are met, copy into 'listOfStrings'? I do not want to declare total number of elements in the begining. It can be assumed to be a growing list of strings.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    I'd go with (aside from choosing a programming language from a different branch of the programming language tree) C++. Use the vector template from the standard template library.
    http://www.sgi.com/tech/stl/Vector.html and the internet is loaded with stl examples.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    I agree. Though if you need to stick with C, then look into realloc(). You can Google on man page realloc for the documentation.
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,113
    Rep Power
    1803
    Originally Posted by dwise1_aol
    I agree. Though if you need to stick with C, then look into realloc(). You can Google on man page realloc for the documentation.
    A naive implementation might thrash the memory manager a lot and be very slow (same issue with STL <vector>, although the implementation of <vector> uses methods to avoid that as far as possible). If the stored data is subsequently assessed sequentially rather than direct access, a std::list or linked list might be preferable.

    Either way, the C++ STL will likely have a suitable container. To help you decide which is appropriate this page describes the properties of various container classes.

IMN logo majestic logo threadwatch logo seochat tools logo