473,396 Members | 1,777 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,396 software developers and data experts.

Fixed sized datatypes.

Hi I'm currently doing a project in my free time. One problem I ran into
is that things like short, int, and long all have different meanings as to
size on different platforms.

In this program it's pretty important that some values only be 16-bits in
length. Is there a way to do this and make it portable?

Thanks in advance,
JB
Jul 22 '05 #1
10 1583
"Justin Barrett" <ju****@ezrs.com> wrote in message
news:pa****************************@ezrs.com...
Hi I'm currently doing a project in my free time. One problem I ran into
is that things like short, int, and long all have different meanings as to
size on different platforms.

In this program it's pretty important that some values only be 16-bits in
length. Is there a way to do this and make it portable?


You can create a platform-specific header file containing typedefs for
fixed-sized types, e.g.,
typedef short Int16;
typedef unsigned short Uint16;

Use only the Int16 or Uint16 types in your code where you need 16-bit types.
When you move to a different platform, alter the header file as necessary
and re-compile.

DW

Jul 22 '05 #2
Justin Barrett wrote:
Hi I'm currently doing a project in my free time. One problem I ran into
is that things like short, int, and long all have different meanings as to
size on different platforms.

In this program it's pretty important that some values only be 16-bits in
length. Is there a way to do this and make it portable?


'unsigned short' is guaranteed to have _at_least_ 16 bits. If you need
_exactly_ 16 bits, you are out of luck. There are platforms out there
that simply don't have any types smaller than 32 bits. Your code could
never be ported there. If you don't care about that, you can settle on
making it "portable" to only those platforms that do have _a_ type which
is exactly 16 bits. Once you decide that that's what you want, you simply
create a header with conditionally compiled sections each of which defines
its own "justin_16bit_signed" type:

#ifdef WIN32
typedef short justin_16bit_signed;
typedef unsigned shot justin_16bit_unsigned;
#else
....

Victor
Jul 22 '05 #3
Victor Bazarov wrote:
#ifdef WIN32
typedef short justin_16bit_signed;
typedef unsigned shot justin_16bit_unsigned;
#else
...


This should be conditionalized on the compiler, not the OS.

#ifdef _MSC_VER
....

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #4
David White wrote:
You can create a platform-specific header file
containing typedefs for fixed-sized types, e.g., typedef short Int16;
typedef unsigned short Uint16;

Use only the Int16 or Uint16 types in your code where you need 16-bit types.
When you move to a different platform,
alter the header file as necessary and re-compile.


This is very *bad* advice.
Use the [C99] standard header file

#include <stdint.h>

instead.
Then your code will port to every platform
that supports a C99 compiler
and integers of the sizes that you need.
Jul 22 '05 #5
Thanks for all of your advance. It really helped me out.

Justin

Jul 22 '05 #6
"Pete Becker" <pe********@acm.org> wrote...
Victor Bazarov wrote:
#ifdef WIN32
typedef short justin_16bit_signed;
typedef unsigned shot justin_16bit_unsigned;
#else
...


This should be conditionalized on the compiler, not the OS.

#ifdef _MSC_VER
...


I do not agree, but I am not going to argue. It's up to the
programmer what to conditionalize on. For all we know it could
be conditionalized on some custom macro defined by the programmer
in the project settings.

V
Jul 22 '05 #7
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:cq**********@nntp1.jpl.nasa.gov...
David White wrote:
You can create a platform-specific header file
containing typedefs for fixed-sized types, e.g.,
typedef short Int16;
typedef unsigned short Uint16;

Use only the Int16 or Uint16 types in your code where you need 16-bit types. When you move to a different platform,
alter the header file as necessary and re-compile.


This is very *bad* advice.


Why is it "*bad*" advice? It would work perfectly well. I agree that if
there's support for fixed-sized types provided by the language standard,
then that would be preferable, but your program isn't going to suddenly
crash or disappear in a puff of smoke if you follow my advice.
Use the [C99] standard header file

