Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > array to container

Reply
Thread Tools

array to container

 
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      06-28-2007

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.

 
Reply With Quote
 
 
 
 
Stefan Naewe
Guest
Posts: n/a
 
      06-28-2007
On 6/28/2007 12:15 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
container inside container in stl wolverine C++ 2 07-24-2006 03:08 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
std::transform container => std::abs(container) Steven T. Hatton C++ 4 12-05-2004 07:10 AM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments