union initialization in c
i was wondering whether union variable will be initialized like a structure variable or not...
#include<stdio.h>
int main(void)
{
struct a
{
int i;
char c;
};
struct a ob={4};
printf("%d",ob.c);
}
the above code gives 0 as output..
so when i is intialized c also gets intialized..
in the below code...if the union member integer also gets intialized with the character array this snippet would give the output 515...
(i verified it by allocating memory for the union variable using malloc..it works fine.)
#include<stdio.h>
int main(void)
{
union a
{
int i;
char c[2];
};
union a ob;
ob.ch[0]=3;
ob.ch[1]=2;
printf("%d",ob.i);
return 0;
}
but without allocating memory whether it could intialize the int i or not..(the hex value of int i in this code is set to 0x990203).
i think that 99 is the result that shows that the higher bits are not intialized..
am i correct?..
c
add a comment |
i was wondering whether union variable will be initialized like a structure variable or not...
#include<stdio.h>
int main(void)
{
struct a
{
int i;
char c;
};
struct a ob={4};
printf("%d",ob.c);
}
the above code gives 0 as output..
so when i is intialized c also gets intialized..
in the below code...if the union member integer also gets intialized with the character array this snippet would give the output 515...
(i verified it by allocating memory for the union variable using malloc..it works fine.)
#include<stdio.h>
int main(void)
{
union a
{
int i;
char c[2];
};
union a ob;
ob.ch[0]=3;
ob.ch[1]=2;
printf("%d",ob.i);
return 0;
}
but without allocating memory whether it could intialize the int i or not..(the hex value of int i in this code is set to 0x990203).
i think that 99 is the result that shows that the higher bits are not intialized..
am i correct?..
c
Reading from the field of the union to which you did not write is undefined behavior. You could see arbitrary data there, it worth nothing that your particular compiler does it one way or the other.
– dasblinkenlight
Mar 22 '12 at 14:51
2
@dasblinkenlight: reading a member you did not write to is defined behaviour -- however, if that member is represented by bytes which did not partake in the last store (ie if the accessed member is of greater size than the member used in the last store), the behaviour is unspecified; see stackoverflow.com/a/8513748/48015 for details
– Christoph
Mar 22 '12 at 14:54
You may be right, have a look at the assembly output from your compiler to make sure.
– Gowtham
Mar 22 '12 at 14:54
could you comment about using malloc leaves the value of int to 515..
– cdummy
Mar 22 '12 at 15:05
add a comment |
i was wondering whether union variable will be initialized like a structure variable or not...
#include<stdio.h>
int main(void)
{
struct a
{
int i;
char c;
};
struct a ob={4};
printf("%d",ob.c);
}
the above code gives 0 as output..
so when i is intialized c also gets intialized..
in the below code...if the union member integer also gets intialized with the character array this snippet would give the output 515...
(i verified it by allocating memory for the union variable using malloc..it works fine.)
#include<stdio.h>
int main(void)
{
union a
{
int i;
char c[2];
};
union a ob;
ob.ch[0]=3;
ob.ch[1]=2;
printf("%d",ob.i);
return 0;
}
but without allocating memory whether it could intialize the int i or not..(the hex value of int i in this code is set to 0x990203).
i think that 99 is the result that shows that the higher bits are not intialized..
am i correct?..
c
i was wondering whether union variable will be initialized like a structure variable or not...
#include<stdio.h>
int main(void)
{
struct a
{
int i;
char c;
};
struct a ob={4};
printf("%d",ob.c);
}
the above code gives 0 as output..
so when i is intialized c also gets intialized..
in the below code...if the union member integer also gets intialized with the character array this snippet would give the output 515...
(i verified it by allocating memory for the union variable using malloc..it works fine.)
#include<stdio.h>
int main(void)
{
union a
{
int i;
char c[2];
};
union a ob;
ob.ch[0]=3;
ob.ch[1]=2;
printf("%d",ob.i);
return 0;
}
but without allocating memory whether it could intialize the int i or not..(the hex value of int i in this code is set to 0x990203).
i think that 99 is the result that shows that the higher bits are not intialized..
am i correct?..
c
c
edited Oct 21 '15 at 7:16
jruizaranguren
6,18443856
6,18443856
asked Mar 22 '12 at 14:48
cdummycdummy
311212
311212
Reading from the field of the union to which you did not write is undefined behavior. You could see arbitrary data there, it worth nothing that your particular compiler does it one way or the other.
– dasblinkenlight
Mar 22 '12 at 14:51
2
@dasblinkenlight: reading a member you did not write to is defined behaviour -- however, if that member is represented by bytes which did not partake in the last store (ie if the accessed member is of greater size than the member used in the last store), the behaviour is unspecified; see stackoverflow.com/a/8513748/48015 for details
– Christoph
Mar 22 '12 at 14:54
You may be right, have a look at the assembly output from your compiler to make sure.
– Gowtham
Mar 22 '12 at 14:54
could you comment about using malloc leaves the value of int to 515..
– cdummy
Mar 22 '12 at 15:05
add a comment |
Reading from the field of the union to which you did not write is undefined behavior. You could see arbitrary data there, it worth nothing that your particular compiler does it one way or the other.
– dasblinkenlight
Mar 22 '12 at 14:51
2
@dasblinkenlight: reading a member you did not write to is defined behaviour -- however, if that member is represented by bytes which did not partake in the last store (ie if the accessed member is of greater size than the member used in the last store), the behaviour is unspecified; see stackoverflow.com/a/8513748/48015 for details
– Christoph
Mar 22 '12 at 14:54
You may be right, have a look at the assembly output from your compiler to make sure.
– Gowtham
Mar 22 '12 at 14:54
could you comment about using malloc leaves the value of int to 515..
– cdummy
Mar 22 '12 at 15:05
Reading from the field of the union to which you did not write is undefined behavior. You could see arbitrary data there, it worth nothing that your particular compiler does it one way or the other.
– dasblinkenlight
Mar 22 '12 at 14:51
Reading from the field of the union to which you did not write is undefined behavior. You could see arbitrary data there, it worth nothing that your particular compiler does it one way or the other.
– dasblinkenlight
Mar 22 '12 at 14:51
2
2
@dasblinkenlight: reading a member you did not write to is defined behaviour -- however, if that member is represented by bytes which did not partake in the last store (ie if the accessed member is of greater size than the member used in the last store), the behaviour is unspecified; see stackoverflow.com/a/8513748/48015 for details
– Christoph
Mar 22 '12 at 14:54
@dasblinkenlight: reading a member you did not write to is defined behaviour -- however, if that member is represented by bytes which did not partake in the last store (ie if the accessed member is of greater size than the member used in the last store), the behaviour is unspecified; see stackoverflow.com/a/8513748/48015 for details
– Christoph
Mar 22 '12 at 14:54
You may be right, have a look at the assembly output from your compiler to make sure.
– Gowtham
Mar 22 '12 at 14:54
You may be right, have a look at the assembly output from your compiler to make sure.
– Gowtham
Mar 22 '12 at 14:54
could you comment about using malloc leaves the value of int to 515..
– cdummy
Mar 22 '12 at 15:05
could you comment about using malloc leaves the value of int to 515..
– cdummy
Mar 22 '12 at 15:05
add a comment |
3 Answers
3
active
oldest
votes
Reading from a member of the union other than the one most recently written to outside the "byte footprint" of the member to which you have most recently written leads to undefined unspecified behavior. You should not be reading i until after you have written to it: Whatever you see there is non-portable junk.
EDIT 1 Edited in response to Cristoph's comment.
3
this is a common misconception - reading from a union member you haven't written to last is only illegal in C++; however, the value of a member is unspecified if the last store happened to a smaller one
– Christoph
Mar 22 '12 at 15:00
Yes, it is only undefined behavior if the contents of the object when interpreted as the type that you are accessing it is a trap representation. First, such trap representations are rare nowadays, and second you'd really have bad luck to see it.
– Jens Gustedt
Mar 22 '12 at 15:04
@Christoph Thanks for the correction! I've been out of the "pure C" world long enough to start thinking that C is a subset of C++ :)
– dasblinkenlight
Mar 22 '12 at 15:08
add a comment |
Your assignment through the char could lead to undefined behavior if the new value happens to be a trap representation (rare) for the type int.
Your example with the union is not an initialization but only an assignment and thus it only changes exactly the bytes that you are accessing and the other stay with unspecific values. For unions it is always a good idea to do an initialization of the widest member something like
union a ob = { .i = 0 };
Thereby you can guarantee that all bytes of your object are initialized by 0.
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
@cdummy, if you don't initialize the variable (on the stack or allocated withmalloc) any value that happens to be in there is used for theintvalue. This is completely arbitrary. Only the twocharyou change directly have a value that you control.
– Jens Gustedt
Mar 22 '12 at 15:44
whether it is same for calloc
– cdummy
Mar 22 '12 at 15:49
With calloc you would be certain to get 515 (0x203, as you set the char array) because the upper bytes of the int would have been cleared by calloc.
– William Morris
Mar 22 '12 at 16:52
thanks dude....
– cdummy
Mar 22 '12 at 17:24
add a comment |
i think that 99 is the result that shows that the higher bits are not
intialized.. am i correct?..
Correct because you only assign to two bytes explicitly in your second example, so two bytes of the integer remain uninitialized. In the first example you assign 4 to i, which is an integer and shares a byte with c. However, If both union members are of same type, then assuming that both will be initialized is correct. Also, the space allocated for a union is the space taken by its largest member so assuming some of the bytes of i will change when you assign to c[x] would not be wrong.
The different values you may see for uninitialized bytes with different initialization methods, in different scopes and contexts are irrelevant, case specific and not defined. However, I cannot comment on 515 as it is not clear to me how you get that value.
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
@cdummy, updated the answer
– perreal
Mar 22 '12 at 15:49
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%2f9824641%2funion-initialization-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Reading from a member of the union other than the one most recently written to outside the "byte footprint" of the member to which you have most recently written leads to undefined unspecified behavior. You should not be reading i until after you have written to it: Whatever you see there is non-portable junk.
EDIT 1 Edited in response to Cristoph's comment.
3
this is a common misconception - reading from a union member you haven't written to last is only illegal in C++; however, the value of a member is unspecified if the last store happened to a smaller one
– Christoph
Mar 22 '12 at 15:00
Yes, it is only undefined behavior if the contents of the object when interpreted as the type that you are accessing it is a trap representation. First, such trap representations are rare nowadays, and second you'd really have bad luck to see it.
– Jens Gustedt
Mar 22 '12 at 15:04
@Christoph Thanks for the correction! I've been out of the "pure C" world long enough to start thinking that C is a subset of C++ :)
– dasblinkenlight
Mar 22 '12 at 15:08
add a comment |
Reading from a member of the union other than the one most recently written to outside the "byte footprint" of the member to which you have most recently written leads to undefined unspecified behavior. You should not be reading i until after you have written to it: Whatever you see there is non-portable junk.
EDIT 1 Edited in response to Cristoph's comment.
3
this is a common misconception - reading from a union member you haven't written to last is only illegal in C++; however, the value of a member is unspecified if the last store happened to a smaller one
– Christoph
Mar 22 '12 at 15:00
Yes, it is only undefined behavior if the contents of the object when interpreted as the type that you are accessing it is a trap representation. First, such trap representations are rare nowadays, and second you'd really have bad luck to see it.
– Jens Gustedt
Mar 22 '12 at 15:04
@Christoph Thanks for the correction! I've been out of the "pure C" world long enough to start thinking that C is a subset of C++ :)
– dasblinkenlight
Mar 22 '12 at 15:08
add a comment |
Reading from a member of the union other than the one most recently written to outside the "byte footprint" of the member to which you have most recently written leads to undefined unspecified behavior. You should not be reading i until after you have written to it: Whatever you see there is non-portable junk.
EDIT 1 Edited in response to Cristoph's comment.
Reading from a member of the union other than the one most recently written to outside the "byte footprint" of the member to which you have most recently written leads to undefined unspecified behavior. You should not be reading i until after you have written to it: Whatever you see there is non-portable junk.
EDIT 1 Edited in response to Cristoph's comment.
edited Mar 22 '12 at 15:05
answered Mar 22 '12 at 14:54
dasblinkenlightdasblinkenlight
615k617861205
615k617861205
3
this is a common misconception - reading from a union member you haven't written to last is only illegal in C++; however, the value of a member is unspecified if the last store happened to a smaller one
– Christoph
Mar 22 '12 at 15:00
Yes, it is only undefined behavior if the contents of the object when interpreted as the type that you are accessing it is a trap representation. First, such trap representations are rare nowadays, and second you'd really have bad luck to see it.
– Jens Gustedt
Mar 22 '12 at 15:04
@Christoph Thanks for the correction! I've been out of the "pure C" world long enough to start thinking that C is a subset of C++ :)
– dasblinkenlight
Mar 22 '12 at 15:08
add a comment |
3
this is a common misconception - reading from a union member you haven't written to last is only illegal in C++; however, the value of a member is unspecified if the last store happened to a smaller one
– Christoph
Mar 22 '12 at 15:00
Yes, it is only undefined behavior if the contents of the object when interpreted as the type that you are accessing it is a trap representation. First, such trap representations are rare nowadays, and second you'd really have bad luck to see it.
– Jens Gustedt
Mar 22 '12 at 15:04
@Christoph Thanks for the correction! I've been out of the "pure C" world long enough to start thinking that C is a subset of C++ :)
– dasblinkenlight
Mar 22 '12 at 15:08
3
3
this is a common misconception - reading from a union member you haven't written to last is only illegal in C++; however, the value of a member is unspecified if the last store happened to a smaller one
– Christoph
Mar 22 '12 at 15:00
this is a common misconception - reading from a union member you haven't written to last is only illegal in C++; however, the value of a member is unspecified if the last store happened to a smaller one
– Christoph
Mar 22 '12 at 15:00
Yes, it is only undefined behavior if the contents of the object when interpreted as the type that you are accessing it is a trap representation. First, such trap representations are rare nowadays, and second you'd really have bad luck to see it.
– Jens Gustedt
Mar 22 '12 at 15:04
Yes, it is only undefined behavior if the contents of the object when interpreted as the type that you are accessing it is a trap representation. First, such trap representations are rare nowadays, and second you'd really have bad luck to see it.
– Jens Gustedt
Mar 22 '12 at 15:04
@Christoph Thanks for the correction! I've been out of the "pure C" world long enough to start thinking that C is a subset of C++ :)
– dasblinkenlight
Mar 22 '12 at 15:08
@Christoph Thanks for the correction! I've been out of the "pure C" world long enough to start thinking that C is a subset of C++ :)
– dasblinkenlight
Mar 22 '12 at 15:08
add a comment |
Your assignment through the char could lead to undefined behavior if the new value happens to be a trap representation (rare) for the type int.
Your example with the union is not an initialization but only an assignment and thus it only changes exactly the bytes that you are accessing and the other stay with unspecific values. For unions it is always a good idea to do an initialization of the widest member something like
union a ob = { .i = 0 };
Thereby you can guarantee that all bytes of your object are initialized by 0.
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
@cdummy, if you don't initialize the variable (on the stack or allocated withmalloc) any value that happens to be in there is used for theintvalue. This is completely arbitrary. Only the twocharyou change directly have a value that you control.
– Jens Gustedt
Mar 22 '12 at 15:44
whether it is same for calloc
– cdummy
Mar 22 '12 at 15:49
With calloc you would be certain to get 515 (0x203, as you set the char array) because the upper bytes of the int would have been cleared by calloc.
– William Morris
Mar 22 '12 at 16:52
thanks dude....
– cdummy
Mar 22 '12 at 17:24
add a comment |
Your assignment through the char could lead to undefined behavior if the new value happens to be a trap representation (rare) for the type int.
Your example with the union is not an initialization but only an assignment and thus it only changes exactly the bytes that you are accessing and the other stay with unspecific values. For unions it is always a good idea to do an initialization of the widest member something like
union a ob = { .i = 0 };
Thereby you can guarantee that all bytes of your object are initialized by 0.
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
@cdummy, if you don't initialize the variable (on the stack or allocated withmalloc) any value that happens to be in there is used for theintvalue. This is completely arbitrary. Only the twocharyou change directly have a value that you control.
– Jens Gustedt
Mar 22 '12 at 15:44
whether it is same for calloc
– cdummy
Mar 22 '12 at 15:49
With calloc you would be certain to get 515 (0x203, as you set the char array) because the upper bytes of the int would have been cleared by calloc.
– William Morris
Mar 22 '12 at 16:52
thanks dude....
– cdummy
Mar 22 '12 at 17:24
add a comment |
Your assignment through the char could lead to undefined behavior if the new value happens to be a trap representation (rare) for the type int.
Your example with the union is not an initialization but only an assignment and thus it only changes exactly the bytes that you are accessing and the other stay with unspecific values. For unions it is always a good idea to do an initialization of the widest member something like
union a ob = { .i = 0 };
Thereby you can guarantee that all bytes of your object are initialized by 0.
Your assignment through the char could lead to undefined behavior if the new value happens to be a trap representation (rare) for the type int.
Your example with the union is not an initialization but only an assignment and thus it only changes exactly the bytes that you are accessing and the other stay with unspecific values. For unions it is always a good idea to do an initialization of the widest member something like
union a ob = { .i = 0 };
Thereby you can guarantee that all bytes of your object are initialized by 0.
answered Mar 22 '12 at 15:11
Jens GustedtJens Gustedt
65.2k272141
65.2k272141
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
@cdummy, if you don't initialize the variable (on the stack or allocated withmalloc) any value that happens to be in there is used for theintvalue. This is completely arbitrary. Only the twocharyou change directly have a value that you control.
– Jens Gustedt
Mar 22 '12 at 15:44
whether it is same for calloc
– cdummy
Mar 22 '12 at 15:49
With calloc you would be certain to get 515 (0x203, as you set the char array) because the upper bytes of the int would have been cleared by calloc.
– William Morris
Mar 22 '12 at 16:52
thanks dude....
– cdummy
Mar 22 '12 at 17:24
add a comment |
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
@cdummy, if you don't initialize the variable (on the stack or allocated withmalloc) any value that happens to be in there is used for theintvalue. This is completely arbitrary. Only the twocharyou change directly have a value that you control.
– Jens Gustedt
Mar 22 '12 at 15:44
whether it is same for calloc
– cdummy
Mar 22 '12 at 15:49
With calloc you would be certain to get 515 (0x203, as you set the char array) because the upper bytes of the int would have been cleared by calloc.
– William Morris
Mar 22 '12 at 16:52
thanks dude....
– cdummy
Mar 22 '12 at 17:24
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
@cdummy, if you don't initialize the variable (on the stack or allocated with
malloc) any value that happens to be in there is used for the int value. This is completely arbitrary. Only the two char you change directly have a value that you control.– Jens Gustedt
Mar 22 '12 at 15:44
@cdummy, if you don't initialize the variable (on the stack or allocated with
malloc) any value that happens to be in there is used for the int value. This is completely arbitrary. Only the two char you change directly have a value that you control.– Jens Gustedt
Mar 22 '12 at 15:44
whether it is same for calloc
– cdummy
Mar 22 '12 at 15:49
whether it is same for calloc
– cdummy
Mar 22 '12 at 15:49
With calloc you would be certain to get 515 (0x203, as you set the char array) because the upper bytes of the int would have been cleared by calloc.
– William Morris
Mar 22 '12 at 16:52
With calloc you would be certain to get 515 (0x203, as you set the char array) because the upper bytes of the int would have been cleared by calloc.
– William Morris
Mar 22 '12 at 16:52
thanks dude....
– cdummy
Mar 22 '12 at 17:24
thanks dude....
– cdummy
Mar 22 '12 at 17:24
add a comment |
i think that 99 is the result that shows that the higher bits are not
intialized.. am i correct?..
Correct because you only assign to two bytes explicitly in your second example, so two bytes of the integer remain uninitialized. In the first example you assign 4 to i, which is an integer and shares a byte with c. However, If both union members are of same type, then assuming that both will be initialized is correct. Also, the space allocated for a union is the space taken by its largest member so assuming some of the bytes of i will change when you assign to c[x] would not be wrong.
The different values you may see for uninitialized bytes with different initialization methods, in different scopes and contexts are irrelevant, case specific and not defined. However, I cannot comment on 515 as it is not clear to me how you get that value.
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
@cdummy, updated the answer
– perreal
Mar 22 '12 at 15:49
add a comment |
i think that 99 is the result that shows that the higher bits are not
intialized.. am i correct?..
Correct because you only assign to two bytes explicitly in your second example, so two bytes of the integer remain uninitialized. In the first example you assign 4 to i, which is an integer and shares a byte with c. However, If both union members are of same type, then assuming that both will be initialized is correct. Also, the space allocated for a union is the space taken by its largest member so assuming some of the bytes of i will change when you assign to c[x] would not be wrong.
The different values you may see for uninitialized bytes with different initialization methods, in different scopes and contexts are irrelevant, case specific and not defined. However, I cannot comment on 515 as it is not clear to me how you get that value.
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
@cdummy, updated the answer
– perreal
Mar 22 '12 at 15:49
add a comment |
i think that 99 is the result that shows that the higher bits are not
intialized.. am i correct?..
Correct because you only assign to two bytes explicitly in your second example, so two bytes of the integer remain uninitialized. In the first example you assign 4 to i, which is an integer and shares a byte with c. However, If both union members are of same type, then assuming that both will be initialized is correct. Also, the space allocated for a union is the space taken by its largest member so assuming some of the bytes of i will change when you assign to c[x] would not be wrong.
The different values you may see for uninitialized bytes with different initialization methods, in different scopes and contexts are irrelevant, case specific and not defined. However, I cannot comment on 515 as it is not clear to me how you get that value.
i think that 99 is the result that shows that the higher bits are not
intialized.. am i correct?..
Correct because you only assign to two bytes explicitly in your second example, so two bytes of the integer remain uninitialized. In the first example you assign 4 to i, which is an integer and shares a byte with c. However, If both union members are of same type, then assuming that both will be initialized is correct. Also, the space allocated for a union is the space taken by its largest member so assuming some of the bytes of i will change when you assign to c[x] would not be wrong.
The different values you may see for uninitialized bytes with different initialization methods, in different scopes and contexts are irrelevant, case specific and not defined. However, I cannot comment on 515 as it is not clear to me how you get that value.
edited Mar 22 '12 at 15:49
answered Mar 22 '12 at 15:03
perrealperreal
72.4k9111138
72.4k9111138
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
@cdummy, updated the answer
– perreal
Mar 22 '12 at 15:49
add a comment |
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
@cdummy, updated the answer
– perreal
Mar 22 '12 at 15:49
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
could you comment about using malloc leaves the value of int to 515.
– cdummy
Mar 22 '12 at 15:38
@cdummy, updated the answer
– perreal
Mar 22 '12 at 15:49
@cdummy, updated the answer
– perreal
Mar 22 '12 at 15:49
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%2f9824641%2funion-initialization-in-c%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
Reading from the field of the union to which you did not write is undefined behavior. You could see arbitrary data there, it worth nothing that your particular compiler does it one way or the other.
– dasblinkenlight
Mar 22 '12 at 14:51
2
@dasblinkenlight: reading a member you did not write to is defined behaviour -- however, if that member is represented by bytes which did not partake in the last store (ie if the accessed member is of greater size than the member used in the last store), the behaviour is unspecified; see stackoverflow.com/a/8513748/48015 for details
– Christoph
Mar 22 '12 at 14:54
You may be right, have a look at the assembly output from your compiler to make sure.
– Gowtham
Mar 22 '12 at 14:54
could you comment about using malloc leaves the value of int to 515..
– cdummy
Mar 22 '12 at 15:05