J
Jim Langston
Bruce. said:Jim Langston said:Bruce. 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.
Why do you need v4 to be the size of the remaining bytes? If it's because
you need a pointer...
typedef union
{
struct {
int v1;
int v2;
int v3;
char v4[1]
} 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;
}
Now v4 will point to the biggin of the remaining data. You already said you
would get the actual length in another variable, and that's all you need,
right? Just because v4 is only declared as having one character, you can
still use v4[1] v4[2] etc... This is acutally done all the time on accident
by programmers (buffer overrun) but in this case you actually have more
data.