is the syntax for fixed-length integers the same for C and C++?

Discussion in 'C++' started by darren, Jun 30, 2008.

  1. darren

    darren Guest

    Hi

    I have an assignment that is in C and, for an API call, asks for a
    uint_16 and uint_32 in one of its functions.

    In my C++ code i've been using uint16_t and uint32_t for fixed length
    integers. Are these two Types compatible?


    Also, if i make a struct of a few variables of type uint_xxx_t, can I
    be assured that this struct will be the same size on either end of the
    wire when sent over a network (note, the hosts may not be the same
    platform). I know that some architecture's treat int's as different
    lengths, but I thought that is why we use uint_xxx_t, to solve that
    poblem.

    Thanks
     
    darren, Jun 30, 2008
    #1
    1. Advertising

  2. darren

    Jim Langston Guest

    "darren" <> wrote in message
    news:...
    > Hi
    >
    > I have an assignment that is in C and, for an API call, asks for a
    > uint_16 and uint_32 in one of its functions.
    >
    > In my C++ code i've been using uint16_t and uint32_t for fixed length
    > integers. Are these two Types compatible?


    There are no guarantees, but most likely, yes, they are the same. To find
    out search through your code where uint32_t and uint_32 are defined and make
    sure they are the same. For all likelyhood they will be the same. It's a
    good idea to check though.

    > Also, if i make a struct of a few variables of type uint_xxx_t, can I
    > be assured that this struct will be the same size on either end of the
    > wire when sent over a network (note, the hosts may not be the same
    > platform). I know that some architecture's treat int's as different
    > lengths, but I thought that is why we use uint_xxx_t, to solve that
    > poblem.


    Well, is it your code on the other end of the network also? If it's your
    code then it will be what you set it to. If it's not your code, you'll need
    to check the documentation to see what they expect the size of their ints to
    be.

    But, yes, uint_xxx_t should be the the same size on each architecture. I'm
    not positive it's declared in the standard, however, I don't have a copy of
    the standard handy.
     
    Jim Langston, Jul 1, 2008
    #2
    1. Advertising

  3. darren

    James Kanze Guest

    On Jul 1, 3:13 am, "Jim Langston" <> wrote:
    > "darren" <> wrote in message


    > news:...


    > > I have an assignment that is in C and, for an API call, asks for a
    > > uint_16 and uint_32 in one of its functions.


    > > In my C++ code i've been using uint16_t and uint32_t for fixed length
    > > integers. Are these two Types compatible?


    > There are no guarantees, but most likely, yes, they are the same.


    The C standard guarantees that uint16_t and uint32_t are two's
    complement integers of exactly the given length, with no
    padding. The next version of the C++ standard will make the
    same guarantee. Presumably, the byte order could differ between
    C and C++, but really, I don't see that happening.

    > To find out search through your code where uint32_t and
    > uint_32 are defined and make sure they are the same. For all
    > likelyhood they will be the same. It's a good idea to check
    > though.


    In C, they're defined in <stdint.h>. In the next version of the
    C++ compiler, they will be defined in <cstdint> (and in
    namespace std::); until then, just use the C header.

    > > Also, if i make a struct of a few variables of type
    > > uint_xxx_t, can I be assured that this struct will be the
    > > same size on either end of the wire when sent over a network
    > > (note, the hosts may not be the same platform). I know that
    > > some architecture's treat int's as different lengths, but I
    > > thought that is why we use uint_xxx_t, to solve that poblem.


    > Well, is it your code on the other end of the network also?
    > If it's your code then it will be what you set it to. If it's
    > not your code, you'll need to check the documentation to see
    > what they expect the size of their ints to be.


    But you can't send struct's over the wire. Or even int's. Only
    bytes. So you have to define a format, and conform to it (or
    use an existing format, like XDR).

    > But, yes, uint_xxx_t should be the the same size on each
    > architecture. I'm not positive it's declared in the standard,
    > however, I don't have a copy of the standard handy.


    Same size and same representation. But perhaps different byte
    order.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 1, 2008
    #3
  4. darren

    Adem24 Guest

    "darren" <> wrote:
    >
    > I have an assignment that is in C and, for an API call, asks for a
    > uint_16 and uint_32 in one of its functions.
    >
    > In my C++ code i've been using uint16_t and uint32_t for fixed length
    > integers. Are these two Types compatible?
    >
    > Also, if i make a struct of a few variables of type uint_xxx_t, can I
    > be assured that this struct will be the same size on either end of the
    > wire when sent over a network (note, the hosts may not be the same
    > platform). I know that some architecture's treat int's as different
    > lengths, but I thought that is why we use uint_xxx_t, to solve that
    > poblem.


    See the other replies.
    The easiest method IMO would be to convert all numbers to
    string format and transfer the string over the wire,
    and at the other side convert back to native format,
    ie. using sprintf() and sscanf() in C/C++ or using cout and cin in C++.
    So, for each struct type you would need also a unique id in stringformat.
    Transfer the uid and then the data...
    See also 'serialization' in your C++ documentation.
     
    Adem24, Jul 1, 2008
    #4
    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. Kwaj
    Replies:
    1
    Views:
    636
    David Bishop
    Apr 25, 2004
  2. Julian McMaster

    reading and parsing fixed length text file

    Julian McMaster, Nov 8, 2005, in forum: Java
    Replies:
    1
    Views:
    7,710
    Roedy Green
    Nov 8, 2005
  3. johnp
    Replies:
    4
    Views:
    3,695
    Toby Inkster
    May 23, 2005
  4. Andy Sy
    Replies:
    0
    Views:
    541
    Andy Sy
    Feb 3, 2006
  5. Michel Rouzic
    Replies:
    18
    Views:
    1,579
    Malcolm
    Dec 10, 2005
Loading...

Share This Page