Understanding object representation [duplicate] Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceCan memcpy be used for type punning?Purpose of Unions in C and C++Is type-punning through a union unspecified in C99, and has it become specified in C11?Unable to access Struct contents properlySizeof Structure in CWhat does “representable” mean in C11?Difference between object and value representation by exampleReturning a local partially initialized struct from a function and undefined behaviorUnderallocating memory for a unionWhy value stored in struct and union object correspond to any padding bytes take unspecified values?Trap representation for structures
Classification of bundles, Postnikov towers, obstruction theory, local coefficients
Single author papers against my advisor's will?
When is phishing education going too far?
How to rotate it perfectly?
What do you call a plan that's an alternative plan in case your initial plan fails?
How to add zeros to reach same number of decimal places in tables?
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
What is the largest species of polychaete?
I'm having difficulty getting my players to do stuff in a sandbox campaign
What LEGO pieces have "real-world" functionality?
Can a 1st-level character have an ability score above 18?
Limit for e and 1/e
Should you tell Jews they are breaking a commandment?
Is above average number of years spent on PhD considered a red flag in future academia or industry positions?
Writing Thesis: Copying from published papers
How should I respond to a player wanting to catch a sword between their hands?
Statistical model of ligand substitution
Was credit for the black hole image misattributed?
Stop battery usage [Ubuntu 18]
What are the performance impacts of 'functional' Rust?
Interesting examples of non-locally compact topological groups
Strange behaviour of Check
Windows 10: How to Lock (not sleep) laptop on lid close?
Fishing simulator
Understanding object representation [duplicate]
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceCan memcpy be used for type punning?Purpose of Unions in C and C++Is type-punning through a union unspecified in C99, and has it become specified in C11?Unable to access Struct contents properlySizeof Structure in CWhat does “representable” mean in C11?Difference between object and value representation by exampleReturning a local partially initialized struct from a function and undefined behaviorUnderallocating memory for a unionWhy value stored in struct and union object correspond to any padding bytes take unspecified values?Trap representation for structures
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
Can memcpy be used for type punning?
5 answers
Question: If we have two non-compatible structures or unions, but objects of both of the types have the same size of object representation would I get Undefined/Unspecified/Well-defined Behavior if I take object representation of some object of one of the types and "reinterpret" it as another type. (I do hope the wording is not weird).
My thoughts:
I mentioned the structure or union because N6.2.6.1(p6):
The value of a structure or union object is never a trap
Also I found that we can copy a value of an object into a char array 6.2.6.1(p4):
The value may be copied into an object of type
unsigned char [n]
(e.g., bymemcpy); the resulting set of bytes is called the object
representation of the value.
But the Standard does not specify that we can copy the object representation back. So I think it is UB to copy object representation back into an object of a type having representation of the same size (even if it's not a trap), but I'm not sure...
Example:
struct test1_t
int a;
long b;
;
struct test2_t
int c;
int d;
int e;
int f;
;
int main()
struct test1_t t1 = .a = 100, .b = 2000;
struct test2_t t2 = .c = 1000, .d = 20000, .e = 300000, .f = 4000000;
size_t size;
if((size = sizeof(struct test1_t)) == sizeof(struct test2_t))
char repr[size];
memcpy(&repr, &t2, size); // since value of structure or union
// is never a trap why don't we treat
// the representation as of some object
// of type struct test_t1
memcpy(&t1, &repr, size);
printf("t1.a = %dn", t1.a); //t1.a = 1000
printf("t1.b = %dn", t1.b); //t1.b = 300000
The result can be explained with padding in struct test1_t after int a;.
c struct language-lawyer
marked as duplicate by EOF, Community♦ Mar 9 at 16:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Can memcpy be used for type punning?
5 answers
Question: If we have two non-compatible structures or unions, but objects of both of the types have the same size of object representation would I get Undefined/Unspecified/Well-defined Behavior if I take object representation of some object of one of the types and "reinterpret" it as another type. (I do hope the wording is not weird).
My thoughts:
I mentioned the structure or union because N6.2.6.1(p6):
The value of a structure or union object is never a trap
Also I found that we can copy a value of an object into a char array 6.2.6.1(p4):
The value may be copied into an object of type
unsigned char [n]
(e.g., bymemcpy); the resulting set of bytes is called the object
representation of the value.
But the Standard does not specify that we can copy the object representation back. So I think it is UB to copy object representation back into an object of a type having representation of the same size (even if it's not a trap), but I'm not sure...
Example:
struct test1_t
int a;
long b;
;
struct test2_t
int c;
int d;
int e;
int f;
;
int main()
struct test1_t t1 = .a = 100, .b = 2000;
struct test2_t t2 = .c = 1000, .d = 20000, .e = 300000, .f = 4000000;
size_t size;
if((size = sizeof(struct test1_t)) == sizeof(struct test2_t))
char repr[size];
memcpy(&repr, &t2, size); // since value of structure or union
// is never a trap why don't we treat
// the representation as of some object
// of type struct test_t1
memcpy(&t1, &repr, size);
printf("t1.a = %dn", t1.a); //t1.a = 1000
printf("t1.b = %dn", t1.b); //t1.b = 300000
The result can be explained with padding in struct test1_t after int a;.
c struct language-lawyer
marked as duplicate by EOF, Community♦ Mar 9 at 16:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
@EOF As mentioned in the referenced answer, we can usememcpyfor type punning and in my case the behavior is well-defined sincet1has declared typestruct test1_twhich is an effective type as well.
– Some Name
Mar 9 at 13:23
add a comment |
This question already has an answer here:
Can memcpy be used for type punning?
5 answers
Question: If we have two non-compatible structures or unions, but objects of both of the types have the same size of object representation would I get Undefined/Unspecified/Well-defined Behavior if I take object representation of some object of one of the types and "reinterpret" it as another type. (I do hope the wording is not weird).
My thoughts:
I mentioned the structure or union because N6.2.6.1(p6):
The value of a structure or union object is never a trap
Also I found that we can copy a value of an object into a char array 6.2.6.1(p4):
The value may be copied into an object of type
unsigned char [n]
(e.g., bymemcpy); the resulting set of bytes is called the object
representation of the value.
But the Standard does not specify that we can copy the object representation back. So I think it is UB to copy object representation back into an object of a type having representation of the same size (even if it's not a trap), but I'm not sure...
Example:
struct test1_t
int a;
long b;
;
struct test2_t
int c;
int d;
int e;
int f;
;
int main()
struct test1_t t1 = .a = 100, .b = 2000;
struct test2_t t2 = .c = 1000, .d = 20000, .e = 300000, .f = 4000000;
size_t size;
if((size = sizeof(struct test1_t)) == sizeof(struct test2_t))
char repr[size];
memcpy(&repr, &t2, size); // since value of structure or union
// is never a trap why don't we treat
// the representation as of some object
// of type struct test_t1
memcpy(&t1, &repr, size);
printf("t1.a = %dn", t1.a); //t1.a = 1000
printf("t1.b = %dn", t1.b); //t1.b = 300000
The result can be explained with padding in struct test1_t after int a;.
c struct language-lawyer
This question already has an answer here:
Can memcpy be used for type punning?
5 answers
Question: If we have two non-compatible structures or unions, but objects of both of the types have the same size of object representation would I get Undefined/Unspecified/Well-defined Behavior if I take object representation of some object of one of the types and "reinterpret" it as another type. (I do hope the wording is not weird).
My thoughts:
I mentioned the structure or union because N6.2.6.1(p6):
The value of a structure or union object is never a trap
Also I found that we can copy a value of an object into a char array 6.2.6.1(p4):
The value may be copied into an object of type
unsigned char [n]
(e.g., bymemcpy); the resulting set of bytes is called the object
representation of the value.
But the Standard does not specify that we can copy the object representation back. So I think it is UB to copy object representation back into an object of a type having representation of the same size (even if it's not a trap), but I'm not sure...
Example:
struct test1_t
int a;
long b;
;
struct test2_t
int c;
int d;
int e;
int f;
;
int main()
struct test1_t t1 = .a = 100, .b = 2000;
struct test2_t t2 = .c = 1000, .d = 20000, .e = 300000, .f = 4000000;
size_t size;
if((size = sizeof(struct test1_t)) == sizeof(struct test2_t))
char repr[size];
memcpy(&repr, &t2, size); // since value of structure or union
// is never a trap why don't we treat
// the representation as of some object
// of type struct test_t1
memcpy(&t1, &repr, size);
printf("t1.a = %dn", t1.a); //t1.a = 1000
printf("t1.b = %dn", t1.b); //t1.b = 300000
The result can be explained with padding in struct test1_t after int a;.
This question already has an answer here:
Can memcpy be used for type punning?
5 answers
c struct language-lawyer
c struct language-lawyer
asked Mar 8 at 14:50
Some NameSome Name
1,830418
1,830418
marked as duplicate by EOF, Community♦ Mar 9 at 16:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by EOF, Community♦ Mar 9 at 16:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
@EOF As mentioned in the referenced answer, we can usememcpyfor type punning and in my case the behavior is well-defined sincet1has declared typestruct test1_twhich is an effective type as well.
– Some Name
Mar 9 at 13:23
add a comment |
@EOF As mentioned in the referenced answer, we can usememcpyfor type punning and in my case the behavior is well-defined sincet1has declared typestruct test1_twhich is an effective type as well.
– Some Name
Mar 9 at 13:23
@EOF As mentioned in the referenced answer, we can use
memcpy for type punning and in my case the behavior is well-defined since t1 has declared type struct test1_t which is an effective type as well.– Some Name
Mar 9 at 13:23
@EOF As mentioned in the referenced answer, we can use
memcpy for type punning and in my case the behavior is well-defined since t1 has declared type struct test1_t which is an effective type as well.– Some Name
Mar 9 at 13:23
add a comment |
1 Answer
1
active
oldest
votes
If we have two non-compatible structures or unions, but objects of
both of the types have the same size of object representation would I
get Undefined/Unspecified/Well-defined Behavior if I take object
representation of some object of one of the types and "reinterpret" it
as another type.
The question Can memcpy be used for type punning? and especially EOF's answer to it contain considerable relevant information and discussion, though they focus on different specifics. In particular,
- the
memcpy()itself is fine as long as the two types indeed have the same size (which appears unlikely to be the case in your example code), and - from there, it depends in part on whether the receiving object has a declared type.
- if it does not, then subsequently accessing it via an lvalue of type different than the effective type of the source object produces UB.
- if it does, as in your example, then accessing the structure itself is ok, for as you observed, structure types do not have trap representations.
HOWEVER, in the case of structures and unions, we also have to consider members. Their types may afford trap representations, and if they do, then the memcpy() may result in one or more members containing such a representation. Reading the value of a member that currently contains a trap representation produces UB.
Furthermore, even supposing that the two structure types in your example have the same size, relevant details of their layouts are unspecified. In particular, in your example code it would be surprising if both (1) the two structures have the same size and (2) the second printf call prints the result suggested by the code comment. (After correcting the format to match the data correctly; presently the printf has UB on account of the mismatch between format and variable.)
Overall, then, your example code is not strictly conforming. Aspects of its behavior are unspecified, and it is possible, but not certain, that it exhibits undefined behavior.
which appears unlikely to be the case in your example code But they do. I checked thesizeofoutput for both of them. It was equal to 16 on my machine. I guessed the padding took place forstruct test1_t::a.
– Some Name
Mar 8 at 17:28
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If we have two non-compatible structures or unions, but objects of
both of the types have the same size of object representation would I
get Undefined/Unspecified/Well-defined Behavior if I take object
representation of some object of one of the types and "reinterpret" it
as another type.
The question Can memcpy be used for type punning? and especially EOF's answer to it contain considerable relevant information and discussion, though they focus on different specifics. In particular,
- the
memcpy()itself is fine as long as the two types indeed have the same size (which appears unlikely to be the case in your example code), and - from there, it depends in part on whether the receiving object has a declared type.
- if it does not, then subsequently accessing it via an lvalue of type different than the effective type of the source object produces UB.
- if it does, as in your example, then accessing the structure itself is ok, for as you observed, structure types do not have trap representations.
HOWEVER, in the case of structures and unions, we also have to consider members. Their types may afford trap representations, and if they do, then the memcpy() may result in one or more members containing such a representation. Reading the value of a member that currently contains a trap representation produces UB.
Furthermore, even supposing that the two structure types in your example have the same size, relevant details of their layouts are unspecified. In particular, in your example code it would be surprising if both (1) the two structures have the same size and (2) the second printf call prints the result suggested by the code comment. (After correcting the format to match the data correctly; presently the printf has UB on account of the mismatch between format and variable.)
Overall, then, your example code is not strictly conforming. Aspects of its behavior are unspecified, and it is possible, but not certain, that it exhibits undefined behavior.
which appears unlikely to be the case in your example code But they do. I checked thesizeofoutput for both of them. It was equal to 16 on my machine. I guessed the padding took place forstruct test1_t::a.
– Some Name
Mar 8 at 17:28
add a comment |
If we have two non-compatible structures or unions, but objects of
both of the types have the same size of object representation would I
get Undefined/Unspecified/Well-defined Behavior if I take object
representation of some object of one of the types and "reinterpret" it
as another type.
The question Can memcpy be used for type punning? and especially EOF's answer to it contain considerable relevant information and discussion, though they focus on different specifics. In particular,
- the
memcpy()itself is fine as long as the two types indeed have the same size (which appears unlikely to be the case in your example code), and - from there, it depends in part on whether the receiving object has a declared type.
- if it does not, then subsequently accessing it via an lvalue of type different than the effective type of the source object produces UB.
- if it does, as in your example, then accessing the structure itself is ok, for as you observed, structure types do not have trap representations.
HOWEVER, in the case of structures and unions, we also have to consider members. Their types may afford trap representations, and if they do, then the memcpy() may result in one or more members containing such a representation. Reading the value of a member that currently contains a trap representation produces UB.
Furthermore, even supposing that the two structure types in your example have the same size, relevant details of their layouts are unspecified. In particular, in your example code it would be surprising if both (1) the two structures have the same size and (2) the second printf call prints the result suggested by the code comment. (After correcting the format to match the data correctly; presently the printf has UB on account of the mismatch between format and variable.)
Overall, then, your example code is not strictly conforming. Aspects of its behavior are unspecified, and it is possible, but not certain, that it exhibits undefined behavior.
which appears unlikely to be the case in your example code But they do. I checked thesizeofoutput for both of them. It was equal to 16 on my machine. I guessed the padding took place forstruct test1_t::a.
– Some Name
Mar 8 at 17:28
add a comment |
If we have two non-compatible structures or unions, but objects of
both of the types have the same size of object representation would I
get Undefined/Unspecified/Well-defined Behavior if I take object
representation of some object of one of the types and "reinterpret" it
as another type.
The question Can memcpy be used for type punning? and especially EOF's answer to it contain considerable relevant information and discussion, though they focus on different specifics. In particular,
- the
memcpy()itself is fine as long as the two types indeed have the same size (which appears unlikely to be the case in your example code), and - from there, it depends in part on whether the receiving object has a declared type.
- if it does not, then subsequently accessing it via an lvalue of type different than the effective type of the source object produces UB.
- if it does, as in your example, then accessing the structure itself is ok, for as you observed, structure types do not have trap representations.
HOWEVER, in the case of structures and unions, we also have to consider members. Their types may afford trap representations, and if they do, then the memcpy() may result in one or more members containing such a representation. Reading the value of a member that currently contains a trap representation produces UB.
Furthermore, even supposing that the two structure types in your example have the same size, relevant details of their layouts are unspecified. In particular, in your example code it would be surprising if both (1) the two structures have the same size and (2) the second printf call prints the result suggested by the code comment. (After correcting the format to match the data correctly; presently the printf has UB on account of the mismatch between format and variable.)
Overall, then, your example code is not strictly conforming. Aspects of its behavior are unspecified, and it is possible, but not certain, that it exhibits undefined behavior.
If we have two non-compatible structures or unions, but objects of
both of the types have the same size of object representation would I
get Undefined/Unspecified/Well-defined Behavior if I take object
representation of some object of one of the types and "reinterpret" it
as another type.
The question Can memcpy be used for type punning? and especially EOF's answer to it contain considerable relevant information and discussion, though they focus on different specifics. In particular,
- the
memcpy()itself is fine as long as the two types indeed have the same size (which appears unlikely to be the case in your example code), and - from there, it depends in part on whether the receiving object has a declared type.
- if it does not, then subsequently accessing it via an lvalue of type different than the effective type of the source object produces UB.
- if it does, as in your example, then accessing the structure itself is ok, for as you observed, structure types do not have trap representations.
HOWEVER, in the case of structures and unions, we also have to consider members. Their types may afford trap representations, and if they do, then the memcpy() may result in one or more members containing such a representation. Reading the value of a member that currently contains a trap representation produces UB.
Furthermore, even supposing that the two structure types in your example have the same size, relevant details of their layouts are unspecified. In particular, in your example code it would be surprising if both (1) the two structures have the same size and (2) the second printf call prints the result suggested by the code comment. (After correcting the format to match the data correctly; presently the printf has UB on account of the mismatch between format and variable.)
Overall, then, your example code is not strictly conforming. Aspects of its behavior are unspecified, and it is possible, but not certain, that it exhibits undefined behavior.
answered Mar 8 at 17:22
John BollingerJohn Bollinger
85.6k74279
85.6k74279
which appears unlikely to be the case in your example code But they do. I checked thesizeofoutput for both of them. It was equal to 16 on my machine. I guessed the padding took place forstruct test1_t::a.
– Some Name
Mar 8 at 17:28
add a comment |
which appears unlikely to be the case in your example code But they do. I checked thesizeofoutput for both of them. It was equal to 16 on my machine. I guessed the padding took place forstruct test1_t::a.
– Some Name
Mar 8 at 17:28
which appears unlikely to be the case in your example code But they do. I checked the
sizeof output for both of them. It was equal to 16 on my machine. I guessed the padding took place for struct test1_t::a.– Some Name
Mar 8 at 17:28
which appears unlikely to be the case in your example code But they do. I checked the
sizeof output for both of them. It was equal to 16 on my machine. I guessed the padding took place for struct test1_t::a.– Some Name
Mar 8 at 17:28
add a comment |
@EOF As mentioned in the referenced answer, we can use
memcpyfor type punning and in my case the behavior is well-defined sincet1has declared typestruct test1_twhich is an effective type as well.– Some Name
Mar 9 at 13:23