array to container

K

keith

Hi all,
I'm one of those with about twenty years of experience writing in C,
who is hacking his way through the paradigm shift toward C++, so bear
with me here...

I'm trying to figure out the 'least evil' way of dealing with the
following situation. I have a library of functions written in C (as
it happens, it's an encryption library), which is written in C. A
large number of the functions in that library expect arguments of type
'unsigned char *', i.e. arrays of binary data.

I am writing a C++ application which needs to use this library, but I
am told that 'arrays are evil; bad things will happen to you', so I
looked at the standard containers. If in my application I use vectors
of unsigned char, how do I pass the contained data to the C library
functions? Or should I take the view that if the library wants arrays
of unsigned chars, that's what I should be using and to hell with
being evil?!

I imagine this particular wheel has been invented many many times, so
the wisdom of those who have gone before would be appreciated.
 
S

Stefan Naewe

Hi all,
I'm one of those with about twenty years of experience writing in C,
who is hacking his way through the paradigm shift toward C++, so bear
with me here...

I'm trying to figure out the 'least evil' way of dealing with the
following situation. I have a library of functions written in C (as
it happens, it's an encryption library), which is written in C. A
large number of the functions in that library expect arguments of type
'unsigned char *', i.e. arrays of binary data.

I am writing a C++ application which needs to use this library, but I
am told that 'arrays are evil; bad things will happen to you', so I
looked at the standard containers. If in my application I use vectors
of unsigned char, how do I pass the contained data to the C library
functions? Or should I take the view that if the library wants arrays
of unsigned chars, that's what I should be using and to hell with
being evil?!

I imagine this particular wheel has been invented many many times, so
the wisdom of those who have gone before would be appreciated.

void your_c_function(unsigned char* p, size_t len);

std::vector<unsigned char> vuc;
/// populate vuc;

your_c_function(&vuc[0], vic.size());


Regards,
Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,174
Latest member
RonnyLoren

Latest Threads

Top