It´s an old Beta version of Visual Studio, which doesn´t understand NULL and so set it to 0. Anyway it works with that online compiler. Results as expected. My faith in C++ has been restored.
gr8 stuff, thx.
- Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
- Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Reply to: C++ technical question
Collapse
You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:
- You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
- You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
- If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.
Logging in...
Previously on "C++ technical question"
Collapse
-
Just tried it in VC8 for you, and the destructor is only called once. Perhaps the problem is with the other code you're not showing us?
You need a return type for the function (i.e. void) in VC8 otherwise it'll moan.
Leave a comment:
-
Yes thx for that.
....of course a pointer to a pointer as it were.
That online compiler is very useful.
I´m using Microsoft Visual Studio C++ compiler which gives me that unexpected result of the destructor being called twice even though I set the ptr to NULL (when using *&).
Leave a comment:
-
Which 'crappy' MS compiler are we talking here?
Have you sanity-tested your code on any of these: https://www.google.co.uk/search?q=on...2B%2B+compiler
Leave a comment:
-
It'll call Kill() twice, but the second time ptr is NULL and so delete ptr will do nothing. Is that what you're saying is happening?
If Kill just takes a T* then ptr=NULL is only changing the local pointer, not the original, so the second call will try to delete an object that has already been deleted, and bang.
Leave a comment:
-
C++ technical question
Given this code:
template <class T> Kill (T *& ptr)
{
delete ptr;
ptr = NULL;
};
class MyClass{};
int main()
{
MyClass* myClass = new MyClass;
Kill(myClass);
Kill(myClass);
}
What should happen here?
With my (crappy) Microsoft compiler I executed this and it destructs twice when I have *& but generates an exception when I use * instead of *& in the template definition.
Surely when the pointer is set to NULL calling delete the second time should have no effect. Why was it executed twice with *&
Is it so that the above code should just execute without error and delete the object only once?Last edited by BlasterBates; 26 March 2013, 16:13.Tags: None
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- New UK crypto rules now apply. Here’s how mandatory reporting affects contractors Today 07:03
- What the Ray McCann Loan Charge Review means for contractors Yesterday 06:21
- IT contractor demand defied seasonal slump in December 2025 Jan 13 07:10
- Five tax return hacks for contractors as Jan 31st looms Jan 12 07:45
- How to land a temporary technology job in 2026 Jan 9 07:01
- Spring Forecast 2026 ‘won’t put up taxes on contractors’ Jan 8 07:26
- Six things coming to contractors in 2026: a year of change, caution and (maybe) opportunity Jan 7 06:24
- Umbrella companies, beware JSL tunnel vision now that the Employment Rights Act is law Jan 6 06:11
- 26 predictions for UK IT contracting in 2026 Jan 5 07:17
- How salary sacrifice pension changes will hit contractors Dec 24 07:48

Leave a comment: