473,785 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Structure size

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.
Jul 19 '07
56 3176
On Jul 19, 7:15 pm, "Bruce." <no...@nowhere. comwrote:
"LR" <lr...@superlin k.netwrote in message
news:46******** *************** @news.uslec.net ...
Bruce. wrote:
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.
If you're streaming the data, you probably can't use int
directly anyway. So just use char buffer[whatever].

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 20 '07 #31
On 2007-07-20 16:45, LR wrote:
Victor Bazarov wrote:
>LR wrote:

>>(I'm assuming that as you said in another post, you don't have a
portability problem and you can control your compiler to take care of
packing issues.)

struct x1 {
int a,b,c;
};

struct x2 {
int a; double e,f,g,h,i;
};

template<type name T>
struct Message {
T t;
char v[1024 - sizeof(T)];
};

then, else where you can...

Message<x1m 1;
Message<x2m 2;
>>>
[..]

I am not sure here, but I believe there is no guarantee that the size
of the 'Message<x1>' or 'Message<x2>' is actually going to be 1024
even with the mechanism you give here.

Is that because of templates? Nested structs? Are you referring to the
OP's particular compiler and it's control of packing/alignment?
The structs I would guess, there's no guarantee that there wont be any
padding between the t and v in Message.

--
Erik Wikström
Jul 20 '07 #32
On Jul 20, 4:27 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
LR wrote:
[...]
(I'm assuming that as you said in another post, you don't have a
portability problem and you can control your compiler to take care of
packing issues.)
struct x1 {
int a,b,c;
};
struct x2 {
int a; double e,f,g,h,i;
};
template<typena me T>
struct Message {
T t;
char v[1024 - sizeof(T)];
};
then, else where you can...
Message<x1m1;
Message<x2m2;
there will be the added annoyance of having to reference the
individual members of x1 and x2 by doing something like m1.t.a = 43;
It might be wise to pick a better name for t.
[..]
I am not sure here, but I believe there is no guarantee that the size
of the 'Message<x1>' or 'Message<x2>' is actually going to be 1024
even with the mechanism you give here.
It's certainly not guaranteed by the standard. A compiler could
insert padding between the t and v, and still be conform. As a
quality of implementation issue... I would consider it a poor
choice on a byte addressed machine (and none of the compilers
I've ever used did insert padding here).
I wonder, however, if there
is something in the Standard related to the sizes (and alignments)
that I missed, and it might actually impose certain limitations thus
giving programmers more leeway when it comes to determining type sizes.
I don't think so.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 20 '07 #33
Bruce. wrote:
"John Harrison" <jo************ *@hotmail.comwr ote in message
news:ve******** ******@newsfe4-gui.ntli.net...
>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.

Try:

struct mystruct {
struct {
int var1;
int var2;
int var3;
} header;
char data[1024 - sizeof(header)];
}

we still have a slight padding issue because in theory the compiler
could add padding between header and data but in general it won't though
there maybe alignment padding at the end of header but that will be part
of the header object and so included in the sizeof(header).
The above method also has the advantage that it is more maintainable
(easier to change type of a field in the header or add new fields to the
header specification.
--
Please do NOT use robinton.demon. co.uk addresses
They cease to be valid on July 14
replace with Francis.Glassbo row at btinternet.com
Jul 22 '07 #34
Francis Glassborow wrote:
Bruce. wrote:
>"John Harrison" <jo************ *@hotmail.comwr ote in message
news:ve******* *******@newsfe4-gui.ntli.net...
>>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.

Try:

struct mystruct {
struct {
int var1;
int var2;
int var3;
} header;
char data[1024 - sizeof(header)];
}

we still have a slight padding issue because in theory the compiler
could add padding between header and data but in general it won't
though there maybe alignment padding at the end of header but that
will be part of the header object and so included in the
sizeof(header).

The above method also has the advantage that it is more maintainable
(easier to change type of a field in the header or add new fields to
the header specification.
I just thought of a slightly different way:

struct mystruct {
int var1, var2, var3;
char data[1024 - offsetof(data)];
};

Should that alleviate any trouble with possible padding?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 22 '07 #35
Victor Bazarov wrote:
Francis Glassborow wrote:
>Bruce. wrote:
>>"John Harrison" <jo************ *@hotmail.comwr ote in message
news:ve****** ********@newsfe 4-gui.ntli.net...
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.

Try:

struct mystruct {
struct {
int var1;
int var2;
int var3;
} header;
char data[1024 - sizeof(header)];
}

we still have a slight padding issue because in theory the compiler
could add padding between header and data but in general it won't
though there maybe alignment padding at the end of header but that
will be part of the header object and so included in the
sizeof(header) .

The above method also has the advantage that it is more maintainable
(easier to change type of a field in the header or add new fields to
the header specification.

I just thought of a slightly different way:

struct mystruct {
int var1, var2, var3;
char data[1024 - offsetof(data)];
Of course this ought to be

char data[1024 - offsetof(mystru ct, data)];
};

Should that alleviate any trouble with possible padding?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 22 '07 #36
On Jul 22, 9:03 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
struct mystruct {
int var1, var2, var3;
char data[1024 - offsetof(data)];
That's "offsetof( mystruct, data )", of course (as you've
already corrected).
};
Should that alleviate any trouble with possible padding?
I don't think you can use offsetof on an incomplete type.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 22 '07 #37
"Victor Bazarov" <v.********@com Acast.netwrote in message
news:Xt******** *************** *******@comcast .com...
I just thought of a slightly different way:

struct mystruct {
int var1, var2, var3;
char data[1024 - offsetof(mystru ct, data)];
};
Unfortunately that won't compile:

error C2027: use of undefined type 'mystruct'

Bruce.
Jul 22 '07 #38
Victor Bazarov wrote:
Victor Bazarov wrote:
>Francis Glassborow wrote:
>>Bruce. wrote:
"John Harrison" <jo************ *@hotmail.comwr ote in message
news:ve***** *********@newsf e4-gui.ntli.net...
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.
Try:

struct mystruct {
struct {
int var1;
int var2;
int var3;
} header;
char data[1024 - sizeof(header)];
}

we still have a slight padding issue because in theory the compiler
could add padding between header and data but in general it won't
though there maybe alignment padding at the end of header but that
will be part of the header object and so included in the
sizeof(header ).

The above method also has the advantage that it is more maintainable
(easier to change type of a field in the header or add new fields to
the header specification.
I just thought of a slightly different way:

struct mystruct {
int var1, var2, var3;
char data[1024 - offsetof(data)];

Of course this ought to be

char data[1024 - offsetof(mystru ct, data)];
>};

Should that alleviate any trouble with possible padding?
It shouldn't even compile, as offsetof requires mystruct
to be complete, and hence can't be used in the definition
of mystruct.

-- James
Jul 22 '07 #39

James Kanze <ja*********@gm ail.comwrote in message...
On Jul 22, 9:03 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>struct mystruct {
int var1, var2, var3;
char data[1024 - offsetof(data)];
>That's "offsetof( mystruct, data )", of course (as you've
already corrected).
>};
Should that alleviate any trouble with possible padding?
>I don't think you can use offsetof on an incomplete type.
As poor Bruce found out.

If you can't find a sane way to do it, think outside the box.

struct dummy{
int var1, var2, var3;
char data[1];
};

struct mystruct{
int var1, var2, var3;
char data[ 1024 - offsetof( dummy, data ) ];
};

// cout<<"sizeof dummy="<<sizeof (dummy)<<std::e ndl;
// cout<<"sizeof mystruct="<<siz eof(mystruct)<< std::endl;
// sizeof dummy=16
// sizeof mystruct=1024
We'd have to assume (or prey) that the compiler would construct the two
structs the same (up to 'data').

No, it wasn't my bath-water I drank!
<G>
--
Bob R
POVrookie
Jul 22 '07 #40

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3290
by: John | last post by:
Hi all, Can a linked list be a member of a structure? If so, when I add or remove an element from the linked list, the size of the structure will change. Will it cause any problem? Thanks a lot. John
13
3894
by: Amarendra | last post by:
Folks, This structure padding issue is bothering me now, could not locate a satisfactory answer on clc, so here it goes... I have a structure, given below: typedef struct { int flag; char keys; char padding;
2
1857
by: Sachin | last post by:
typdef struct { int i; char ch; }str; str str_var; char x, y; main() { //do nothing
10
2315
by: ranjeet.gupta | last post by:
Dear All !! Before i qoute my querry, I will like to qoute my analysis and my Knowledge Struct a { int raw; char data; };
4
3897
by: marco_segurini | last post by:
Hi, From my VB program I call a C++ function that gets a structure pointer like parameter. The structure has a field that contains the structure length and other fields. My problem is that each 'double' fields get 12 bytes instead of 8 so the structure length results wrong. '----Sample
6
5019
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public class MyClass {
4
11214
by: junky_fellow | last post by:
Can somebody please tell me about the structure alignment rules ? What I found was that on my system (cygwin running on PC, size of int=4 sizeof long=4, size of long long = 8) the cygwin compiler put the padding after the last member of structure. For eg, struct test { int i; char c; /* no padding required between int and char */ /* 3 byte padding is inserted here, Why ? */
15
2239
by: kris | last post by:
Hi I am writing a small program where I need to obtain the actual size of a structure. The programm is as follows struct abc { int j; char k; int i; }*a;
5
3798
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
6
4881
by: carles | last post by:
Hi, Here, sample code where a byte array is used to fill a particular structure: fs = File.OpenRead(path); // FileStream BITMAPFILEHEADER bfh = new BITMAPFILEHEADER(); b = new byte;
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.