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

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

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;
}
Dec 16 '06 #1
7 1747
r.z. wrote:
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
>
class A
{
B* my_pointer;
A();
~A();
}

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

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

Dec 16 '06 #2


On Sat, 16 Dec 2006, r.z. wrote:
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
Dec 16 '06 #3

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

class A
{
B* my_pointer;
public:
A();
~A();
}
missing semi-colon.
>
A::A()
{
my_pointer = 0;
}
A::A() : my_pointer(0) { }
>
A::~A()
{
delete my_pointer;
}
thats no problem.

Dec 16 '06 #4
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
Dec 19 '06 #5
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 19 '06 #6
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;
On 12/18/06 5:57 PM, in article 4u*************@mid.individual.net, "Alf P.
Steinbach" <al***@start.nowrote:
* 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.
Dec 19 '06 #7
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

Dec 19 '06 #8

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

Similar topics

3
by: maadhuu | last post by:
well,i am curious to know what would be the output of the following: delete this; basically , comment on this .
32
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
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
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.