5

A C function expects an array of buffers to be in scope at runtime. e.g.

char values[x][y]

The C function will populate the buffers
I would like to use a dynamic array so I don't have to hard code the dimensions
How do I use a std::vector in this situation?

Just to be clear, I am using C++. The C function is contained in a library that I cannot modify.

user754425
  • 51
  • 1
  • 1
  • 3

3 Answers3

11

If you just want to pass the dynamic array encapsulated in a std::vector to a c routine you can pass a pointer to the head of the underlying array as:

std::vector<char> myvector;
// size-up myvector as needed
foo(&myvector[0]); // pass a pointer to start of myvector to function foo

The c++ standard ensures that the underlying array in std::vector is always contiguous.

Hope this helps.

EDIT: While the declaration char values[x][y] creates an "array of arrays" the memory for values will actually just be a contiguous block, essentially char linear_values[x * y].

If you size your std::vector to include a count of x * y elements it should give you the same underlying dynamically allocated array space.

The c function will access the array in row-major order, so the first row of elements will come first, followed by the second full row etc...

Darren Engwirda
  • 6,915
  • 4
  • 26
  • 42
  • It does its a char type, and you can just forward declare the function or method prototype and the linker will link the call into the c obj file. – Chad Jul 15 '11 at 01:54
  • @Jesus: It's been stated that we're dealing with a `c++` compiler. I assume that we're trying to get some `c++` code to call to a `c` library, but with everything compiled as `c++`. Is this the case?? – Darren Engwirda Jul 15 '11 at 01:55
  • I believe he said that it is a C function which means it will be compiled with gcc in a .c file which is why he's asking if you can pass the vector a straight c function – Jesus Ramos Jul 15 '11 at 01:57
  • @Jesus Wait, `foo` is just getting a `char *` here. That should be valid C. @user754425 also said in a comment under his own question that he is using C++. – Chris Frederick Jul 15 '11 at 01:57
  • @Jesus, Chris: Yes exactly, I'm not passing a `std::vector`, I'm passing a `char *`, just a pointer to a dynamically allocated array... – Darren Engwirda Jul 15 '11 at 01:58
  • @Darren Engwirda. You are absolutely correct. We are dealing with a C++ compiler. I have just edited my question to make this clear. – user754425 Jul 15 '11 at 01:59
  • That's odd in firefox its not rendering the [0] part, but in Chrome it displays fine, I shall delete my old comment – Jesus Ramos Jul 15 '11 at 01:59
  • FWIW, I think that Scott Meyers mentioned this in one of his Effective C++ series, too. – Chris Frederick Jul 15 '11 at 01:59
  • @Darren Engwirda. Your answer does not pass a two dimension vector. See my example in the question. It is an array of array of chars – user754425 Jul 15 '11 at 02:09
3

C doesn't have standard data structures libraries.

If you really want all the functionality of a vector, and it's not for something critical, you can probably find someone's pet implementation of a straight C vector online and just use that.

If it is critical, write your own. It's not too hard, and can be quite useful.

If you just want a dynamically growing array, it's easy to emulate that behavior of a vector using the realloc function, which extends the dimensions of a heap-allocated array. Start with a small array, and grow as needed when you reach the end. It's more efficient to grow in big chunks, but if you have some idea of what your data looks like you could grow it in a different way. A common method is doubling the array size every time you run out.

You can get the details of realloc at:

http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/

or, on a *nix system:

man realloc

John Doucette
  • 4,370
  • 5
  • 37
  • 61
0

You can't.

By definition, C knows nothing of any of the required components of a std::vector, including, but not limited to:

  • C does not have namespaces, so it can't understand the std namespace.

  • C does not have templates, so it can't understand the std::vector<T> type.

Essentially, you need what looks like a C function, but that is, for all intents and purposes, a C++ function.

The simplest way to achieve this is probably to write what looks like a C function, using C++, and running the whole mess through a C++ compiler rather than a C compiler.

Williham Totland
  • 28,471
  • 6
  • 52
  • 68
  • The C function is in a library. I can only use it as is. I thought if it is possible to pass a vector to C functions ( &v[0] ) then what I am trying to do is perhaps doable I just don't know how – user754425 Jul 15 '11 at 01:50