Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

define my own type

Posted on 2003-03-06
11
Medium Priority
?
215 Views
Last Modified: 2011-04-14
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.
0
Comment
Question by:mr_o_uk
  • 5
  • 5
11 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8082423
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
 
LVL 1

Expert Comment

by:cybeonix
ID: 8082476
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
 

Author Comment

by:mr_o_uk
ID: 8082537
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mr_o_uk
ID: 8082540
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
 
LVL 6

Expert Comment

by:gj62
ID: 8082699
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
 
LVL 6

Expert Comment

by:gj62
ID: 8082750
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
 

Author Comment

by:mr_o_uk
ID: 8088800
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
 
LVL 6

Expert Comment

by:gj62
ID: 8088854
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
 

Author Comment

by:mr_o_uk
ID: 8089050
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
 
LVL 6

Accepted Solution

by:
gj62 earned 300 total points
ID: 8089190
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
 

Author Comment

by:mr_o_uk
ID: 8089222
think im going to use a 64 and ignore the rest! seems like the least hassle!

thankyou.

Jon.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question