You are not logged in.

#1 2010-04-30 03:00:14

dozerismydogsname
Member
Registered: 2010-02-11
Posts: 65

When to use pointers in c++

I've recently started to get the hang off using c++. The only thing i'm having difficulty understanding is the use of pointers. I understand that pointers basically "point" to memory addresses of that variable. When should you start using pointers in your variables?

For example:

Without using a pointer i could just do:

myobject hello(1);

However i could also use a pointer to do just the same:

myobject* hello = new myobject(1);

With those 2 examples, you are basically creating a myobject named hello. Is there any difference between the pointer hello and the normal hello?

Last edited by dozerismydogsname (2010-04-30 03:05:10)

Offline

#2 2010-04-30 03:25:43

Basu
Member
From: Cornell University
Registered: 2006-12-15
Posts: 296
Website

Re: When to use pointers in c++

C++ is a very large and complex language and is backwards compatible with C ie. you can take a C program and compile it as is with a C++ compiler. You are right that pointers allow you to point to raw memory locations (and hence do lots of very dangerous things). When you create a new object using new, you get back a pointer to that object.

Now, it is technically possible to write pointer free C++. However, using pointers can allow you to write programs that are more memory efficient. For example, if you want to send an object to a function, you could send the actual object itself. The object would then get *copied* on to the stack and the original object would be unaffected. But if you send the pointer to the function, it would take up less space and the function would change the original object (which may or may not be what you want).

Pointers are very powerful but very dangerous too. If you really want to understand how pointers work, study about computer hardware and learn some straight C.

As an aside, I would probably not recommend C++ if it's your first programming language.


The Bytebaker -- Computer science is not a science and it's not about computers
Check out my open source software at Github

Offline

#3 2010-04-30 03:43:57

tomd123
Developer
Registered: 2008-08-12
Posts: 565

Re: When to use pointers in c++

Basu wrote:

C++ is a very large and complex language and is backwards compatible with C ie. you can take a C program and compile it as is with a C++ compiler.

http://en.wikipedia.org/wiki/Compatibil … nd_C%2B%2B

Offline

#4 2010-04-30 06:08:33

the_isz
Member
Registered: 2009-04-14
Posts: 280

Re: When to use pointers in c++

dozerismydogsname wrote:

Is there any difference between the pointer hello and the normal hello?

The short answer: the object the pointer hello points to is allocated on the
heap, the normal hello is allocated on the stack. That means, after your current
block of code (enclosed in {}) ends, the normal hello will be destroyed while
the other one needs to be deleted by you using delete. If you don't delete it,
you'll get what is called a memory leak.

Whenever possible, you should use stack variables. When you need to use
pointers, you should really think about using std::auto_ptr or boost::shared_ptr
to prevent memory leaks as much as possible.

But I think your real question is this:

dozerismydogsname wrote:

When should you start using pointers in your variables?

That question is far more difficult to answer. Usually, you tend to use pointers
if you start passing around arrays:

double sum (int n, const double* const numbers)
{
  double result = 0.;

  for (int i=0; i<n; ++i)
    result += numbers[i];

 return result;
}

However, this is C-style. In C++ you would try avoiding using pointers directly
by using encapsulating objects like such:

#include <vector>

double sum (const std::vector<double> numbers)
{
  double result = 0.;

  for (int i=0; i<numbers.size(); ++i)
    result += numbers[i];

 return result;
}

Which again saves you from thinking about who has to delete the pointer after
it's been used.

Often you want to use pointers to avoid copying large structures, but here you
typically can use references as well.

The only reasonable place in C++ I can currently think of to use pointers are
function pointers and places where you want to share structures (class or struct
instances) among several objects and want to be able to dynamically exchange
them. Again, you should use boost::shared_ptr here to avoid memory leaks. An
example:

class ReduceStrategy
{
  public:
    virtual double reduce (const double& a, const double& b) = 0;

  protected:
    ReduceStrategy ();
};

class SumStrategy : public ReduceStrategy
{
  public:
    SumStrategy ()
    : ReduceStrategy() {}

    virtual double reduce (const double& a, const double& b)
    {
      return a + b;
    }
};

class SubtractStrategy [...]

class Reducer
{
  public:
    Reducer (boost::shared_ptr<ReduceStrategy> strategy)
    : _strategy(strategy) {}

  [...]
};

[...]

boost::shared_ptr<ReduceStrategy> strategy(new SumStrategy);

std::vector<Reducer> reducers;

for (int i=0; i<1000; ++i)
  {
  reducers.append(Reducer(strategy));
  }

In this example, all 1000 Reducer instances share a pointer to the very same
ReduceStrategy object. If that object were very large now, you'd save a hell lot
of memory. Also, it would be possible to exchange the strategy for one, several
or all of the Reducer instances.

Wow, this text has become much longer than I originally intended^^ Hope it helps
at least a little.

Have fun learning C++!

Offline

#5 2010-04-30 11:18:42

dozerismydogsname
Member
Registered: 2010-02-11
Posts: 65

Re: When to use pointers in c++

Awesome, thanks for the help.

Offline

Board footer

Powered by FluxBB