Why does the rvalue change with assignment?

H

Hemant Mohan

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
 
J

Jack Klein

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,843
Latest member
WizcraftEntertainmentAgen

Latest Threads

Top