By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,251 Members | 2,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

Why does the rvalue change with assignment?

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.