Pointers

Sep 26, 2009 at 12:50am
Hello,

I'm new to C++ and so coming from C# where all the memory is handled for me, I am having some trouble in that arena.
1
2
3
4
5
6
7
void CreateCat()
{
	Cat * pCat = new Cat();
	pCat->Meow();
	delete pCat;
	pCat = 0;
}

When I call
 
delete pCat;

the memory address that was created on the heap for this object is cleared. However, if I were to write:
1
2
3
4
5
void CreateCat()
{
	Cat cat;
	cat.Meow();
}

This object has its memory created on the stack right? or am I incorrect? Either way, I wanted to make sure that the memory allocated for this object is cleared automatically when I exit the scope of the function CreateCat().

Help?
Sep 26, 2009 at 1:06am
Sep 26, 2009 at 1:15am
Your second example is the best way to deal with it -- it is a temporary which is created on the stack.

There are cases where you need to create temporaries on the heap. For example, large objects are best constructed on the heap. In that case, it is best to use a std::auto_ptr.

Instead of your first example, you would do something like this:

1
2
3
4
5
6
7
#include <memory>

void CreateCat()
{
    std::auto_ptr<Cat> pCat(new Cat);
    pCat->Meow();
}


The auto_ptr automatically deletes the Cat object when it goes out of scope.

auto_ptr is just one class of types called "smart pointers". There may be others that are more suitable depending on how the pointer is used.

http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/smart_ptr.htm

Sep 26, 2009 at 1:18am
OK. Thank you! Another question though. Why use references? Aren't pointers, while slightly more annoying, more powerful? Having said that, other than passing in pointers/references through functions, why use them?
Sep 26, 2009 at 1:34am
Pointers are indeed more powerful, and there are many situations where pointers do things references can't do. References work as syntactic sugar for all the other situations.
For example, a common case is passing an std::string by reference/pointer without accepting null pointers. You can either pass a const std::string * and check whether the pointer is valid, or just pass a const std::string & and make the check unnecessary (and impossible).
Basically, the question of why to use references is the same as why to use the [] operator when you can just do *(pointer+offset), or why use -> when you can just do (*pointer).member.
Sep 26, 2009 at 1:38am
More powerful == a bigger gun to shoot yourself in the foot with. Why use a bazooka when a peashooter will do the trick?

References and pointers allow you to pass objects around without copying them.

http://www.parashift.com/c++-faq-lite/references.html#faq-8.6
Sep 26, 2009 at 1:45am
My current understanding of the usage of pointers and references is that it is both permissible and recommended to pass pointers/references when the object in question is large and/or will not be modified (unless the modification of said object is intended).
Sep 26, 2009 at 5:09am
is this an accurate assessment?
Sep 26, 2009 at 5:41am
You pass by reference* when the object you want to pass won't be modified in the caller's stack frame but is too large to copy (e.g. a string or a vector) or when it needs to be modified in the caller's stack frame regardless of its size.



*Passing by reference is a CS term that means passing data to a function in such a way that the modifications applied in the callee are applied also in the caller. In C++ terminology, it means passing either a pointer or a reference.
Topic archived. No new replies allowed.