By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

Advanced pointer?

P: n/a
I have a question about advanced poiters. I know the basics of memory
managment and pointers. That a pointer points to a memory location. I
also have read that dynamic memory new is used for creating an array
that you don't know the size of the array at compile time. Still I see
pointers used in other ways. I can create object o; or object * o =
new object; What is the different from creating an object one way or
the other. I have used pointers:

obj{
member * pmemberr;

};

obj::obj(){
pmember = new member;
}

I have some books that deal with pointers but they give me are
simplistic and don't cover examples I see in other programs. If a good
explanition is dificult to give where can I look this up to get a
better idea how to use pointers.

Sep 25 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a

JoeC wrote:
I have a question about advanced poiters. I know the basics of memory
managment and pointers. That a pointer points to a memory location. I
also have read that dynamic memory new is used for creating an array
that you don't know the size of the array at compile time. Still I see
pointers used in other ways. I can create object o; or object * o =
new object; What is the different from creating an object one way or
the other. I have used pointers:

obj{
member * pmemberr;

};

obj::obj(){
pmember = new member;
}

I have some books that deal with pointers but they give me are
simplistic and don't cover examples I see in other programs. If a good
explanition is dificult to give where can I look this up to get a
better idea how to use pointers.
BTW I also know that pointers are needed for polymorphism.

Sep 25 '06 #2

P: n/a
JoeC <en*****@yahoo.comwrote:
>BTW I also know that pointers are needed for polymorphism.
An interesting claim. I'm guessing it's false, and that
a language could (as a language feature) provide many types of
polymorphism without the language providing pointers (as
a language feature).

Probably some people here could cite examples of such languages.

Steve
Sep 26 '06 #3

P: n/a

JoeC wrote:
I have a question about advanced poiters. I know the basics of memory
managment and pointers. That a pointer points to a memory location. I
also have read that dynamic memory new is used for creating an array
that you don't know the size of the array at compile time.
std::vector is a better way of creating an array if you don't know the
size until run time.
Still I see
pointers used in other ways. I can create object o; or object * o =
new object; What is the different from creating an object one way or
the other.
object o;
o has either static storage duration (the object lasts for the entire
life of the program) if this statement appears at file scope, outside
any function, or o has automatic storage duration (the object lasts
until the end of the block of code it is declared in) if this statement
appears inside a function or inside a block of code within a function.
In either case, the compiler is responsible for managing the lifetime
of the object.

object* o = new object;
The object pointed to by o has dynamic storage duration. It exists
until you, the programmer, gets rid of it with delete o; which you must
do to avoid a memeory leak. You, and you alone, are responsible for
managing the lifetime of the object and avoiding a memory. The compiler
will not help you.

Do not give yourself this extra responsibility unless you absolutely
need to. And then, do not use new on its own. Always, always, always
use smart objects to encapsulate your memory management. So for dynamic
arrays use std::vector and for dynamic single objects use a smart
pointer like std::auto_ptr or std::tr1::shared_ptr (or whatever the
boost::shared_ptr is called now it or something like it has been
incorporated into C++).

Gavin Deane

Sep 26 '06 #4

P: n/a
"JoeC" <en*****@yahoo.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
>I have a question about advanced poiters. I know the basics of memory
managment and pointers. That a pointer points to a memory location. I
also have read that dynamic memory new is used for creating an array
that you don't know the size of the array at compile time. Still I see
pointers used in other ways. I can create object o; or object * o =
new object; What is the different from creating an object one way or
the other. I have used pointers:

obj{
member * pmemberr;

};

obj::obj(){
pmember = new member;
}

I have some books that deal with pointers but they give me are
simplistic and don't cover examples I see in other programs. If a good
explanition is dificult to give where can I look this up to get a
better idea how to use pointers.
Your question seems to be what's the difference between using new to
allocate one instance of an object and allocating an array of objects.

If I have a pointer such as:
MyClass* MyPointer;
All I know is that it can be used to point to at least one instance of
MyClass. So I can say:

MyPointer = new MyClass;
or
MyPointer = new MyClass[10];

The first example allocates memory and calls constructor for one MyClass and
has MyPointer point to it. The second allocates memory for 10 MyClasses and
calls constructor and has MyPointer point to the first one.

There is little (but some) difference between declaring MyPointer as:
MyClass* MyPointer;
MyClass MyPointer[];

It is up to me to determine how many instances of MyClass MyPointer points
to.

Also, if only one is instatized with new, then you use delete. If more than
one you use delete[]. If you use delete and more than one is declared, this
can cause undefined behavior. The most likely thing that will happen is the
destructors will not be called but on one instance.
Sep 26 '06 #5

P: n/a

Steve Pope wrote:
JoeC <en*****@yahoo.comwrote:
BTW I also know that pointers are needed for polymorphism.

An interesting claim. I'm guessing it's false, and that
a language could (as a language feature) provide many types of
polymorphism without the language providing pointers (as
a language feature).

