473,387 Members | 1,540 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,387 software developers and data experts.

Re: set erase of null and nonexistent elements

Anyway, the code below does not make any use of operator(). It uses
std::less<A*>. If that is not specialized in A.h, the remarks below apply.
If it is specialzed, you have to make sure it is a strict total ordering.
I meant something else entirely. I was refering to algorithms, such as
find_if where passed predicate, which implemented operator () for
comparison. I assume set uses operator == to locate elements of UDT.
Correct me if am wrong.
What does standard or popular implementation dictate of the following
example:
#include<set>
#include "A.h"
using std::set
int main(int argc, char **argv)
{
std::set<A *aSet;
A *nullA=0;
A *a=new A("NOT HERE")
set.insert(new A("something"))
//here is the list of operations I am
aSet.erase(nullA);

null-op: nullA is not in the set.
aSet.erase(a);

null-op: a is not in the set.
//clean up and return
return 0;
}
How do standard containers (instantiated with some pointer to user-
defined type) behave in the presence of null pointer?
Well, if we insert a null pointer into a set, wouldn't it cause an
error whenever the container would try to invoke its member.

Wait, perhaps an std container doesn't do that, hence no errors are to
be seen, then how does find work?
Sep 14 '08 #1
2 1694
puzzlecracker wrote:
>
>Anyway, the code below does not make any use of operator(). It uses
std::less<A*>. If that is not specialized in A.h, the remarks below
apply. If it is specialzed, you have to make sure it is a strict total
ordering.

I meant something else entirely. I was refering to algorithms, such as
find_if where passed predicate, which implemented operator () for
comparison. I assume set uses operator == to locate elements of UDT.
Correct me if am wrong.
You are wrong: std::set<uses std::less<to decide whether two elements
are equal. The idea is that a == b can be characterized as neither a < b
nor b < a. In fact, operator== is not used by std::set<at all.

What does standard or popular implementation dictate of the following
example:
#include<set>
#include "A.h"
using std::set
int main(int argc, char **argv)
{
std::set<A *aSet;
A *nullA=0;
A *a=new A("NOT HERE")
set.insert(new A("something"))
//here is the list of operations I am
aSet.erase(nullA);

null-op: nullA is not in the set.
aSet.erase(a);

null-op: a is not in the set.
//clean up and return
return 0;
}
How do standard containers (instantiated with some pointer to user-
defined type) behave in the presence of null pointer?

Well, if we insert a null pointer into a set, wouldn't it cause an
error whenever the container would try to invoke its member.
Yes. Dereferencing a null-pointer is undefined behavior. That, however, does
not imply that you cannot insert the null-pointer into a set. You just have
to make sure that it does not get dereferenced.

Wait, perhaps an std container doesn't do that, hence no errors are to
be seen, then how does find work?
Post code, so that we can discuss find() or find_if() meaningfully. I don't
trust my crystal ball and can only guess what you are looking at.

Best

Kai-Uwe Bux
Sep 14 '08 #2
On Sep 15, 1:21 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
puzzlecracker wrote:
[...]
Well, if we insert a null pointer into a set, wouldn't it
cause an error whenever the container would try to invoke
its member.
Yes. Dereferencing a null-pointer is undefined behavior. That,
however, does not imply that you cannot insert the
null-pointer into a set. You just have to make sure that it
does not get dereferenced.
Also, std::set<T*never dereferences the pointers it contains.
Let's face it, this is pure template code, with no partial
specializations or whatever, so the code is exactly the same for
pointers and for non-pointers.

Of course, if you explicitly specialize std::less< A* so that
it does dereference the pointer, you'll get into deep trouble if
the pointer is null. But that would be just stupid (unless your
goal is obfuscation). The standard defines a full ordering on
pointers (using std::less, but not with <), and requires
std::set to use this ordering by default. A reader who sees a
set instantiated without a custom ordering function has a right
to expect that this ordering is what is used, and not something
else. (If this ordering isn't appropriate, and it often isn't,
then you should says so explicitly, by providing an ordering
argument to the template instantiation.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 15 '08 #3

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

Similar topics

2
by: s | last post by:
Here is a snippet of my code: <code> list< MyClass * >outgoing_pool; MyClass* GetWaitingObject(string freq) { MyClass *temp_ptr = NULL; list<MyClass*>::iterator i;
3
by: jose luis fernandez diaz | last post by:
Hi, Erase elements while iterating on a map don't invalidate the iterator except the erased one, so the program below: (1) #include <map> int main()
1
by: GGerard | last post by:
Hello I am working with Access 2000 I have two tables joined on a one to many relationship between two fields: Table1:FieldID (one) is joined to Table2:FieldMyID (many) Field Properties...
10
by: Piotr | last post by:
In Effective STL item 9 "Choose carefully among erasing options", it has this example: bool badValue(int x); // returns whether x is 'bad' c.erase ( remove_if(c.begin(), c.end(), badValue),...
9
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10);...
16
by: Frank Neuhaus | last post by:
Hi I have some std list, I'd like to traverse. During the traversal, I want to conditionally delete some objects. My code for that is like this right now: for (std::list<myStruct>::iterator...
3
by: groups | last post by:
This small piece of code is troubling me.. is there anything wrong with it?? After calling this method the contents ot the input vector are completely screwed.. (CCountedCIG_CountZero() applies...
3
by: ma740988 | last post by:
For discussion purposes, assume the vector ivec contains 67108864 (67 million elements) elements. Lets futher assume nstart and end equal 1008000 and 11088000 respectively. The question. ...
3
by: subramanian100in | last post by:
Consider vector<stringv; If we call, v.erase(v.end()) this invokes undefined behaviour. But, if we call
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.