Use of Pointers

I am sorry if this question is too long or badly presented .I am working on a small framework(open frameworks) and was going through its tutorials ,when i encountered one of its oops tutorial .

Can i somebody present the answer of this post on stack overflow :

http://stackoverflow.com/questions/22146094/why-should-i-use-a-pointer-rather-than-the-object-itself

with respect to this :

http://openframeworks.cc/tutorials/first%20steps/001_My_first_particle_system.html

some of the points in the stack overflow are technically a little more than i can understand but with the examples i would surely be able to grasp them.

To put it more short ,can anybody list me 'ALL" the advantage i would get if I
use


 
  ofBall **myball;


instead of :

1
2
ofBall myBall[5][5];


This post is a little related to :
http://www.cplusplus.com/forum/beginner/74950/

Again I am sorry if this post was a little confusing .
Thanks!
Last edited on
Mansfield wrote:
The main two situations in which you might require dynamic allocation:
A. You need the object to outlive the current scope
B. You need to allocate a lot of memory

Pointers: always prefer the alternatives unless you really need pointers.
1. You need reference semantics
2. You need polymorphism
3. You want to represent that an object is optional
4. You want to decouple compilation units to improve compilation time
5. You need to interface with a C library

Which of these were over your level?
Can you explain A and point 2 and 3;
A:
From http://stackoverflow.com/questions/9181782/why-are-the-terms-automatic-and-dynamic-preferred-over-the-terms-stack-and
Automatic tells me something about the lifetime of an object: specifically that it is bound automatically to the enclosing scope, and will be destroyed automatically when that scope exits.

Dynamic tells me that the lifetime of an object is not controlled automatically by the compiler, but is under my direct control.

Example:
1
2
3
4
int * foo() {
  int * bar = new int;
  return bar;
}

The bar is a pointer and an automatic variable. It is destroyed/deallocated at the end of the function.
Line 2 allocates memory dynamically for one integer. The address of that memory is stored in bar. The memory remains allocated after the end of foo().

The foo() returns a pointer. Caller of foo can assign the value to a pointer variable. Value of bar can thus be kept evenm though bar is destroyed. Copying the memory address around is critical , because without it the dynamically addressed cannot be referred any more, not deallocated.

See http://msdn.microsoft.com/en-us/library/hh438473.aspx


2:
http://en.wikipedia.org/wiki/Subtyping
http://www.cplusplus.com/doc/tutorial/polymorphism/


3:
http://qt-project.org/doc/qt-5/qstring.html#toUInt
1
2
3
4
QString str = "42";
bool ok;
uint hex = str.toUInt(&ok);
uint foo = str.toUInt();

Call on line 3 gives the toUInt an address of a bool. The function uses the given memory location to store true/false.
Call on line 4 gives 0 as address. There is no place to write to, so the function does not even attempt to write true/false.
In other words storing of the conversion success info is optional and the caller decides whether to do it.

One could write overloads:
1
2
uint QString::toUInt();
uint QString::toUInt(bool&);

However, that is a lot of code duplication for such a tiny optional feature.
Topic archived. No new replies allowed.