Structure size

B

Bruce.

I would like to allocate a structure size of 1024 bytes but I want the
compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will automattically
make the total structure 1024 bytes without having to manually count the
bytes of the other members myself.

Is that possible in c++?

Thanks.

Bruce.
 
R

Richard Heathfield

Bruce. said:
I would like to allocate a structure size of 1024 bytes but I want the
compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will
automattically make the total structure 1024 bytes without having to
manually count the bytes of the other members myself.

Is that possible in c++?

Not portably. Implementations may insert arbitrary padding after any
structure member. In a pathological implementation, you might have to
give var4 a negative number of elements (which is of course
impossible).

If you need exactly 1024 bytes, allocate an array of unsigned char, 1024
bytes wide, and dip into its bits as and when you need to.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I would like to allocate a structure size of 1024 bytes but I want the
compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will automattically
make the total structure 1024 bytes without having to manually count the
bytes of the other members myself.

You could do something like this:

struct TheStruct {
int var1;
int var2;
int var3;
char var4[1024 - (3 * sizeof(int))];
};

(Notice also the lack of typedef, it's not needed unless you require C
compatibility).
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I would like to allocate a structure size of 1024 bytes but I want the
compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will automattically
make the total structure 1024 bytes without having to manually count the
bytes of the other members myself.

You could do something like this:

struct TheStruct {
int var1;
int var2;
int var3;
char var4[1024 - (3 * sizeof(int))];
};

As Mr. Heathfield pointed out, due to padding this might not work and
you could do as he said. However that's quite ugly, casting ranges of
memory to whatever you need. An alternative would be a union:
#include <iostream>

struct MyStruct {
int var1;
int var2;
int var3;
};

union MyUnion {
char buff[1024];
MyStruct theStruct;
};

int main()
{
MyUnion u;
u.theStruct.var1 = 1;
std::cout << sizeof(MyUnion);
}
 
R

Richard Heathfield

Erik Wikström said:
I would like to allocate a structure size of 1024 bytes but I want
the compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will
automattically make the total structure 1024 bytes without having to
manually count the bytes of the other members myself.

You could do something like this:

struct TheStruct {
int var1;
int var2;
int var3;
char var4[1024 - (3 * sizeof(int))];
};

And it might even work. But it might not. It depends on the
implementation.
(Notice also the lack of typedef, it's not needed unless you require C
compatibility).

It's not actually needed in C either, although admittedly its omission
does lead to wordier code.
 
L

LR

Bruce. said:
I would like to allocate a structure size of 1024 bytes but I want the
compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will automattically
make the total structure 1024 bytes without having to manually count the
bytes of the other members myself.

Is that possible in c++?

May I ask why you want to do that?

LR
 
B

Bruce.

LR said:
Bruce. said:
I would like to allocate a structure size of 1024 bytes but I want the
compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will
automattically make the total structure 1024 bytes without having to
manually count the bytes of the other members myself.

Is that possible in c++?

May I ask why you want to do that?

I'm creating a new message type, and the message buffers are hard coded at
1024 bytes. So I need var4 to be as big as possible but the entire
structure can not exceed 1024 bytes.

Bruce.
 
B

Bruce.

Richard Heathfield said:
Bruce. said:
I would like to allocate a structure size of 1024 bytes but I want the
compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will
automattically make the total structure 1024 bytes without having to
manually count the bytes of the other members myself.

Is that possible in c++?

Not portably. Implementations may insert arbitrary padding after any
structure member. In a pathological implementation, you might have to
give var4 a negative number of elements (which is of course
impossible).

In this case, portability is not a requirement and the compiler packing is
fixed a 1. I'm using MS Developer Studio C++ 2003. Does that change your
answer at all? Thanks.

Bruce.
 
R

Richard Heathfield

Bruce. said:

In this case, portability is not a requirement

Then you're probably better off in a newsgroup devoted to your
implementation, where implementation-specific expertise can be brought
to bear on your problem.
 
B

Bruce.

Richard Heathfield said:
Then you're probably better off in a newsgroup devoted to your
implementation, where implementation-specific expertise can be brought
to bear on your problem.

If I change it to this:

#pragma pack( 1 )

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

Now do you know of a portable way to accomplish what I want?

Bruce.
 
V

Victor Bazarov

Bruce. said:
Richard Heathfield said:
Then you're probably better off in a newsgroup devoted to your
implementation, where implementation-specific expertise can be
brought to bear on your problem.

If I change it to this:

#pragma pack( 1 )

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

Now do you know of a portable way to accomplish what I want?

'#pragma pack( 1 )' is non-portable. How can something portable
be based on non-portable something?

V
 
J

John Harrison

Bruce. said:
I would like to allocate a structure size of 1024 bytes but I want the
compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will automattically
make the total structure 1024 bytes without having to manually count the
bytes of the other members myself.

Is that possible in c++?

Thanks.

Bruce.

Since you aren't worried about padding bytes the obvious answer is

typedef struct
{
int var1;
int var2;
int var3;
char var4[ 1024 - 3*sizeof(int) ];
} MYSTRUCT;

john
 
B

Bruce.

John Harrison said:
Since you aren't worried about padding bytes the obvious answer is

