list = list->Next = malloc(sizeof(node));

A

Army1987

Given:
#include <stdlib.h>
typedef struct Node {
unsigned char Data[SIZE];
struct Node *Next
} node_t, *list_t;
list_t list;

Does
list = list->Next = malloc(sizeof(node));
cause UB? Is it any different (neglecting style) from
list->Next = malloc(sizeof(node));
list = list->Next;
?

In the former, apparently I only write to l itself once, and only
read l to determine what l->next is. So apparently it is OK to do
that within one sequence point. Am I wrong?
 
B

Ben Pfaff

Army1987 said:
Does
list = list->Next = malloc(sizeof(node));
cause UB? Is it any different (neglecting style) from
list->Next = malloc(sizeof(node));
list = list->Next;
?

We've had several extensive threads on this over the years. I
started one of them. I'd suggest trying to find the earlier
discussions instead of starting a new one.
 
C

christian.bau

In the former, apparently I only write to l itself once, and only
read l to determine what l->next is. So apparently it is OK to do
that within one sequence point. Am I wrong?

You are not sure whether your code invokes undefined behavior. It is
trivial to change it so that it will clearly not invoke undefined
behavior. If you don't change it, then yes, you are wrong.
 
O

Old Wolf

Does
list = list->Next = malloc(sizeof(node));
cause UB?

Yes. 'list' is both written, and read for a purpose
other than to determine the value to be written,
without an intervening sequence point. (That purpose
is for determining the value of list->Next).

Although '=' is right-left associative, that doesn't
mean right-left order of evaluation. The associativity
means that A=B=C is read as A=(B=C) rather than (A=B)=C.
BTW. the latter would always cause undefined behaviour
because A would be written twice without a sequence point.

The code is like:
void *x = malloc( sizeof(node) );
list = x, list->Next = x;

except that there is no sequence point where I have
put the comma operator.

So you could end up with the result of malloc having
"->Next" applied to it. (Or any other result, since
this means the behaviour is undefined).
 
R

Richard Tobin

Does
list = list->Next = malloc(sizeof(node));
cause UB?
[/QUOTE]
Yes. 'list' is both written, and read for a purpose
other than to determine the value to be written,
without an intervening sequence point. (That purpose
is for determining the value of list->Next).

That's not quite right, because the same is true of

list = list->Next;

in which list is read to determine the value of list->Next, but that
is done in order to determine the value to be written, so there is no
problem. The problem with the quoted example is that list is read in
order to determine the *location* to be written in the right-hand
assignment.

-- Richard
 

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,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top