Hi all,
I understand that some compilers pad some bytes to the
aggregate data types in order to access the members of the aggregate
data types(i.e. structure) fast. This depends on the architectures.
Some architectures cannot access the data which will be stored on the
odd addresses or they may find difficult to access it. This is the
reason for padding extra bytes.
Now consider the following structure:
struct {
int i; // 4 bytes
char c; // 1 byte
char b; // 1 byte
}a;
Now consider the memory representation for this structure:
----------------------------- Memory locations
i 0
-----------------------------
i 1
-----------------------------
i 2
------------------------------
i 3
-------------------------------
c 4
--------------------------------
b 5
--------------------------------
padding 6
---------------------------------
padding 7
---------------------------------
The size of this strucutre will be 8bytes after padding.
The first 4 memory locations(0 to 3) are allocated for int i;
The next byte(4) is allocated for char c;
the next byte(5) is allocated for char b;
Now my doubt is , does not the processor find it difficult to access
the value of the variable which is stored in odd memory location(ie.
5)? can anyone elaborate on this?
Regards 28 20804
ramu wrote: Hi all, I understand that some compilers pad some bytes to the aggregate data types in order to access the members of the aggregate data types(i.e. structure) fast. This depends on the architectures. Some architectures cannot access the data which will be stored on the odd addresses or they may find difficult to access it. This is the reason for padding extra bytes.
This is off topic here.
Now consider the following structure:
struct { int i; // 4 bytes char c; // 1 byte char b; // 1 byte }a;
Now consider the memory representation for this structure:
< snip >
<OT>
If such a padding is to take place, it's more likely that both `c` and
`d` will `occupy` a 4-byte memory block, or whichever block is
convenient for the implementation.
</OT>
Cheers
Vladimir
--
"Stealing a rhinoceros should not be attempted lightly."
In 32 - bit processors , Each bus cycle will access the 32 - bit data
from memory. For reading variable b, first the processor reads the
entire 32 bit word which includes b, c and the padded data.
This is the reason why padding is required. (it is required for
allignment).
santosh wrote: In 32 - bit processors , Each bus cycle will access the 32 - bit data from memory. For reading variable b, first the processor reads the entire 32 bit word which includes b, c and the padded data.
This is the reason why padding is required. (it is required for allignment).
Before posting again, please read: http://cfaj.freeshell.org/google/
Thank you.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
"ramu" <ra******@gmail .com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com... Hi all, I understand that some compilers pad some bytes to the aggregate data types in order to access the members of the aggregate data types(i.e. structure) fast. This depends on the architectures. Some architectures cannot access the data which will be stored on the odd addresses or they may find difficult to access it. This is the reason for padding extra bytes.
Now consider the following structure:
struct { int i; // 4 bytes char c; // 1 byte char b; // 1 byte }a;
Okay. Layout can be compiler specific.
Now my doubt is , does not the processor find it difficult to access the value of the variable which is stored in odd memory location(ie. 5)? can anyone elaborate on this?
No idea. The code generate by DJGPP and OW are radically different. You'll
need to study the disassembly.
If you want to see the exact layout of a structure with different packings,
you can play with this code:
#include <stdio.h>
// Works for DJGPP 2.03 and OW 1.3
#ifdef __DJGPP__
#define HANDLE_PRAGMA_P ACK_PUSH_POP 1
#endif
// default alignment
struct {
unsigned int i;
unsigned char c;
unsigned long a;
unsigned short e;
unsigned char b;
} s0;
#pragma pack(push,1)
struct {
unsigned int i;
unsigned char c;
unsigned long a;
unsigned short e;
unsigned char b;
} s1;
#pragma pack(pop)
#pragma pack(push,2)
struct {
unsigned int i;
unsigned char c;
unsigned long a;
unsigned short e;
unsigned char b;
} s2;
#pragma pack(pop)
#pragma pack(push,4)
struct {
unsigned int i;
unsigned char c;
unsigned long a;
unsigned short e;
unsigned char b;
} s3;
#pragma pack(pop)
void print_s(unsigne d char *s, int size)
{
int i;
printf("%d ",size);
for(i=0;i<size; i++)
{
printf("%02x ",s[i]);
}
printf("\n");
}
int main(void)
{
s0.i=(unsigned int)0xF0F1F2F3; // cast in case 16 not 32
s0.a=(unsigned long)0xE0E1E2E3 ; // cast in case 16 not 32
s0.e=(unsigned short)0xD0D1D2D 3; // cast in case 16 not 32
s0.c=0xFC;
s0.b=0xFE;
s1.i=(unsigned int)0xF0F1F2F3; // cast in case 16 not 32
s1.a=(unsigned long)0xE0E1E2E3 ; // cast in case 16 not 32
s1.e=(unsigned short)0xD0D1D2D 3; // cast in case 16 not 32
s1.c=0xFC;
s1.b=0xFE;
s2.i=(unsigned int)0xF0F1F2F3; // cast in case 16 not 32
s2.a=(unsigned long)0xE0E1E2E3 ; // cast in case 16 not 32
s2.e=(unsigned short)0xD0D1D2D 3; // cast in case 16 not 32
s2.c=0xFC;
s2.b=0xFE;
s3.i=(unsigned int)0xF0F1F2F3; // cast in case 16 not 32
s3.a=(unsigned long)0xE0E1E2E3 ; // cast in case 16 not 32
s3.e=(unsigned short)0xD0D1D2D 3; // cast in case 16 not 32
s3.c=0xFC;
s3.b=0xFE;
printf("s0 ");
print_s((unsign ed char *)&s0,sizeof(s0 ));
printf("s1 ");
print_s((unsign ed char *)&s1,sizeof(s1 ));
printf("s2 ");
print_s((unsign ed char *)&s2,sizeof(s2 ));
printf("s3 ");
print_s((unsign ed char *)&s3,sizeof(s3 ));
return(0);
}
Rod Pemberton
santosh wrote: In 32 - bit processors , Each bus cycle will access the 32 - bit data from memory. For reading variable b, first the processor reads the entire 32 bit word which includes b, c and the padded data.
This is the reason why padding is required. (it is required for allignment).
It isn't required (but is desirable) on processors that can perform
misaligned access.
Alignment is very implementation specific and can be a compatibility
headache if code assumes a certain alignment.
--
Ian Collins.
I dint know that.
Thanks.
Nelu wrote: santosh wrote: In 32 - bit processors , Each bus cycle will access the 32 - bit data from memory. For reading variable b, first the processor reads the entire 32 bit word which includes b, c and the padded data.
This is the reason why padding is required. (it is required for allignment). Before posting again, please read: http://cfaj.freeshell.org/google/
Thank you.
-- Ioan - Ciprian Tandau tandau _at_ freeshell _dot_ org (hope it's not too late) (... and that it still works...)
santosh wrote: Nelu wrote: santosh wrote: In 32 - bit processors , Each bus cycle will access the 32 - bit data from memory. For reading variable b, first the processor reads the entire 32 bit word which includes b, c and the padded data.
This is the reason why padding is required. (it is required for allignment). Before posting again, please read: http://cfaj.freeshell.org/google/
I dint know that.
I believe it also says something about top-posting (I corrected yours
here), and possibly even about not quoting signatures.
Cheers
Vladimir
"ramu" <ra******@gmail .com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com... Hi all, I understand that some compilers pad some bytes to the aggregate data types in order to access the members of the aggregate data types(i.e. structure) fast. This depends on the architectures. Some architectures cannot access the data which will be stored on the odd addresses or they may find difficult to access it. This is the reason for padding extra bytes.
Now consider the following structure:
struct { int i; // 4 bytes char c; // 1 byte char b; // 1 byte }a;
[snip]
The size of this strucutre will be 8bytes after padding. The first 4 memory locations(0 to 3) are allocated for int i; The next byte(4) is allocated for char c; the next byte(5) is allocated for char b;
Now my doubt is , does not the processor find it difficult to access the value of the variable which is stored in odd memory location(ie. 5)? can anyone elaborate on this?
Alignment is a problem when objects lie across boundaries, requiring
multiple fetches if they can be fetched at all.
The types which are smaller than a word never straddle a boundary. They
were provided in C so that you can pack more than one item into a word. The
unpacking overhead should be relatively minor, but if you're worried, you
can always store your characters in ints. That's your choice: if you choose
the "packable" type, it's not the compiler's job to change your mind.
--
RSH
"Vladimir S. Oka" <no****@btopenw orld.com> wrote: ramu wrote:
I understand that some compilers pad some bytes to the aggregate data types in order to access the members of the aggregate data types(i.e. structure) fast. This depends on the architectures. Some architectures cannot access the data which will be stored on the odd addresses or they may find difficult to access it. This is the reason for padding extra bytes.
This is off topic here.
Not really. That certain types may require alignments, and that
structures may contain padding for that and other reasons, is perfectly
Standard. Which types happen to require padding on a specific
implementation, and how much, that's off topic here. The principle
itself, though, is on topic. Now consider the following structure:
struct { int i; // 4 bytes char c; // 1 byte char b; // 1 byte }a;
Now consider the memory representation for this structure:
< snip >
<OT> If such a padding is to take place, it's more likely that both `c` and `d` will `occupy` a 4-byte memory block, or whichever block is convenient for the implementation.
Not necessarily. One thing is certain, though: if normal ints aren't
just 4 bytes large, but also require 4-byte alignment, the
implementation must make certain that no ints, including the one in the
second member of an array of these structs, break alignment. Of course
it's possible for the implementation to fudge this in the background by
accessing all struct-member ints as arrays of char, but the best and
perfectly Standard way would be to make the whole struct take 8 bytes: 4
for the int, 1 each for the chars, and 2 for padding to make sure the
next int also ends up on a well-aligned memory address.
Richard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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: damian birchler |
last post by:
Hi
I'm wondering of what type a structure is. Of course, it is a
_structure_, but an array isn't an _array_ either. So of what type is
a structure? I'd say a pointer, am I right?
|
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: Lalatendu Das |
last post by:
hi let's say i have a structure
struct test {
int A;
char B;
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
because variable 'A' going to take 4 byte then four charachter of
| |
by: kyle york |
last post by:
Greetings,
Why does the C standard require the members of a structure not be
re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which
means one cannot rely on the exact layout anyway, so what's the point?
Without this restriction the compiler could layout the structure in the
most efficient way possible, for some definition of efficient. It would
be easy enough to turn this reordering off with a compiler specific...
|
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: karthikbalaguru |
last post by:
Hi,
I find that the structure padding is not being taken into account
while using 'new' operator.
Is there a way to enable it ?
struct Dataunit
{
char dataid;
int keyid;
|
by: Rohit kumar Chandel |
last post by:
Hi All,
Please let me know how to find in a structure whether compiler has used
padding or not.
Regards
Rohit
|
by: Kislay |
last post by:
case 1 :
struct s
{
char c1; // 8
double d; // 8
int i1; // 4
char c2; // 4
int i2; // 4
};
|
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: 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...
|
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...
| |