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

D

darren

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
 
J

Jim Langston

darren said:
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.
 
J

James Kanze

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
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.
 
A

Adem24

darren said:
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.
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top