0

I need a simple class to wrap a c style array of pointer strings, namely char **
I need minimal operations on my wrapper including adding elements( const or non const ), iterating, random access and querying size. I started using a std::vector like below but I don't like it for reasons described below
This is my start

struct CstrVector
{
   CstrVector(){}
    char *convertToC(const std::string & str)
    {
        char *c = new char[str.size()+1];
        std::strcpy(c, str.c_str());
        return c; 
    }
    void add( const char* c)
    {
       //??
    }
    void add(const std::string& str )
    {
        data.push_back(convertToC(str));
    }
    void add(const std::string& s, unsigned int pos )
    {
        if( pos < data.size())
        {
            data[pos] = convertToC(s);
        }
    } 
   ~CstrVector()
   {
     for ( size_t i = 0 ; i < data.size() ; i++ )
     {
        delete [] data[i];
     }
   }
   char ** vdata;
  std::vector<char*> data; // I want to replace this with char ** vdata
};

Why not use
std::vector<char*>
I don't see a benefit it since I still have to allocate and free the memory outside the container ( std::vector ) anyway
Why not use
std::vector<std:string>
because I want to be able to easily pass the array to a C function that will manipulate / change it contents.

7
  • 2
    You don't see a benefit in using std::vector? I can see one: you don't have to write it. Commented Aug 24, 2011 at 15:49
  • You said adding elements( const or non const ) but why would you add on a const ? Commented Aug 24, 2011 at 15:53
  • You don't mention std::vector< std::vector <char > > in your list of choices: this is probably cleaner and more efficient than std::vector<std:string> Commented Aug 24, 2011 at 15:54
  • @R. Martinho Fernandes I use std::vector all the time but I guess I wonder if it is worth it in this case. The vector does not even manage the memory for me. All I get is iteration and random access. Commented Aug 24, 2011 at 15:55
  • @MrLunchtime The nested vector will not have contiguous memory I think Commented Aug 24, 2011 at 15:56

3 Answers 3

2

You should be using

std::vector<std:string> 

If you want to pass char string to c functions you can simply do so using string::c_str

Sign up to request clarification or add additional context in comments.

7 Comments

Unfortunately string::c_str is const.
He's not trying to pass a C-string, he's trying to pass an array of C-strings, which is not as easily done. It could be encapsulated in a method that dynamically allocates an array of pointers and sets each of them to the c_str result.
@R. Martinho Fernandes: Yup, that's right, that would add a little overhead of copying it in a temp buffer before passing it on ahead.
@Als C functions cannot write to std::string. May be they can I just don't know how
@mohan: Ofcourse, they can't, they know nothing about string being a type. I am a little confused about your comment here and what you quote in the question.
|
2

You could still use the std::vector<char *>. The vector guarantees to store its contents in a consecutive manner, more precisely in an array. If you want to pass the char ** array to a C function you just have to pass the address of the first element in the vector:

void c_function(char ** data);

std::vector<char *> myvec;
...

c_function(&myvec[0]);

Notes:

  • You still have to do the memory management for the single char*s
  • this only works for std::vector<char*> and not for std::vector<std::string>!

Comments

1

Would Boost ptr_vector work for you?

2 Comments

I am not sure. Is boost::ptr_vector<char> same as std::vector<char*> ?
@mohan: it is similar, but deletes the objects that the contained pointers point to when it is destructed. More info: boost.org/doc/libs/1_47_0/libs/ptr_container/doc/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.