473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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
Jun 30 '08 #1
3 2470
"darren" <mi******@gmail.comwrote in message
news:20**********************************@i18g2000 prn.googlegroups.com...
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.
Jul 1 '08 #2
On Mon, 30 Jun 2008 15:25:51 -0700 (PDT), darren <mi******@gmail.com>
wrote in comp.lang.c++:
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.
These are not standard C data types, so we can only guess at what they
are. They are some types defined by the author(s) of the library you
are using.
In my C++ code i've been using uint16_t and uint32_t for fixed length
integers. Are these two Types compatible?
The types int#_t and uint#_t are standard C data types, added in the
1999, and later, versions of the C language standard. They will be
added to the next revision of the C++ standard, most likely finalized
in the next year or so. But they are not part of standard C++ yet.

As Jim Langston already suggested, find the headers with the typedefs
for the non-standard uint_16, etc., and those for the standard
C/semi-standard C++ uint16_t, etc., and see if they are aliases for
the same underlying types.

On today's common 32-bit desktop platforms, it is quite possible that
uint_32 could be an alias for "unsigned int" but uint32_t an alias for
"unsigned long". Or vice versa. That could result in a large number
of complaints from the compiler, depending on which headers were
included with which source code.
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.
You can't ever assume binary layout compatibility across a network,
especially if different platform are involved. Even if the sizes of
the individual members were the same, structure alignment and padding
could result in the structure being two different sizes on the
different platforms.

And the representation of the integer types could be different, even
if they have the same number of bits. The most common difference is
big- versus little-endian.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jul 1 '08 #3
"darren" <mi******@gmail.comwrote:
>
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.

Jul 1 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Andreas Neudecker | last post by:
Hello. I am relatively new to Python and I have a strange problem with some code. In a class the __call__ method gets parameters like this: class WhatsUp: __call__ ( self, var1,
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
3
by: PHil Coveney | last post by:
Hello, I am making calls into a legacy DLL. One function in this DLL expects as a parameter a struct with several fields, each of which is a 40-char array. The effect of this function is to copy...
0
by: David E. | last post by:
What is the syntax for a fixed (stream manipulation function) in a emacs environment?
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
2
by: rockstar_ | last post by:
Hello all- I'm developing a Content Management software for my own site, and possibly package and deploy to other sites (for friends, family, etc.) The content management software is combined...
4
by: daniel | last post by:
I'm not quite new to this language, but such error has never happened so frequently before. I have no idea what to do about it, because there's actually no syntax error at all !! I'm using...
11
by: Michael Spencer | last post by:
Announcing: compiler2 --------------------- For all you bytecode enthusiasts: 'compiler2' is an alternative to the standard library 'compiler' package, with several advantages. Improved...
3
by: bearophileHUGS | last post by:
Probably you have already discussed this topic, but maybe you can stand touching it again briefly. Maybe properties aren't used so often to deserve a specific syntax, but I don't like their syntax...
17
by: trose178 | last post by:
Good day all, I am working on a multi-select list box for a standard question checklist database and I am running into a syntax error in the code that I cannot seem to correct. I will also note...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.