2
  1. Is there portable unsigned long in C++?
  2. Is there a way to make a portable structures?

On my home machine unsigned long is 8 bytes. On work machine its 4 bytes. When you start at home applications that are written on work there is a problem of correctness of data transmitted over the network.

Thanks.

nick
  • 643
  • 1
  • 5
  • 11

4 Answers4

8

#include <stdint.h> for the following typedefs:

  • (u)int8_t
  • (u)int16_t
  • (u)int32_t
  • (u)int64_t

While the last may or may not be available, depending on the platform.

Xeo
  • 129,499
  • 52
  • 291
  • 397
  • They all may or may not be available, depending on the platform. And they are available only to C99 implementations or as an extra (unreliable) feature. – pmg Jun 16 '11 at 21:24
  • @Xeo: This is of little or no help with network transparency. – Omnifarious Jun 16 '11 at 21:25
  • In linux with a fairly modern gcc you can use that header without problems. In VC is not included as default but there are headers available, see http://stackoverflow.com/questions/126279/c99-stdint-h-header-and-ms-visual-studio – javier-sanz Jun 16 '11 at 21:28
  • @Omni: Sure, but the question wanted types that are portable in size, so I gave that. :) Since endianess is a general problem with network related stuff, you'd need to work around that anyways. – Xeo Jun 16 '11 at 21:30
  • 2
    @fco: VC10 at least has ``. – Xeo Jun 16 '11 at 21:30
  • This is C++, not C -- include `cstdint`. – ildjarn Jun 16 '11 at 21:57
  • @ildjarn: That will be a standard header in C++0x, but isn't in C++03. Current C++ implementations may or may not have it, but all implementations that also support C99 have ``, so I regard that as more portable. – Mike Seymour Jun 16 '11 at 23:30
5

Then don't do that. Use something like protocol buffers to transfer stuff over the network. Dumping structures into file descriptors is just a bad idea all around and you shouldn't be doing it.

You can use <stdint.h> if you have C99 compliant C implementation to make sure that the types you're using are big enough to hold the data you're sending. But even that isn't enough as your work machine and home machine might have a different endianess or any number of other problems. <stdint.h> is great for making sure your variables are big enough to hold the full range of values that they need to hold, but they are not the tool for making sure you can transfer your data over the network with no headaches.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
2

As others have mentioned, using standard data sizes will work (e.g. from stdint.h.)

You can make structures more portable by using those data types in conjunction with some techniques used to make data structures more portable, such as data structure alignment. Typically, this involves enforcing each field to be a certain number of bytes and then copying the values into those bytes before transmitting them over a network, writing them to a file, etc.

Tom
  • 18,685
  • 15
  • 71
  • 81
0

Most platforms have typedefs called uint32_t, uint64_t, etc. that you can use. As far as I know these are not standard, but every platform I've used had them.

HighCommander4
  • 50,428
  • 24
  • 122
  • 194
  • 3
    They were standardized by [C99](http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf). – pmg Jun 16 '11 at 21:25