Friday, December 31, 2010

Delete Pointer Passed to Function C++ Parameter

I hate C++ pointers. I switch from C to C++ to Java a lot, so I tend to need refreshing once in a while when I go from Java to C++.
I was updating some old code the other day and came across a function which was supposed to delete a pointer passed to it and then set it to null. When the function returned, the pointer was deleted, but was not null. Reason why: the function had been set up wrong:

void deletePointer(XYZ* _xyz)
{
delete _xyz;
_xyz = 0;
}


void callDeletePointer()
{
XYZ* xyz = new XYZ();
...
...
...
deletePointer(xyz);
}

The way deletePointer really needed to be set up is like this:

void deletePointer(XYZ*& _xyz)
{
delete _xyz;
_xyz = 0;
}

Here's why:
The first deletePointer receives a copy of the pointer to _xyz, which allows the user to modify the object addressed by the pointer.
The second deletePointer receives the actual pointer to _xyz, which allows the user to modify the pointer itself.

Oh and one more tip:
When deallocating memory do this:

XYZ* xyz = new XYZ();
delete xyz;
xyz = 0;

instead of:

XYZ* xyz = new XYZ();
if(xyz != 0)
{
delete xyz;
xyz = 0;
}


Apparently, C++ guarantees operator delete() won't be called by a delete if the pointer is 0, so testing for null is not necessary. In fact, if you do include a pointer null test, the test will be done twice.

Source: C++ Primer, Third Edition, by Stanley B. Lippman, Josee Lajoie


More C++


Friday, December 3, 2010

Verizon Wireless Rolls Out 4G

So on Sunday Verizon Wireless is rolling out its 4G network to 38 select cities. You can go to: www.verizonwireless.com/4Glte, to see if your area will be covered. There seems to be a problem with the link at the moment though.
Initially 4G will only be available for laptops. Verizon's line of 4G smartphones will be revealed next month.
I'm totally looking forward to this. 4G is supposed to be 10x faster than 3G.
3G on my Samsung Alias 2 is pretty good. It's faster than my dial up. I live in a rural area, so dial up and satellite are my only options. At least I thought they were my only options until I saw an ad for AT&T's usb modem. I can use that modem to hop on AT&T's cell phone network with my laptop. Verizon Wireless has a similar modem. I would probably stick with Verizon. The plan I would choose runs about $60 per month. My dial up plan at toast.net is about $10 per month. I haven't been ready to spend that extra $50 a month, but with the 4G network I think I may be ready. The 4G plan is also cheaper than the 3G. The 4G plan is $50 a month for 5 gig of bandwith or $80 for 10 gig. Toast.net is not bad, but downloading and uploading anything larger than a few meg is painful. With 4G, you can download a 2 hour movie in 5 minutes. Watching videos and listening to music online with dial up is extremely painful. My favorite channel, SoapNet, will be no longer in 2012, so I may end up having to watch my soaps online.
When my cell phone contract is up a year from now, I think I'm also going to go for a smartphone, maybe a Droid. We'll see what's available at that point.