How does __attribute__((packed)) for a field affect struct which contains this field?
If I have a field in my struct which is packed, why my whole structure is becoming packed?
Example:
#include <stdio.h>
struct foo {
int a;
} __attribute__((packed));
struct bar {
char b;
struct foo bla;
char a;
};
int main() {
printf("%ldn", sizeof(struct bar));
return 0;
}
https://ideone.com/bjoZHB
Sizeof of bar
struct is 6, but it should be 12, because it should be aligned.
c gcc
add a comment |
If I have a field in my struct which is packed, why my whole structure is becoming packed?
Example:
#include <stdio.h>
struct foo {
int a;
} __attribute__((packed));
struct bar {
char b;
struct foo bla;
char a;
};
int main() {
printf("%ldn", sizeof(struct bar));
return 0;
}
https://ideone.com/bjoZHB
Sizeof of bar
struct is 6, but it should be 12, because it should be aligned.
c gcc
add a comment |
If I have a field in my struct which is packed, why my whole structure is becoming packed?
Example:
#include <stdio.h>
struct foo {
int a;
} __attribute__((packed));
struct bar {
char b;
struct foo bla;
char a;
};
int main() {
printf("%ldn", sizeof(struct bar));
return 0;
}
https://ideone.com/bjoZHB
Sizeof of bar
struct is 6, but it should be 12, because it should be aligned.
c gcc
If I have a field in my struct which is packed, why my whole structure is becoming packed?
Example:
#include <stdio.h>
struct foo {
int a;
} __attribute__((packed));
struct bar {
char b;
struct foo bla;
char a;
};
int main() {
printf("%ldn", sizeof(struct bar));
return 0;
}
https://ideone.com/bjoZHB
Sizeof of bar
struct is 6, but it should be 12, because it should be aligned.
c gcc
c gcc
asked Jan 2 at 17:44
Sukhanov NiсkolaySukhanov Niсkolay
6971617
6971617
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
it seems because __attribute__((packed))
means use the minimum memory for structure, it also means that it can ignore alignment for siding members when it is in another structure. Check following structure:
struct bar {
char b;
__attribute__((packed)) int bla;
char a;
};
When you check size for this structure, it will be 6. This happens because it ignores member alignment for 2 side members(a
and b
here). But this structure:
struct bar {
char b;
__attribute__((packed)) int bla;
char a;
int c;
};
has size of 12, because it is aligned c
on 4 bytes boundary. In your case, if you use aligned
attribute too at same time, it works as you expect:
struct bar {
char b;
__attribute__((aligned (4), packed)) int bla;
char a;
};
This structure size is 12.
Update:
I only found this in GCC
's aligned section of attributes. I think it is related to what I mentioned here:
The aligned attribute can only increase the alignment; but you can
decrease it by specifying packed as well
.Just remember that if you want to keep child structure packed but main structure aligned, you need to use 2 attributes in 2 different declarations. For example following structure has size of 12:
struct foo {
char b;
int a;
} __attribute__((packed));
struct bar {
char b;
__attribute__((aligned(4))) struct foo bla;
char a;
};
but if you use aligned()
in declaration of foo
as __attribute__((aligned (4), packed))
, size will be 16. This happens because foo
gets aligned too, and it will not be useful in case of packing.
Where can I read about "means that it can ignore alignment when it is in another structure" I didn't find it in the gcc docs or anywhere. :(
– Sukhanov Niсkolay
Jan 2 at 18:03
@SukhanovNiсkolay it had a "it seems" at start of sentence too. but I found 1 sentence about it in gcc manual and I add it to reply.
– Afshin
Jan 2 at 18:05
Yeah, I saw the same cite, then it seems it is an ISO C requirement. :) UPDATE: Sorry, wrong cite, I saw this: "ISO C standard to be at least a perfect multiple of the lowest common multiple of the alignments of all of the members of the struct"
– Sukhanov Niсkolay
Jan 2 at 18:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010839%2fhow-does-attribute-packed-for-a-field-affect-struct-which-contains-this-f%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
it seems because __attribute__((packed))
means use the minimum memory for structure, it also means that it can ignore alignment for siding members when it is in another structure. Check following structure:
struct bar {
char b;
__attribute__((packed)) int bla;
char a;
};
When you check size for this structure, it will be 6. This happens because it ignores member alignment for 2 side members(a
and b
here). But this structure:
struct bar {
char b;
__attribute__((packed)) int bla;
char a;
int c;
};
has size of 12, because it is aligned c
on 4 bytes boundary. In your case, if you use aligned
attribute too at same time, it works as you expect:
struct bar {
char b;
__attribute__((aligned (4), packed)) int bla;
char a;
};
This structure size is 12.
Update:
I only found this in GCC
's aligned section of attributes. I think it is related to what I mentioned here:
The aligned attribute can only increase the alignment; but you can
decrease it by specifying packed as well
.Just remember that if you want to keep child structure packed but main structure aligned, you need to use 2 attributes in 2 different declarations. For example following structure has size of 12:
struct foo {
char b;
int a;
} __attribute__((packed));
struct bar {
char b;
__attribute__((aligned(4))) struct foo bla;
char a;
};
but if you use aligned()
in declaration of foo
as __attribute__((aligned (4), packed))
, size will be 16. This happens because foo
gets aligned too, and it will not be useful in case of packing.
Where can I read about "means that it can ignore alignment when it is in another structure" I didn't find it in the gcc docs or anywhere. :(
– Sukhanov Niсkolay
Jan 2 at 18:03
@SukhanovNiсkolay it had a "it seems" at start of sentence too. but I found 1 sentence about it in gcc manual and I add it to reply.
– Afshin
Jan 2 at 18:05
Yeah, I saw the same cite, then it seems it is an ISO C requirement. :) UPDATE: Sorry, wrong cite, I saw this: "ISO C standard to be at least a perfect multiple of the lowest common multiple of the alignments of all of the members of the struct"
– Sukhanov Niсkolay
Jan 2 at 18:10
add a comment |
it seems because __attribute__((packed))
means use the minimum memory for structure, it also means that it can ignore alignment for siding members when it is in another structure. Check following structure:
struct bar {
char b;
__attribute__((packed)) int bla;
char a;
};
When you check size for this structure, it will be 6. This happens because it ignores member alignment for 2 side members(a
and b
here). But this structure:
struct bar {
char b;
__attribute__((packed)) int bla;
char a;
int c;
};
has size of 12, because it is aligned c
on 4 bytes boundary. In your case, if you use aligned
attribute too at same time, it works as you expect:
struct bar {
char b;
__attribute__((aligned (4), packed)) int bla;
char a;
};
This structure size is 12.
Update:
I only found this in GCC
's aligned section of attributes. I think it is related to what I mentioned here:
The aligned attribute can only increase the alignment; but you can
decrease it by specifying packed as well
.Just remember that if you want to keep child structure packed but main structure aligned, you need to use 2 attributes in 2 different declarations. For example following structure has size of 12:
struct foo {
char b;
int a;
} __attribute__((packed));
struct bar {
char b;
__attribute__((aligned(4))) struct foo bla;
char a;
};
but if you use aligned()
in declaration of foo
as __attribute__((aligned (4), packed))
, size will be 16. This happens because foo
gets aligned too, and it will not be useful in case of packing.
Where can I read about "means that it can ignore alignment when it is in another structure" I didn't find it in the gcc docs or anywhere. :(
– Sukhanov Niсkolay
Jan 2 at 18:03
@SukhanovNiсkolay it had a "it seems" at start of sentence too. but I found 1 sentence about it in gcc manual and I add it to reply.
– Afshin
Jan 2 at 18:05
Yeah, I saw the same cite, then it seems it is an ISO C requirement. :) UPDATE: Sorry, wrong cite, I saw this: "ISO C standard to be at least a perfect multiple of the lowest common multiple of the alignments of all of the members of the struct"
– Sukhanov Niсkolay
Jan 2 at 18:10
add a comment |
it seems because __attribute__((packed))
means use the minimum memory for structure, it also means that it can ignore alignment for siding members when it is in another structure. Check following structure:
struct bar {
char b;
__attribute__((packed)) int bla;
char a;
};
When you check size for this structure, it will be 6. This happens because it ignores member alignment for 2 side members(a
and b
here). But this structure:
struct bar {
char b;
__attribute__((packed)) int bla;
char a;
int c;
};
has size of 12, because it is aligned c
on 4 bytes boundary. In your case, if you use aligned
attribute too at same time, it works as you expect:
struct bar {
char b;
__attribute__((aligned (4), packed)) int bla;
char a;
};
This structure size is 12.
Update:
I only found this in GCC
's aligned section of attributes. I think it is related to what I mentioned here:
The aligned attribute can only increase the alignment; but you can
decrease it by specifying packed as well
.Just remember that if you want to keep child structure packed but main structure aligned, you need to use 2 attributes in 2 different declarations. For example following structure has size of 12:
struct foo {
char b;
int a;
} __attribute__((packed));
struct bar {
char b;
__attribute__((aligned(4))) struct foo bla;
char a;
};
but if you use aligned()
in declaration of foo
as __attribute__((aligned (4), packed))
, size will be 16. This happens because foo
gets aligned too, and it will not be useful in case of packing.
it seems because __attribute__((packed))
means use the minimum memory for structure, it also means that it can ignore alignment for siding members when it is in another structure. Check following structure:
struct bar {
char b;
__attribute__((packed)) int bla;
char a;
};
When you check size for this structure, it will be 6. This happens because it ignores member alignment for 2 side members(a
and b
here). But this structure:
struct bar {
char b;
__attribute__((packed)) int bla;
char a;
int c;
};
has size of 12, because it is aligned c
on 4 bytes boundary. In your case, if you use aligned
attribute too at same time, it works as you expect:
struct bar {
char b;
__attribute__((aligned (4), packed)) int bla;
char a;
};
This structure size is 12.
Update:
I only found this in GCC
's aligned section of attributes. I think it is related to what I mentioned here:
The aligned attribute can only increase the alignment; but you can
decrease it by specifying packed as well
.Just remember that if you want to keep child structure packed but main structure aligned, you need to use 2 attributes in 2 different declarations. For example following structure has size of 12:
struct foo {
char b;
int a;
} __attribute__((packed));
struct bar {
char b;
__attribute__((aligned(4))) struct foo bla;
char a;
};
but if you use aligned()
in declaration of foo
as __attribute__((aligned (4), packed))
, size will be 16. This happens because foo
gets aligned too, and it will not be useful in case of packing.
edited Jan 2 at 18:22
answered Jan 2 at 18:00
AfshinAfshin
3,1811627
3,1811627
Where can I read about "means that it can ignore alignment when it is in another structure" I didn't find it in the gcc docs or anywhere. :(
– Sukhanov Niсkolay
Jan 2 at 18:03
@SukhanovNiсkolay it had a "it seems" at start of sentence too. but I found 1 sentence about it in gcc manual and I add it to reply.
– Afshin
Jan 2 at 18:05
Yeah, I saw the same cite, then it seems it is an ISO C requirement. :) UPDATE: Sorry, wrong cite, I saw this: "ISO C standard to be at least a perfect multiple of the lowest common multiple of the alignments of all of the members of the struct"
– Sukhanov Niсkolay
Jan 2 at 18:10
add a comment |
Where can I read about "means that it can ignore alignment when it is in another structure" I didn't find it in the gcc docs or anywhere. :(
– Sukhanov Niсkolay
Jan 2 at 18:03
@SukhanovNiсkolay it had a "it seems" at start of sentence too. but I found 1 sentence about it in gcc manual and I add it to reply.
– Afshin
Jan 2 at 18:05
Yeah, I saw the same cite, then it seems it is an ISO C requirement. :) UPDATE: Sorry, wrong cite, I saw this: "ISO C standard to be at least a perfect multiple of the lowest common multiple of the alignments of all of the members of the struct"
– Sukhanov Niсkolay
Jan 2 at 18:10
Where can I read about "means that it can ignore alignment when it is in another structure" I didn't find it in the gcc docs or anywhere. :(
– Sukhanov Niсkolay
Jan 2 at 18:03
Where can I read about "means that it can ignore alignment when it is in another structure" I didn't find it in the gcc docs or anywhere. :(
– Sukhanov Niсkolay
Jan 2 at 18:03
@SukhanovNiсkolay it had a "it seems" at start of sentence too. but I found 1 sentence about it in gcc manual and I add it to reply.
– Afshin
Jan 2 at 18:05
@SukhanovNiсkolay it had a "it seems" at start of sentence too. but I found 1 sentence about it in gcc manual and I add it to reply.
– Afshin
Jan 2 at 18:05
Yeah, I saw the same cite, then it seems it is an ISO C requirement. :) UPDATE: Sorry, wrong cite, I saw this: "ISO C standard to be at least a perfect multiple of the lowest common multiple of the alignments of all of the members of the struct"
– Sukhanov Niсkolay
Jan 2 at 18:10
Yeah, I saw the same cite, then it seems it is an ISO C requirement. :) UPDATE: Sorry, wrong cite, I saw this: "ISO C standard to be at least a perfect multiple of the lowest common multiple of the alignments of all of the members of the struct"
– Sukhanov Niсkolay
Jan 2 at 18:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010839%2fhow-does-attribute-packed-for-a-field-affect-struct-which-contains-this-f%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown