473,326 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,326 software developers and data experts.

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

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?
May 25 '07 #1
4 3079
"Army1987" <pl********@for.itwrites:
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.
--
Ben Pfaff
http://benpfaff.org
May 25 '07 #2
On May 25, 11:39 am, "Army1987" <please....@for.itwrote:
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.

May 26 '07 #3
On May 25, 10:39 pm, "Army1987" <please....@for.itwrote:
>
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).
May 28 '07 #4
In article <11**********************@n15g2000prd.googlegroups .com>,
Old Wolf <ol*****@inspire.net.nzwrote:
>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).
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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 28 '07 #5

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

Similar topics

6
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesn¹t matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
0
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
0
by: saijin | last post by:
I'm planning to call a list of data from an XML file but when I duplicate the content inside the <data></data> it is not showing anything Here's the ActionScript 3.0 import...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.