473,800 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CurrentElement->next = CurrentElement->next->next (UNDEFINED?)

Hi,

I'm working with a single linked list and want to delete elements by
searching through the list (starting form the HEAD) then finding the
element, then doing the following:

NewElement = CurrentElement->next;
CurrentElement->next = NewElement->next->next;
free(NewElement );

Does the double ->next invoke undefined behaviour (sequence points
etc)?

========
The full function follows:
========

typedef struct ElementTag Element;

extern Element *NewElement;
extern Element *CurrentElement ;
extern Element *HeadElement;

/* Finds and deletes person- returns 1, returns 0 for failure) */
int
FindDeletePerso n(const char *name)
{
if(!strcmp(Head Element->Person.name, name))
{
NewElement = HeadElement->next;
free(HeadElemen t);
HeadElement = NewElement;
return 1;
}
else
{
CurrentElement = HeadElement;
while(CurrentEl ement->next)
{
if(!strcmp(Curr entElement->next->Person.name, name))
{
NewElement = CurrentElement->next;
CurrentElement->next = NewElement->next->next;
free(NewElement );
return 1;
}
CurrentElement = CurrentElement->next;
}
}
return 0;
}

Nov 14 '05 #1
2 1756
On 8 Mar 2005 14:26:21 -0800, Deniz Bahar wrote:
Hi,

I'm working with a single linked list and want to delete elements by
searching through the list (starting form the HEAD) then finding the
element, then doing the following:

NewElement = CurrentElement->next;
CurrentElement->next = NewElement->next->next;
free(NewElement );

Does the double ->next invoke undefined behaviour (sequence points
etc)?


<snip>

You can use as many ->next as you wish, as long as the ->next link exists.
Nov 14 '05 #2
Deniz Bahar wrote:
...
I'm working with a single linked list and want to delete elements by
searching through the list (starting form the HEAD) then finding the
element, then doing the following:

NewElement = CurrentElement->next;
CurrentElement->next = NewElement->next->next;
free(NewElement );

Does the double ->next invoke undefined behaviour (sequence points
etc)?
...


No. Firstly, you are not accessing the same object twice.
'CurrentElement->next' as an lvalue is neither 'NewElement' nor
'NewElement->next' nor 'NewElement->next->next'. There's no problem with
sequence points here.

However, the code itself doesn't do what it is supposed to do. Doing

NewElement = CurrentElement->next;
CurrentElement->next = NewElement->next->next;

will exclude _two_ consecutive elements from the list (both 'NewElement'
and 'NewElement->next'), not one. Apparently, this is not what you
wanted to do. You probably intended to do either

NewElement = CurrentElement->next;
CurrentElement->next = NewElement->next;

or

NewElement = CurrentElement->next;
CurrentElement->next = CurrentElement->next->next;

That would exclude only one element from the list.

Now in that last version the value of 'CurrentElement->next' is accessed
twice (once to read it and once to modify it), which might rise the
question about sequence points etc. However, it this case everything is
fine too. The old value of 'CurrentElement->next' is read for the sole
purpose of determining its new value.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #3

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

Similar topics

4
2346
by: Christian Christmann | last post by:
Hi, I've some old code which I would like to replace by STL. It's a self-written double linked list which consists of instances of another class Element which contains the information as void* (I'm going to replace the void* by a template but that's not an issue here). The class Element is defined as: class Element
18
6635
by: Rob Panosh | last post by:
Hello, When traversing an ArrayList which is faster? For Each oItem as Something in me.ArrayList ..... ..... Next OR
9
1954
by: k1ckthem1dget | last post by:
Can someone please help me with this program. How would i go about writing a program which determines the prime numbers from 2 to 1000 in increasing order using the “sieve method of Eratosthenes”. AND Display the values determined to be prime for the user at the display device
0
9691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10276
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10035
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.