Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does "layout-compatible with C" mean?

Tags:

c++

c

stl

vector

It is said that stl vector is "layout-compatible with C". Where can I find the definition of "layout-compatible with C"?

like image 291
Leon Avatar asked Mar 16 '11 18:03

Leon


2 Answers

This means that, as long as the vector is not empty, &vector.front() will give you a pointer to a contiguous array of objects, which could be passed to a C API that expects such an array.

like image 118
Mike Seymour Avatar answered Nov 07 '22 14:11

Mike Seymour


It means that the contents of the vector will be laid out in memory the same way they would be in a C array of the same type. That means that if you have a C function that expects to receive a pointer to an array of some type, you can use a vector and pass the function a pointer to the first element of the vector.

like image 39
Rob Kennedy Avatar answered Nov 07 '22 14:11

Rob Kennedy