"You can, however, use delete on a pointer with the value 0"

R

r.z.

This is from Visual Studio docs. But is this standard behaviour?
I mean, is it ok in every environment:

class A
{
B* my_pointer;
A();
~A();
}

A::A()
{
my_pointer = 0;
}

A::~A()
{
delete my_pointer;
}
 
B

Bo Persson

r.z. said:
This is from Visual Studio docs. But is this standard behaviour?
I mean, is it ok in every environment:

Yes, it is. Deleting a null pointer has no effect.

Bo Persson
 
K

Kohn Emil Dan

This is from Visual Studio docs. But is this standard behaviour?
I mean, is it ok in every environment:

class A
{
B* my_pointer;
A();
~A();
}

A::A()
{
my_pointer = 0;
}

A::~A()
{
delete my_pointer;
}


Yes, it is ok to call delete or delete[] on null pointers. Doing so is
guaranteed to have no effect.

However your example has a small problem: You have declared
all your constructors and destructors as private, and your class has no
friends. Pretty hard to create and destroy objects, I'd rather say ;-)



Emil
 
S

sideburn

I have a question about the best, cleanest tightest coolest way to verify
that multiple variables all contain the same value.

Lets say I have 5 variables:

Int a,b,c,d,e;

And I assign values to them:

a=4;
b=4;
c=5;
d=4;
e=4;

Then I do a test

If(a == b && a == c && a == d && a == e)
result = -1; //failed, values don¹t match
else
result = 1; //passed, all variables match

This would work but its kind of messy code. In my case I have lots of
variables (they are actually string sizes as in myString.size() )

What I want to do is if all string sizes are not the same then I return an
error.

Isn't there a way to do this by mashing the bits together or something? Or
some way other than the way I describe above?

Thanks for any help.

-Tavis
 
A

Alf P. Steinbach

* sideburn:
I have a question about the best, cleanest tightest coolest way to verify
that multiple variables all contain the same value.

Lets say I have 5 variables:

Int a,b,c,d,e;

And I assign values to them:

a=4;
b=4;
c=5;
d=4;
e=4;

Then I do a test

If(a == b && a == c && a == d && a == e)
result = -1; //failed, values don¹t match
else
result = 1; //passed, all variables match

This would work but its kind of messy code. In my case I have lots of
variables (they are actually string sizes as in myString.size() )

What I want to do is if all string sizes are not the same then I return an
error.

Isn't there a way to do this by mashing the bits together or something? Or
some way other than the way I describe above?

Why don't you use a std::vector rather than individually named variables.
 
S

sideburn

I am...

Heres a sample of what I am doing:


std::vector<std::string> DisplayIDList, DisplayNameList,
DisplaySerialNumList, LastCalibrationList,
ChromaticityXList, ChromaticityYList,
LuminanceList, ;




totalCalibDateDisplays = LastCalibrationList.size();
totalChromXDisplays = ChromaticityXList.size();
totalChromYDisplays = ChromaticityYList.size();
totalLumDisplays = LuminanceList.size();

if( totalCalibDateDisplays == totalChromXDisplays &&
totalCalibDateDisplays == totalChromXDisplays
&& totalCalibDateDisplays == totalChromYDisplays &&
totalCalibDateDisplays == totalLumDisplays)
match = true;
else
match = false;
 
M

Mirek Fidler

If(a == b && a == c && a == d && a == e)
Isn't there a way to do this by mashing the bits together or something?

if((a ^ b) | (a ^ c) | (a ^ d) | (a ^ e) == 0)

Not sure whether this is the right answer for your problem.

Anyway, the advantage of this code is that it avoids 3 branches ->
potentially faster on superscalar CPUs.

Mirek
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top