typedef struct
{
int var1;
int var2;
int var3;
char var4[ 1024 - 3*sizeof(int) ];
} MYSTRUCT;

Thanks but my example was oversimplified to keep it brief. The actual
structures are much more complex, too complex for a solution like that.

I guess what I was looking for doesn't exist.

But thanks anyway.

Bruce.
 
B

Bruce.

Victor Bazarov said:
'#pragma pack( 1 )' is non-portable. How can something portable
be based on non-portable something?

Sorry. I haven't used non-windows compilers. A unix programmer once told
me that pragma pack worked everywhere. I guess not judging from your
response.

Bruce.
 
V

Victor Bazarov

Bruce. said:
Sorry. I haven't used non-windows compilers. A unix programmer once
told me that pragma pack worked everywhere. I guess not judging from
your response.

The preprocessor directive #pragma is defined to be implemenation-
specific way to control the compilation process. So, by definition,
it's implementation specific and you cannot rely on it being the same
or different, or even to exist in all the implementations.

V
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

LR said:
Bruce. said:
I would like to allocate a structure size of 1024 bytes but I want the
compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will
automattically make the total structure 1024 bytes without having to
manually count the bytes of the other members myself.

Is that possible in c++?

May I ask why you want to do that?

I'm creating a new message type, and the message buffers are hard coded at
1024 bytes. So I need var4 to be as big as possible but the entire
structure can not exceed 1024 bytes.

I may be missing something, but why do you need to fill the whole
buffer? Does it not just specify the maximum size and not also the minimum?
 
J

JohnQ

Bruce. said:
I would like to allocate a structure size of 1024 bytes but I want the
compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will automattically
make the total structure 1024 bytes without having to manually count the
bytes of the other members myself.

Is that possible in c++?

Thanks.

Bruce.

Define "MyStruct", ("Msg" in your scenario), as a composition of a header
and data:

#define MSG_SZ 1024

struct MsgHdr
{
__int32 var1;
__int32 var2;
__int32 var3;
};
const unsigned int MsgHdrSz = sizeof(MsgHdr);
const unsigned int MaxMsgDataSz = MSG_SZ - MsgHdrSz;

struct Msg
{
MsgHdr hdr;
char data[MSG_SZ - MsgHdrSz];
};

Use byte alignment, or be careful with the var types in the header so that
they align "nicely", and be conscious of the hardware you compile and deploy
on.

John
 
B

Bruce.

Erik Wikström said:
I may be missing something, but why do you need to fill the whole buffer?
Does it not just specify the maximum size and not also the minimum?

The data we need to pump through our message stream is many MB in size, but
our buffers are limited to (for example) to 1024 bytes. So the original
block has to be broken up in to many smaller bites. Each bite needs to be
the biggest possible for efficiency. Our message rates exceed 400,000 per
second. The actual structure also contains a length so the recievers know
how much data is in each one:

typedef struct
{
int var1;
int var2;
int var3;
int dataLength;
char var4[ ?????? ];
} MYSTRUCT;

dataLength may be set be as small as 1, but not larger than var4.

Bruce.
 
J

Jim Langston

Bruce. said:
John Harrison said:
Since you aren't worried about padding bytes the obvious answer is

typedef struct
{
int var1;
int var2;
int var3;
char var4[ 1024 - 3*sizeof(int) ];
} MYSTRUCT;

Thanks but my example was oversimplified to keep it brief. The actual
structures are much more complex, too complex for a solution like that.

I guess what I was looking for doesn't exist.

But thanks anyway.

Someone already showed you how to use it. Use a union.

#include <iostream>

typedef union
{
struct {
int v1;
int v2;
int v3;
} var;
char unused [1024];
} MyStruct;

int main(void)
{
MyStruct Foo;

std::cout << sizeof( Foo ) << "\n";

Foo.var.v1 = 10;
Foo.var.v2 = 20;
Foo.var.v3 = 30;
}

var.v1 will start at position 0.
 
B

Bruce.

Jim Langston said:
Bruce. said:
John Harrison said:
Since you aren't worried about padding bytes the obvious answer is

typedef struct
{
int var1;
int var2;
int var3;
char var4[ 1024 - 3*sizeof(int) ];
} MYSTRUCT;

Thanks but my example was oversimplified to keep it brief. The actual
structures are much more complex, too complex for a solution like that.

I guess what I was looking for doesn't exist.

But thanks anyway.

Someone already showed you how to use it. Use a union.

#include <iostream>

typedef union
{
struct {
int v1;
int v2;
int v3;
} var;
char unused [1024];
} MyStruct;

int main(void)
{
MyStruct Foo;

std::cout << sizeof( Foo ) << "\n";

Foo.var.v1 = 10;
Foo.var.v2 = 20;
Foo.var.v3 = 30;
}

var.v1 will start at position 0.

You forgot var4. I need var4 to be sized automattically to achieve a
structure size of 1024. I need var4 to the size of the remaining space up
to 1024 bytes. In this case it would be:

struct {
int v1;
int v2;
int v3;
char v4[1024 - 12];
} var;

But I want the compiler to figure the 12 automattically.

Bruce.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top