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
BobR wrote:
:: James Kanze <ja*********@gm ail.comwrote in message...
:: On Jul 22, 9:03 pm, "Victor Bazarov" <v.Abaza...@com Acast.net>
:: wrote:
::
:::: 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').
::
Unfortunately, I don't think we can be sure about that (except in
practice:-).
An architecture odd enough to require padding between integers, or
between an integer and a char, could of course have different padding
for an odd number of characters (char[1]) and an even number of
characters (assuming, again, that's what we get :-).
I think [1024 - 3 * sizeof(int)] would work just as well. Otherwise we
should also start to think about the size of a byte. Are there 8 bits
in a char?!
Bo Persson
Bo Persson <bo*@gmb.dkwrot e in message...
BobR wrote:
:: James Kanze <ja*********@gm ail.comwrote in message...
::
::: 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').
::
Unfortunately, I don't think we can be sure about that (except in
practice:-).
An architecture odd enough to require padding between integers, or
between an integer and a char, could of course have different padding
for an odd number of characters (char[1]) and an even number of
characters (assuming, again, that's what we get :-).
I think [1024 - 3 * sizeof(int)] would work just as well. Otherwise we
should also start to think about the size of a byte. Are there 8 bits
in a char?!
No, there are 8 bits in a byte (Webster's <G>). If the byte is RAM, it's 9
bits (the parity bit :-}) in many/most machines in use (Intel,AMD (did Apple
completely drop Motorola?)). [ loosely speaking. ]
That's one thing I (slightly) miss about Assembler, being able to lay out
things exactly where I want them. What I don't miss is the memory management
(real, flat, protected (IA32)). So 'char' don't look so bad from where I
sit. I let the compiler worry about it's size in 9999 of 10000 cases.
The OP should be using Assembler for what (s)he wants to do, IMHO.
Or, 'std::vector<un signed charMyStruct(10 24)', and some *heavy* casting
(dang endians!). <G>
--
Bob R
POVrookie
BobR said:
<snip>
>
No, there are 8 bits in a byte (Webster's <G>).
Webster's is not normative. In both C and, I am given to understand,
C++, there are CHAR_BIT bits in a byte, where CHAR_BIT is at least 8. A
byte is exactly big enough to store one char.
--
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
Richard Heathfield <rj*@see.sig.in validwrote in message...
BobR said:
<snip>
No, there are 8 bits in a byte (Webster's <G>).
[ I should have 'double-grin'-ed that line. ]
>
Webster's is not normative. In both C and, I am given to understand,
C++, there are CHAR_BIT bits in a byte, where CHAR_BIT is at least 8.
A byte is exactly big enough to store one char.
Yep. In C || C++, I'd go by the CHAR_BIT.
"...., there are CHAR_BIT bits in a 'char' ", would be more proper
terminology, I think.
--
Bob R
POVrookie
In article <F4************ *********@bgtns c04-news.ops.worldn et.att.net>, re***********@w orldnet.att.net says...
[ ... ]
Yep. In C || C++, I'd go by the CHAR_BIT.
"...., there are CHAR_BIT bits in a 'char' ", would be more proper
terminology, I think.
Not really -- as the terms are used in the standards, 'char' is a type
whereas 'byte' is an amount of storage. As it happens, the standards go
on to require that a byte is always exactly enough storage to hold an
object of type char (and, due to other requirements, signed char or
unsigned char).
Nonetheless, the type char is more or less distinct from the amount of
storage it occupies -- in particular, there are a few requirements on
the char type that go beyond the fact that it has to fit in one byte of
storage.
--
Later,
Jerry.
The universe is a figment of its own imagination.
On Jul 23, 7:21 pm, "BobR" <removeBadB...@ worldnet.att.ne twrote:
No, there are 8 bits in a byte (Webster's <G>).
Then Webster's wrong. The original use was for 6 bits, on a
PDP-10, it was programmable (7 was conventionally used, but
implementations of C used 9), on a CDC mainframe, 10. Of
course, a byte was 8 bits on an IBM 360, and later on the
PDP-11, and those were very influential machines.
That's why line protocols are defined in terms of octets, and
not bytes.
If the byte is RAM, it's 9
bits (the parity bit :-}) in many/most machines in use (Intel,AMD (did Apple
completely drop Motorola?)). [ loosely speaking. ]
At one time, at least, a lot of machine memory used
auto-corrector codes (not parity). Something like 37 physical
bits for 32 accessible to the program. (Back in the good old
days, of course, memory was core. You could toggle your program
in on the front panel of one machine, turn the machine off, take
the memory board out, and insert it into another machine, and
still have your program. Times have changed, however, and I
don't quite see myself toggling in the machine instructions for
the latest Linux on a front panel. Even if the machine had a
front panel.)
--
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
James Kanze wrote:
On Jul 23, 7:21 pm, "BobR" <removeBadB...@ worldnet.att.ne twrote:
>No, there are 8 bits in a byte (Webster's <G>).
Then Webster's wrong. The original use was for 6 bits, on a
PDP-10, it was programmable (7 was conventionally used, but
implementations of C used 9), on a CDC mainframe, 10.
Did you mean 12? Or maybe you meant 10 characters per CPU word? AFAIR,
on CDC 6600s, Cyber 70s and 700s and and even the 800s in 700
compatibility mode (which I think they had) bytes were 12 bits,
essentially the size of a PP word, and characters were, well, thats a
can of worms, but the 63 and 64 character sets had 6 bit characters and
then I think there were variations on 8 and maybe 9 bit characters with
various packings and also 12 bit characters of two or maybe three kinds,
escaped 6 bit and 8 bits packed in 12. Of course I/O was another set of
problems. And I don't recall at all how the APL character set was
stored, but I think there may have been a few tricks.
Talk about fun! How do we read that file again?
LR
On Jul 24, 3:42 pm, LR <lr...@superlin k.netwrote:
James Kanze wrote:
On Jul 23, 7:21 pm, "BobR" <removeBadB...@ worldnet.att.ne twrote:
No, there are 8 bits in a byte (Webster's <G>).
Then Webster's wrong. The original use was for 6 bits, on a
PDP-10, it was programmable (7 was conventionally used, but
implementations of C used 9), on a CDC mainframe, 10.
Did you mean 12?
Maybe. It's been a very, very long time. I seem to remember
something about 6 10 bit bytes in a 60 bit word. But that could
be completely wrong.
My point was only that byte size has always varied.
--
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
In article <11************ **********@k79g 2000hse.googleg roups.com>, ja*********@gma il.com says...
[ 'byte' on Control Data mainframes ... ]
Maybe. It's been a very, very long time. I seem to remember
something about 6 10 bit bytes in a 60 bit word. But that could
be completely wrong.
It's backwards: the most common setup was 10 6-bit bytes in a 60-bit
word.
My point was only that byte size has always varied.
....and that is certainly valid.
--
Later,
Jerry.
The universe is a figment of its own imagination.
Jerry Coffin wrote:
In article <11************ **********@k79g 2000hse.googleg roups.com>, ja*********@gma il.com says...
[ 'byte' on Control Data mainframes ... ]
>Maybe. It's been a very, very long time. I seem to remember something about 6 10 bit bytes in a 60 bit word. But that could be completely wrong.
It's backwards: the most common setup was 10 6-bit bytes in a 60-bit
word.
No. It was six bit characters. The bytes were 12 bits, the size of a PP
word.
>
>My point was only that byte size has always varied.
...and that is certainly valid.
Have to agree with that.
LR This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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;
|
by: Sachin |
last post by:
typdef struct
{
int i;
char ch;
}str;
str str_var;
char x, y;
main()
{
//do nothing
|
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;
};
|
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
| |
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
{
|
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 ?
*/
|
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;
|
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++.
|
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;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |