• C

define my own type

I basically want to define my own type, like int, but i want it to be of a specific bit size. i need to define blocks of memory of 32, 48, 56 and
64.
I could use int for block32 and __int64 for block 64, but cant get one of 48 or 56. (unless i can combine __int32 and __int16. I know that i can
use malloc to define a block of the right size, but i dont want to have to do this everythime i declare something. So i want to be able to define
it in an .h file.

I want to put it in a union such as...

typedef union {
     int i;
     float r;
     char *s;
     void *p;
     __int32 b3;
     block48 b4;
     block56 b5;
     __int64 b6;
} tokenvalue_t;


where my blocks are defined as memory blocks of the right size.

tried stuff like this...

#define block48 (nom) (malloc(nom, 48));

dont know if i have described this well enough, but hey, hope you can help!

Many thanks
J.
mr_o_ukAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gj62Commented:
You're compiler won't give you just 48 bits you know - are you trying to pack structures as tightly as possible, or just twiddle bits?  What machine are you targeting?
0
cybeonixCommented:
You can define the type using a structure.

struct mytype
{
  long b1;
  long b2;
  long b3;
  long b4;
  long b5;
};

typedef struct mytype MYTYPE;

Then use it as a typedef data..

MYTYPE *data;

defining a data and allocating the memory for it are going to be separated.

mydata = malloc(sizeof(MYTYPE));


Of course you don't have to allocate the memory, you can throw it on the stack, provided you aren't using too many large structures.

MYTYPE data;
data.b1 = ...
data.b2 = ...
0
mr_o_ukAuthor Commented:
i want to twiddle with them!! i want to do bit manipulations with them. will i just have to declare it as a 64 bit block and ignore the last 16 bits?

using malloc(var, 48) - will that not give me a block of the right size? is there not a way i can put it in a macro or something to define a block of the right size?

if i define it as in MYTYPE, ill have to treat each block of 32 and 16 seperately and join them together and stuff, bit operations wouldnt be that easy. it would be so much easier if they were together, it would be easier to use 64 and ignore the last 16 bits.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

mr_o_ukAuthor Commented:
i want to twiddle with them!! i want to do bit manipulations with them. will i just have to declare it as a 64 bit block and ignore the last 16 bits?

using malloc(var, 48) - will that not give me a block of the right size? is there not a way i can put it in a macro or something to define a block of the right size?

if i define it as in MYTYPE, ill have to treat each block of 32 and 16 seperately and join them together and stuff, bit operations wouldnt be that easy. it would be so much easier if they were together, it would be easier to use 64 and ignore the last 16 bits.
0
gj62Commented:
First, you have to see if your compiler will allow you to turn off structure padding - most do, but you better check.  If not, you can't assume the location of members within a struct...

Here's a good discussion about the dangers of bit-twiddling in C...

http://steve-parker.org/notes/esr/first-class-bitfields.html



0
gj62Commented:
You can do something like:

struct
{
    unsigned short icon : 8; /* 8 bits */
    unsigned short color : 4;  /* 4 bits, etc */
    unsigned short underline : 1;
    unsigned short blink : 1;
} screen[25][80];

The array contains 2,000 elements. Each element is an individual structure containing four bit-field members: icon, color, underline, and blink. The size of each structure is two bytes.

However, once again, you can't assume alignment between structs, nor even within the struct...

Alot of this is machine dependent as well - what machine/compiler are you using?
0
mr_o_ukAuthor Commented:
can you explain the :8 part of the code?

is it of form
unsigned varname:# of bits;
???

does that make it an unsigned integer of 8 bits? do they have to be in certain multiples? does it have a maximum?
0
gj62Commented:
C Bit Fields

In addition to declarators for members of a structure or union, a structure declarator can also be a specified number of bits, called a “bit field.” Its length is set off from the declarator for the field name by a colon. A bit field is interpreted as an integral type.

Syntax
struct-declarator :
declarator
type-specifier declarator opt : constant-expression

The constant-expression specifies the width of the field in bits.

The type-specifier for the declarator must be unsigned int, signed int, or int, and the constant-expression must be a nonnegative integer value.

If the value is zero, the declaration has no declarator.

Arrays of bit fields, pointers to bit fields, and functions returning bit fields are not allowed.

The optional declarator names the bit field. Bit fields can only be declared as part of a structure. The address-of operator (&) cannot be applied to bit-field components.

Unnamed bit fields cannot be referenced, and their contents at run time are unpredictable. They can be used as “dummy” fields, for alignment purposes. An unnamed bit field whose width is specified as 0 guarantees that storage for the member following it in the struct-declaration-list begins on an int boundary.

Bit fields must also be long enough to contain the bit pattern. For example, these two statements are not legal:

short a:17;        /* Illegal! */
int long y:33;     /* Illegal! */

So, they have minimum use if you really want a single type(no members) with 48 accessible bits - but it is ok if you want to have a struct with members that gets you there...
0
mr_o_ukAuthor Commented:
So it looks like my choices are

to declare it as an 64 bit unsigned int, and just ignore the last 16 bits, pretend theyre not there,

or to use
unsigned __int64 myblock:48;
or will it only use int (as in 32 bits), and nothing bigger?

thanks.
J.
0
gj62Commented:
Not sure if bitfields can be applied larger than an int, and like it says, you can only apply them inside a struct...

e.g.

struct bits
{
  int firstBits:32
  int secondBits:16;
}mybits;

Note that the size will still be 2 ints...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mr_o_ukAuthor Commented:
think im going to use a 64 and ignore the rest! seems like the least hassle!

thankyou.

Jon.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.