#include <stdint.h>

instead.
Then your code will port to every platform
that supports a C99 compiler
and integers of the sizes that you need.


Is this a C or C++ newsgroup? I can find no mention of stdint.h in the ISO
14882 C++ standard.

DW

Jul 22 '05 #8
On Tue, 28 Dec 2004 16:31:31 -0600, Justin Barrett <ju****@ezrs.com>
wrote in comp.lang.c++:
Hi I'm currently doing a project in my free time. One problem I ran into
is that things like short, int, and long all have different meanings as to
size on different platforms.

In this program it's pretty important that some values only be 16-bits in
length. Is there a way to do this and make it portable?

Thanks in advance,
JB


As at least one other poster suggested, do a Google search for the C
standard <stdint.h> header. Now others have pointed out that this was
added in the 1999 update to the C language standard, and is not
actually part of the C++ standard at all, and they are correct but
somewhat short-sighted.

But without a doubt it will be added to the next major update to the
C++ standard, probably as both <stdint.h> and <cstdint.h>. And right
now, quite a few compilers supply this header already, and it works
when they compile C++ programs as well as when compiling C programs.

Even if you can't find one already put together for your
implementation, you should either be able to write one from scratch or
find one similar enough that you can modify it.

Assuming that your C++ implementation provides 2's complement integer
types with widths of exactly 8, 16, and 32 bits, and I'll bet it does,
you can write your own <stdint.h> header that will be 100% C++
standard conforming except for the 64 bit integer types.

It's not that hard at all. I've done it for several C and C++
compilers.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #9
"Jack Klein" <ja*******@spamcop.net> wrote...
[...]
But without a doubt it will be added to the next major update to the
C++ standard, probably as both <stdint.h> and <cstdint.h>. [...]


Probably as both <stdint.h> and <cstdint> (no .h)

V
Jul 22 '05 #10
Victor Bazarov wrote:
"Pete Becker" <pe********@acm.org> wrote...
Victor Bazarov wrote:
#ifdef WIN32
typedef short justin_16bit_signed;
typedef unsigned shot justin_16bit_unsigned;
#else
...


This should be conditionalized on the compiler, not the OS.

#ifdef _MSC_VER
...

I do not agree, but I am not going to argue. It's up to the
programmer what to conditionalize on. For all we know it could
be conditionalized on some custom macro defined by the programmer
in the project settings.


The point still remains, however: the size of an integral type is
determined by the compiler, so whatever name is used for a magic macro,
ultimately the choice has to be based on the compiler that's being used.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #11

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

Similar topics

10
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the...
179
by: SoloCDM | last post by:
How do I keep my entire web page at a fixed width? ********************************************************************* Signed, SoloCDM
21
by: Arthur Connor | last post by:
I want to embed some source code lines in an article on a web page. As usual I want to use a fixed sized font for this souce code lines (read: the letters should have all the same width). The...
98
by: Pamel | last post by:
I know this must have been asked elsewhere, but I cannot find it. There is a piece of text on my web page that I don't want browsers to resize. IE won't resize it if I specify the size in px, but...
11
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
12
by: JB | last post by:
Hi All, Is it acceptable to use a fixed width vertical navigation column within a fluid 2 or 3 column layout? Example. Left Column (navigation) fixed width of say 180px Right Column (main...
1
by: O.B. | last post by:
In the example below, I'm trying to convert a fixed byte array to a string. I get an error about needing to use "fixed" but I have no clue where to apply it. Help? using System; using...
4
by: Jack | last post by:
Hi, I do a webrequest and it returns some text data in a stream. I want to put this text data into a string. I've got it working just fine, but I have to put the text data into into a...
3
by: kaffekopp | last post by:
Hi, Since this is the first time for me using fixed size arrays, i would appreciate some guidance to solve this question. First of all, these structures are to be used: struct song { char ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.