Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a std::vector in a C function

Tags:

c++

c

stdvector

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.

like image 891
user754425 Avatar asked Jul 15 '11 01:07

user754425


People also ask

Can I use vector in C?

Vectors are a modern programming concept, which, unfortunately, aren't built into the standard C library. Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.

Can you use vector in a function C++?

Vectors as return valuesYes, functions in C++ can return a value of type std::vector . Let us see an example of how this can be used in practice.

What is std::vector used for?

1) std::vector is a sequence container that encapsulates dynamic size arrays. 2) std::pmr::vector is an alias template that uses a polymorphic allocator. The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements.

How do you write a std::vector?

double p[] = {1, 2, 3, 4, 5}; std::vector<double> a(p, p+5); Here we use another constructor provided by vector. It takes two parameters: a pointer to the first element of a C-style array and a pointer to one past the last element of that array. It will initialize the vector with a copy of each element in the array.


2 Answers

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

like image 137
Darren Engwirda Avatar answered Nov 13 '22 09:11

Darren Engwirda


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

like image 31
John Doucette Avatar answered Nov 13 '22 08:11

John Doucette