473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Does casting lvalue lead to Undefined Behaviour ?

Please see the code below

-- start listing is_it_ub.c --

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
unsigned char buff[20];
unsigned int i;

i = 0xaabbccddUL;
*((int *)buff) = i; /* Is this UB ? */

printf("buff: %x:%x:%x:%x\n", buff[0], buff[1], buff[2], buff[3]);

return EXIT_SUCCESS;
}

-- end listing --

Output:
buff: dd:cc:bb:aa

Output seems correct on my Little Endian PC.

In the statement: "*((int *)buff) = i; ", buff is casted to be treated
as an int *. Is this valid?

My compiler does not produce any diagnostics for the above
program but somebody else complained that their compiler
warns "casting of lvalue is deprecated".

Thanks.

Dec 29 '06 #1
5 1256
p_***********@yahoo.co.in wrote:
Please see the code below

-- start listing is_it_ub.c --

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
unsigned char buff[20];
unsigned int i;

i = 0xaabbccddUL;
*((int *)buff) = i; /* Is this UB ? */
Yes. There is no guarantee that buff is correctly aligned for an int.
You can either use memcpy(), or access the representation via (unsigned
char *) &i.

In theory, it's also possible that sizeof(int) is greater than 20,
which will cause obvious problems, but that's not likely to happen in
practice.
printf("buff: %x:%x:%x:%x\n", buff[0], buff[1], buff[2], buff[3]);
This assumes that an int is four bytes. This is not necessarily true,
and in this case the problem does occur in practice.
return EXIT_SUCCESS;
}

-- end listing --

Output:
buff: dd:cc:bb:aa

Output seems correct on my Little Endian PC.

In the statement: "*((int *)buff) = i; ", buff is casted to be treated
as an int *. Is this valid?

My compiler does not produce any diagnostics for the above
program but somebody else complained that their compiler
warns "casting of lvalue is deprecated".
That warning refers to an implementation-specific extension which
you're not using, so don't worry about that.

Dec 29 '06 #2
p_***********@yahoo.co.in said:
Please see the code below

-- start listing is_it_ub.c --

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
unsigned char buff[20];
unsigned int i;

i = 0xaabbccddUL;
*((int *)buff) = i; /* Is this UB ? */
Yes. You're evaluating buff (which is a pointer to char), and then
converting that value to a pointer to int, but there is no guarantee that
it will be properly aligned. You then dereference the possibly-invalid
pointer thus obtained. On systems where this works, it's harmless. On
systems where it doesn't, we're talking potential bus errors, which are
definite showstoppers.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 29 '06 #3
In article <11**********************@a3g2000cwd.googlegroups. com>,
<p_***********@yahoo.co.inwrote:
unsigned char buff[20];
unsigned int i;
i = 0xaabbccddUL;
*((int *)buff) = i; /* Is this UB ? */
Yes, because you don't know that buff has been properly aligned
to write an int into the beginning of it.

Also, you are assuming that unsigned int is big enough to hold
something 32 bits wide; an unsigned int need only be 16 bits wide to
satisify the value constraints.

Your constant is written as an unsigned long and that is assigned
into the unsigned int. That's odd enough to stand out, but it should
not be a problem in practice: if unsigned long is wider than
unsigned int, the unsigned long will not have any sign extension,
and even if it did, that sign extension would effectively be removed
when assigned into the unsigned int.

Your assignment involves type punning on a plain int rather than
an unsigned int. There is the possibility that an arbitrary
unsigned int might become a trap representation when the same
bit pattern is used as an int; such problems become more -likely-
when the unsigned int has bits set that correspond to sign bits
in the int. The particular value you chose, 0xaabbccdd, is -likely-
to overlay the top bit of 0xaa or 0xcc onto the sign bit, so you -could-
be getting into trouble that way. Safer to convert through
unsigned int * .
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Dec 29 '06 #4
Walter Roberson wrote:
In article <11**********************@a3g2000cwd.googlegroups. com>,
<p_***********@yahoo.co.inwrote:
unsigned char buff[20];
unsigned int i;
i = 0xaabbccddUL;
*((int *)buff) = i; /* Is this UB ? */
[...]
Your assignment involves type punning on a plain int rather than
an unsigned int. There is the possibility that an arbitrary
unsigned int might become a trap representation when the same
bit pattern is used as an int; such problems become more -likely-
when the unsigned int has bits set that correspond to sign bits
in the int. The particular value you chose, 0xaabbccdd, is -likely-
to overlay the top bit of 0xaa or 0xcc onto the sign bit, so you -could-
be getting into trouble that way. Safer to convert through
unsigned int * .
There is no possibility for a trap representation. The value is
converted to, not reinterpreted as, a signed int, which results in an
implementation-defined value (and a trap representation, by definition,
is not a value), or the raising of an implementation-defined signal. In
the former case, there's no UB. In the latter case, there is UB if the
signal is not handled, but even then, there is no trap representation.

That said, since padding bits aren't an issue (you know the
representation is a valid unsigned int), the only trap representations
a signed integer can have are all bits one, and all bits zero except
for the sign bit. Neither is possible here.

Dec 29 '06 #5
Harald van Dijk wrote:
Walter Roberson wrote:
In article <11**********************@a3g2000cwd.googlegroups. com>,
<p_***********@yahoo.co.inwrote:
unsigned char buff[20];
unsigned int i;
i = 0xaabbccddUL;
*((int *)buff) = i; /* Is this UB ? */
[...]
Your assignment involves type punning on a plain int rather than
an unsigned int. There is the possibility that an arbitrary
unsigned int might become a trap representation when the same
bit pattern is used as an int; such problems become more -likely-
when the unsigned int has bits set that correspond to sign bits
in the int. The particular value you chose, 0xaabbccdd, is -likely-
to overlay the top bit of 0xaa or 0xcc onto the sign bit, so you -could-
be getting into trouble that way. Safer to convert through
unsigned int * .

There is no possibility for a trap representation. The value is
converted to, not reinterpreted as, a signed int, which results in an
implementation-defined value (and a trap representation, by definition,
is not a value), or the raising of an implementation-defined signal. In
the former case, there's no UB. In the latter case, there is UB if the
signal is not handled, but even then, there is no trap representation.
Correction: the wording actually only says the result is
implementation-defined, not that the result is an
implementation-defined value. However, this is no licence for
implementations to declare the result is undefined (as it would be in
the case of a trap representation).
That said, since padding bits aren't an issue (you know the
representation is a valid unsigned int), the only trap representations
a signed integer can have are all bits one, and all bits zero except
for the sign bit. Neither is possible here.
Dec 29 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: 127.0.0.1 | last post by:
Hello! #include <stdio.h> int main(void) { char *p = "hello, world"; printf("%p\n", p);
14
by: ozbear | last post by:
Someone was asking in another forum how to translate: if (x && (y1=(y+y1)/2)) /* all integers with defined values */ to another language. I am at odds with myself as to whether this causes...
24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
12
by: T Koster | last post by:
Hi lads, In implementing a hash table, I offer the option of storing arbitrarily-typed objects using a pointer-to-void. My hash function is as follows: unsigned int hashfn(void * key,...
14
by: Akhil | last post by:
plz c d following code #include<stdio.h> void main() { int x=4,y=1; y=x++++; //gives error message lvalue required y=x++ + ++y;//no errors
23
by: Christian Christmann | last post by:
Hi, a question on castings. My code example: #include <stdio.h> int main( void ) {
6
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
10
by: subramanian100in | last post by:
Consider the following code: #include <iostream> #include <cstdlib> using namespace std; int main() { const double& ref = 100;
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.