array to container

Discussion in 'C++' started by keith@bytebrothers.co.uk, Jun 28, 2007.

  1. Guest

    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.
     
    , Jun 28, 2007
    #1
    1. Advertising

  2. Stefan Naewe Guest

    On 6/28/2007 12:15 PM, 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
     
    Stefan Naewe, Jun 28, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vivi Orunitia
    Replies:
    11
    Views:
    4,482
    Martijn Lievaart
    Feb 4, 2004
  2. Maitre Bart
    Replies:
    2
    Views:
    527
    Maitre Bart
    Feb 11, 2004
  3. Steven T. Hatton
    Replies:
    4
    Views:
    3,910
    Rob Williscroft
    Dec 5, 2004
  4. Replies:
    4
    Views:
    805
    Daniel T.
    Feb 16, 2006
  5. wolverine
    Replies:
    2
    Views:
    455
    Marcus Kwok
    Jul 24, 2006
Loading...

Share This Page