473,397 Members | 2,099 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,397 software developers and data experts.

Why does the rvalue change with assignment?

Consider the following program snipet:
<snip>

typedef struct
{
unsigned char a ;
unsigned char b ;
unsigned char c ;
unsigned char* d_ptr; /* Pointer to some data array */
} str_t ;

int main()
{

unsigned char arr[12] =
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B };

str_t *my_str = NULL;

my_str = (str_t*)(&arr[0]) ; /* first assignment */

my_str->d_ptr = &(arr[3]) ; /*second assignment*/

return 0;
}

</snip>

On execution of this program I notice that the "second assignment"
modifies the rvalue that is arr at indexes 4,5,6,7 to address of
my_str->d_ptr.
The d_ptr goes to index 4 instead of index 3 probabaly due to
allignment ( padding ) issues.

However, I am unable to explain why the rvalue in the array gets
modified, as normally assignments only modify the lvaue. Also how do I
overcome this problem.

My initial guess is that my_str->d_ptr is a pointer to a pointer and
due to this the rvalue also gets modified.

Thanks in advance
Hemant

Mar 24 '06 #1
1 2268
On 23 Mar 2006 21:25:06 -0800, "Hemant Mohan" <hm****@gmail.com> wrote
in comp.lang.c:
Consider the following program snipet:
My considered opinion is that it contains undefined behavior.
<snip>

typedef struct
{
unsigned char a ;
unsigned char b ;
unsigned char c ;
unsigned char* d_ptr; /* Pointer to some data array */
} str_t ;

int main()
{

unsigned char arr[12] =
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B };

str_t *my_str = NULL;
It is quite silly to initialize a pointer to NULL when you
unconditionally assign a value to it in the very next statement.
my_str = (str_t*)(&arr[0]) ; /* first assignment */
There is no guarantee that the address of arr is properly aligned to
point to an object of your structure type.
my_str->d_ptr = &(arr[3]) ; /*second assignment*/
Here you are violating the defined type of the object arr, producing
undefined behavior. Aside from the face that my_str might not be
aligned properly, or even that "my_str->d_ptr" might not be aligned
correctly to hole a pointer to char.
return 0;
}

</snip>

On execution of this program I notice that the "second assignment"
modifies the rvalue that is arr at indexes 4,5,6,7 to address of
There is no rvalue modified here. By definition, an rvalue can't be
modified. The array arr is an lvalue, although not a modifiable one
by name. Each individual element of the array arr is an lvalue as
well. Exactly what do you think is an rvalue here?
my_str->d_ptr.
The d_ptr goes to index 4 instead of index 3 probabaly due to
allignment ( padding ) issues.

However, I am unable to explain why the rvalue in the array gets
modified, as normally assignments only modify the lvaue. Also how do I
overcome this problem.
I am unable to comprehend what you mean by the "rvalue in the array",
as there are no rvalues in the array. By definition, an array
consists of one or more objects, and objects are lvalues, although
they might not be modifiable lvalues.
My initial guess is that my_str->d_ptr is a pointer to a pointer and
due to this the rvalue also gets modified.
I don't know what it is you are guessing about. The d_ptr member of
your structure is a pointer to char, not a pointer to pointer.
Thanks in advance
Hemant


You seem to have a strange, and incorrect, idea of what an rvalue is.

You defined a pointer to an object type. You pointed that pointer at
some memory. You wrote to that memory through that pointer. Assuming
that the alignment happens to be correct, and ignoring the undefined
behavior caused by ignoring the defined type of the memory, that
assignment through the pointer will modify the memory pointed to.

But no rvalue is modified. What exactly do you think an rvalue is?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 24 '06 #2

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

Similar topics

11
by: JKop | last post by:
The following compiles: // syrup.cpp struct DoubleInDisguise { double data; };
1
by: Tapeesh | last post by:
In C++, why does assignment operator always return a lvalue. Even in cases of assignment of basic datatypes like int ? For eg. int main() { int a, b, c; a = b + c; return 0;
9
by: Kavya | last post by:
These were the questions asked to me by my friend 1. Operator which may require an lvalue operand, yet yield an rvalue. 2. Operator which may require an rvalue operand, yet yield an lvalue. ...
7
by: shuisheng | last post by:
Dear All, I find in std::auto_ptr, the private member is define as template<class _Ty> class auto_ptr { .... private: const _Ty *_Myptr;
25
by: SRR | last post by:
Consider the following code: #include <stdio.h> #include <string.h> struct test{ char a; } funTest( void ); 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;
0
by: Jerry Coffin | last post by:
In article <9f60e411-a5b1-4571-9d3d-005432378cd4@ 56g2000hsm.googlegroups.com>, aitorf666@gmail.com says... That's not the real reason for rvalue references. There are two primary reasons for...
3
by: jerry.teshirogi | last post by:
I have the following class and main: ////////////////////////////////////////////////////////// #include <iostream.h> class myVector { public: double x, y, z:
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.