Probably some people here could cite examples of such languages.

Steve
shape * sh1 = new triangle();
shape * sh2 = new square();

or from a program that I wrote:

hunit::hunit(char n){

switch(n){
case 'u':
p = new unit;
break;
case 'a':
p = new air;
break;
case 's':
p = new sea;
break;
}

cnt = new int(1);

}

Sep 26 '06 #6

P: n/a

Gavin Deane wrote:
JoeC wrote:
I have a question about advanced poiters. I know the basics of memory
managment and pointers. That a pointer points to a memory location. I
also have read that dynamic memory new is used for creating an array
that you don't know the size of the array at compile time.

std::vector is a better way of creating an array if you don't know the
size until run time.
Still I see
pointers used in other ways. I can create object o; or object * o =
new object; What is the different from creating an object one way or
the other.

object o;
o has either static storage duration (the object lasts for the entire
life of the program) if this statement appears at file scope, outside
any function, or o has automatic storage duration (the object lasts
until the end of the block of code it is declared in) if this statement
appears inside a function or inside a block of code within a function.
In either case, the compiler is responsible for managing the lifetime
of the object.

object* o = new object;
The object pointed to by o has dynamic storage duration. It exists
until you, the programmer, gets rid of it with delete o; which you must
do to avoid a memeory leak. You, and you alone, are responsible for
managing the lifetime of the object and avoiding a memory. The compiler
will not help you.

Do not give yourself this extra responsibility unless you absolutely
need to. And then, do not use new on its own. Always, always, always
use smart objects to encapsulate your memory management. So for dynamic
arrays use std::vector and for dynamic single objects use a smart
pointer like std::auto_ptr or std::tr1::shared_ptr (or whatever the
boost::shared_ptr is called now it or something like it has been
incorporated into C++).

Gavin Deane
I like your advice and that is usually the way I design my programs.
Recently, I have been writing some simple games but complex programs
and have been advised to use pointer for some aspects of the game.
There are some times, I realy had to use pointers but other times I
have gotten by with out them. So pointers are good for global
variables or larger objects that may have to get passed to different
functions. Here is an example of my hearder for my map game:

#include<windows.h>
#include<ctime>
#include<vector>

#include "unit.h"
#include "libs.h"
#include "board.h"
#include "terrain.h"
#include "graphic.h"
#include "tbox.h"
#include "cfight.h"
#include "mapmgt.h"

#ifndef GAME_H
#define GAME_H

LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM); //winproc
function
void kill(std::vector<unit>&); //unit killer

#endif
static const int TeamSize = 5; //size of each team

static board * b; //the board

static terrain trn[5]; //terrain array
static char * tn[5]; //terrain name array

static mapmgt m(trn, b->GetSizeX(), b->GetSizeY()); //map manager

static vector<unitrteam(TeamSize); //read side
static vector<unityteam(TeamSize); //purple side originally was going
to
//yellow but didn't look good on
the screen

static bool red; //red's turn
static bool ppl; //purple's turn

static int t1m; //red's current mover
static int t2m; //purple's current mover

Sep 26 '06 #7

P: n/a
JoeC posted:
I have a question about advanced poiters.

What are "advanced pointers"? Are you talking about plain old run-of-the-
mill pointers.
I know the basics of memory managment and pointers. That a pointer
points to a memory location. I also have read that dynamic memory new
is used for creating an array that you don't know the size of the array
at compile time. Still I see pointers used in other ways. I can create
object o; or object * o = new object; What is the different from
creating an object one way or the other. I have used pointers:

The former creates an "automatic object" which comes into being at its
definition, and is destroyed when it goes out of scope.

The latter creates a "dynamicallya allocated object" which come into being
at its creation via new, and is destroyed when it is "delete"'d.

obj{
member * pmemberr;

};

Perhaps you meant "struct obj" rather than simply "obj"?

If a good explanition is dificult to give where can I look this up to
get a better idea how to use pointers.

Here's a recent post of mine which trys to teach about them:

http://groups.google.ie/group/comp.l...ddfb2a7?hl=en&

--

Frederick Gotham
Sep 26 '06 #8

P: n/a
JoeC posted:
BTW I also know that pointers are needed for polymorphism.

It can be achieved with a reference:

class Base : { virtual int Func()...

class Derived1 : public Base { virtual int Func()...

void MyFunc(Base &obj)
{
obj.Func();
}

--

Frederick Gotham
Sep 26 '06 #9

P: n/a

Frederick Gotham wrote:
JoeC posted:
BTW I also know that pointers are needed for polymorphism.


It can be achieved with a reference:

class Base : { virtual int Func()...

class Derived1 : public Base { virtual int Func()...

void MyFunc(Base &obj)
{
obj.Func();
}

--

Frederick Gotham
didn't know that all my books say that you have to obj * prt = new
thing;

Sep 27 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.