views:

206

answers:

7

I have a char** that I frequently need to insert into or perform a lookup. It is very tedious to realloc(), malloc() the array and insert strings.

Is there any standard way that I can add strings to or do lookups in a char**? I guess I'm looking for something like string, but using char**'s instead.

A: 

It's not clear what question you're trying to ask. If you want a sequence of strings, perhaps the Seq_T type from Dave Hanson's C Interfaces and Implementations will do the trick---it's a collection of very helpful data structures written in C, and it manages to grow and shrink memory for you as you insert and remove items.

Norman Ramsey
+1  A: 

Sounds like you want to use something like an STL List or Boost Array of char*.

Note that STL Vectors are not preferable if you need to insert or remove elements from your array.

Parappa
+5  A: 

If you're frequently inserting into this structure, you shouldn't be using a char** at all; an array isn't a suitable data structure for these kinds of operations. Consider a std::vector<string> or something similar if possible.

John Feminella
A: 

If this is plain ol' C -

I haven't seen any standard ways to do this, but that being said, I've done this a lot in my C routines. It's actually fairly straightforward to make a series of functions to do the manipulations you want on char* and char** values.

For me, it was just a matter of building a (small) library of routines. I started with the routines for the memory management and all of the char* operations I wanted. Then used those in making the routines for inserting into char**, etc.

For C++, you should consider refactoring to use std::vector or some other similar data structure. It will be much nicer.

Reed Copsey
In plain C you can write a linked-list and use that.
Parappa
True. A linked list can be a useful way of handling this. With the right routines, though, just doing the pointer setting into a new array as needed is pretty fast and easy, too.
Reed Copsey
A: 

See this info : What is ** in C++?

lsalamon
A: 

if C++ you can use STL but be warned that STL's cross platform support is iffy, which may not matter to you.

if C then you probably want to create a data structure to hold all your strings and create insert, remove, modify functions on that data structure. you should be able to find plenty of open source code for the data structure of your choice.

either way to select a proper data structure you need to consider your lookup and insertion patterns, how often do you insert? how often do you "look up"? is the look up done by searching for a matching string or can you do something more intelligent / faster? basically more information is needed.

for example if the strings are all unique and your dealing with lots of strings it makes sense to calculate a hash of the string and store them in something like a RB-tree keyed on that hash. if your never getting more than 10 strings it may not make sense to do this or maybe the application is such that you can simply assign an ID to a string and use the index as a look up key.

there are lots of options.

A: 

I agree with the (many!) other who have suggested that another data structure is probably better for this situation, but if you must use char** for some reason, the usual heuristic is to

  1. maintain a current length and available length at all times (say in a struct) and
  2. always increase the buffer size by a factor of two (or more) when reallocing (which results in an amortized O(n) time for allocation and coping).
dmckee