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.

asked Jul 15, 2011 at 1:29
3
  • Do you want to know how to pass a std::vector to a C function, or how to emulate a std::vector in a C function? Commented Jul 15, 2011 at 1:38
  • @Chris. The former. I want to present the buffers in a dynamic array ( since I am using C++ std::vector is what came to my mind). Commented Jul 15, 2011 at 1:46
  • @user754425. Isn't that the latter then? Commented Jul 15, 2011 at 1:50

3 Answers 3

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...

answered Jul 15, 2011 at 1:50
Sign up to request clarification or add additional context in comments.

9 Comments

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.
@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??
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 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++.
@Jesus, Chris: Yes exactly, I'm not passing a std::vector, I'm passing a char *, just a pointer to a dynamically allocated array...
|
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

answered Jul 15, 2011 at 1:50

Comments

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.

answered Jul 15, 2011 at 1:36

1 Comment

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.