Home Help Center

I need char pointers (char *)

catalincatalin Member ✭✭
Hi!

I have C/C++ library functions which take as parameter and/or return a char *, so I want to declare Slice operations which would pass such a char * from the server to the client.

The closer I come is to declare a Slice type like sequence<byte> MyType; and then declare operation which take parameters and/or return results of MyType.

I see from the definitions generated by the slice2cpp compiler that sequence<byte> is mapped over to ::std::vector< ::Ice::Byte>. so my question would be how do I type cast to and from ::std::vector< ::Ice::Byte> to char *?

Any demos which do this?

Thanks,
Catalin

Comments

  • marcmarc FloridaAdministrators, ZeroC Staff Marc LaukienOrganization: ZeroC, Inc.Project: The Internet Communications Engine ZeroC Staff
    You can cast a vector<Byte> to a char* as follows:

    std::vector<Byte> bytes;
    char* p = reinterpret_cast<char*>(&bytes[0]);

    However, you cannot cast a char* to a vector<Byte>. You would have to construct a new vector<Byte>, like this:

    char* p = ....
    int len = .... // Number of chars
    std::vector<Byte> bytes;
    bytes.resize(len);
    std::copy(reinterpret_cast<Byte*>(p), reinterpret_cast<Byte*>(p) + len, bytes.begin());

    You need the reinterpret_cast because Ice::Byte maps to unsigned char, not to char.

    However, I don't recommend any of this, because it's a hack. You should instead convert between char* and std::string.
  • bernardbernard Jupiter, FLAdministrators, ZeroC Staff Bernard NormierOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    If you wonder why
    std::vector<Byte> bytes;
    char* p = reinterpret_cast<char*>(&bytes[0]);

    is correct, see http://www.comeaucomputing.com/iso/lwg-defects.html#69

    Cheers,
    Bernard
  • catalincatalin Member ✭✭
    Yes, it says also in "The C++ Standard Library" that vectors are in practice implemented in contiguous memory areas (even though the C++ standard is unclear about this), so it makes it possible to treat them like char pointers.

    I was afraid I would need the typecast from char* to vector, which I had a feeling could not be done without a copy operation, but so far I didn't. As a matter of fact it seems to me that the typecast from vector to char pointers is _always_ enough to implement operations using the sequence<byte> type. Does anyone have examples of situations when this is not true?

    Thanks for helping out again.
    Catalin
  • michimichi Member Michi HenningOrganization: Triodia TechnologiesProject: I have a passing interest in Ice :-) ✭✭✭
    Originally posted by catalin
    Yes, it says also in "The C++ Standard Library" that vectors are in practice implemented in contiguous memory areas (even though the C++ standard is unclear about this), so it makes it possible to treat them like char pointers.

    That information is out of date. The latest revision of the standard makes it mandatory for vector to store its elements in contiguous storage, so taking the address of an element and treating the vector as a buffer is perfectly standards-conforming.

    I was afraid I would need the typecast from char* to vector, which I had a feeling could not be done without a copy operation, but so far I didn't. As a matter of fact it seems to me that the typecast from vector to char pointers is _always_ enough to implement operations using the sequence<byte> type. Does anyone have examples of situations when this is not true?

    sequence<byte> translates to vector<unsigned char). You can get away with treating such a vector as a buffer of char (instead of a buffer of unsigned char) because the standard guarantees that char and unsigned char have the same size, namely 1. Just be careful if you do something like right-shifting one of the characters in the sequence: if the unsigned char value is > 127, you will get sign extension.

    Cheers,

    Michi.
  • rdilipkrdilipk Member Dilip RanganathanOrganization: 3MProject: None ✭✭
    That information is out of date. The latest revision of the standard makes it mandatory for vector to store its elements in contiguous storage, so taking the address of an element and treating the vector as a buffer is perfectly standards-conforming.

    A quote to that effect can be seen here from BS himself:
    http://www.talkaboutprogramming.com/group/comp.lang.c++/messages/782519.html
Sign In or Register to comment.