Variable size...
I want to declare variables of exact sizes...
An int is always the size of a single register on the CPU you compile on. But I want to declare a signed integer that will always be 32 bits in size. How do I that? How do I do the same to create a 16-bit unsigned integer?
I once saw something like this...
int myVar:24;
... which was supposed to create a 24-bit signed integer. But I''ve yet to come across a compiler that doesn''t call syntax errors on that.
Please help me out?
VC++.NET(and 6.0, I assume) accept extensions like __int32, __int16 and __int64. Other compilers will have equivalent extensions. I suggest you wrap this with a typedef and an #if/#endif block.
The syntax you suggest comes from bitfields. For instance:
struct Flags
{
unsigned int is_keyword:1;
unsigned int is_extern:1;
unsigned int is_static:1;
};
This creates three variables of 1-bit each.
But bitfields are very implementation dependant and generally no assumptions can be made. For isntance, it is not known whether the bits above are packed or aligned on 1, 2, 4 or 8 byte bounderies.
Only integers can be declared as bitfields.
In general I would go for the solution used by most APIs. Create a header that contains #defines that check the current compiler and then defines new types to match the same sizes you want.
#ifdef _win32
#define INT32 int
#define INT16 short
#endif
and so on for other systems also.
struct Flags
{
unsigned int is_keyword:1;
unsigned int is_extern:1;
unsigned int is_static:1;
};
This creates three variables of 1-bit each.
But bitfields are very implementation dependant and generally no assumptions can be made. For isntance, it is not known whether the bits above are packed or aligned on 1, 2, 4 or 8 byte bounderies.
Only integers can be declared as bitfields.
In general I would go for the solution used by most APIs. Create a header that contains #defines that check the current compiler and then defines new types to match the same sizes you want.
#ifdef _win32
#define INT32 int
#define INT16 short
#endif
and so on for other systems also.
why did you forget the easiest solution?
short = 16 bit integer
long = 32 bit integer
double= 64 bit integer
Did I missunderstand the question or what?
short = 16 bit integer
long = 32 bit integer
double= 64 bit integer
Did I missunderstand the question or what?
quote:Original post by Peaceman
why did you forget the easiest solution?
short = 16 bit integer
long = 32 bit integer
double= 64 bit integer
Did I missunderstand the question or what?
Double is a floating point value not an integer, but it is true that it is a 64-bit floating point value.
I guess you don''t know C. Those sizes are correct on a Intel 32-bit PC. They are not true in general - and no sizes are given in the C and C++ standard.
For instance on the Digital Alpha 64-but arhitecture the following is the case:
short = 16 bit
int = 32-bit
long = 64-bit
or on a 80286 processor (if I recall correctly):
short = 16 bit
int = 16 bit
long = 16 bit
quote:
I guess you don''t know C. Those sizes are correct on a Intel 32-bit PC. They are not true in general - and no sizes are given in the C and C++ standard.
Yeah, you''re right about me not knowing C. I started with C++ so I didn''t actually quite see the problem :-D
Thanks for explaining it to me anyway.
Continuing Wyrframe''s question - is it possible to create custom sized variables (I don''t think anyone''s actually found an answer to it in this thread)? Let''s suppose I do want to create a 24-bit integer type variable with the very same properties as a regular integer? felonius, is there a way to make sure that my new-sized variable will behave correctly if I use it, for instance, in: fread(ptr, sizefo(myVar), n, stream)??
Does anyone know how the built in variables in C/C++ are defined?
Crispy
Does anyone know how the built in variables in C/C++ are defined?
Crispy
quote:Original post by Crispy
Continuing Wyrframe's question - is it possible to create custom sized variables (I don't think anyone's actually found an answer to it in this thread)? Let's suppose I do want to create a 24-bit integer type variable with the very same properties as a regular integer? felonius, is there a way to make sure that my new-sized variable will behave correctly if I use it, for instance, in: fread(ptr, sizefo(myVar), n, stream) ??
You can't create actual new basic types. The closest you get is by using bit-fields as part of a struct but as said there is no garantee that they are packed and it will probably include passing so if we have
struct myType
{
myVar:24;
};
then sizeof(myType) will likely be 4 or 8 depending on the compiler - not 3 as you might want.
and it certainly will not work with fread.
quote:
Does anyone know how the built in variables in C/C++ are defined?
The built-in types are built-in so they cannot be defined in the language itself, if that is what you mean. The only requirement in the C standard concerning size is that
sizeof(short) <= sizeof(int)
sizeof(int) <= sizeof(long)
sizeof(float) <= sizeof(double)
sizeof(double) <= sizeof(long double)
You cannot say anything about sizeof(char). You cannot assume it is 1.
[edited by - felonius on June 16, 2002 12:34:58 PM]
[edited by - felonius on June 16, 2002 12:35:36 PM]
quote:Original post by tsuraan
Doesn''t the C spec say that sizeof(char) returns 1? Or is that out of date?
You are correct. sizeof() always returns 1 for type char.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement