473,765 Members | 2,137 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 #1
56 3171
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.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 19 '07 #2
On 2007-07-19 17:39, 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.
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).

--
Erik Wikström
Jul 19 '07 #3
Erik Wikström said:
On 2007-07-19 17:39, 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
automattical ly 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.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 19 '07 #4
On 2007-07-19 17:58, Erik Wikström wrote:
On 2007-07-19 17:39, 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.

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.var 1 = 1;
std::cout << sizeof(MyUnion) ;
}

--
Erik Wikström
Jul 19 '07 #5
LR
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?

LR
Jul 19 '07 #6
"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
automattical ly 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.
Jul 19 '07 #7
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:Dq******** *************** *******@bt.com. ..
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
automattical ly 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.
Jul 19 '07 #8
Bruce. said:

<snip>
>
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.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 19 '07 #9
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:rq******** *************@b t.com...
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.
Jul 19 '07 #10

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

Similar topics

5
3288
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
3893
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
1856
by: Sachin | last post by:
typdef struct { int i; char ch; }str; str str_var; char x, y; main() { //do nothing
10
2313
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
11213
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
2238
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
3796
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
4879
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
9398
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
10156
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...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9832
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...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
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
5275
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
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
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.