Connecting Tech Pros Worldwide Forums | Help | Site Map

To use a pointer or not.

a02hansj@NOstudent.SPAMhis.se
Guest
 
Posts: n/a
#1: Jul 23 '05
I've created a simple three-dimensional vector class for a small vector
math library. The class members are three doubles 'x', 'y' and 'z'.
The idea is to represent polygon vertices as an array of pointers to 3d
vectors. My question is, should I make the x, y and z members of the
vecctor pointers? What are the general guidelines of when to use
pointers or just plain variables?

--
Hans

ben
Guest
 
Posts: n/a
#2: Jul 23 '05

re: To use a pointer or not.


[color=blue]
> My question is, should I make the x, y and z members of the
> vecctor pointers?[/color]

What do you mean "members of the vector pointers"? Does a pointer have a
member??

ben


ben
Guest
 
Posts: n/a
#3: Jul 23 '05

re: To use a pointer or not.


Oops, sorry! I must be sleeping.

Keep x, y and z private members and wrap them up with proper member
functions. This way even if you later decide to change them to pointers, you
won't break user code (all encapsulation is about).

ben
[color=blue]
> What do you mean "members of the vector pointers"? Does a pointer have a
> member??
>
> ben
>
>[/color]


Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 23 '05

re: To use a pointer or not.


a02hansj@NOstudent.SPAMhis.se wrote:[color=blue]
> I've created a simple three-dimensional vector class for a small vector
> math library. The class members are three doubles 'x', 'y' and 'z'.
> The idea is to represent polygon vertices as an array of pointers to 3d
> vectors.[/color]
[color=blue]
> My question is, should I make the x, y and z members of the
> vecctor pointers?[/color]

No.
[color=blue]
> What are the general guidelines of when to use
> pointers or just plain variables?[/color]

Common sense. To keep three doubles directly in your vector you will
need 3*sizeof(double) bytes. To keep them as pointers in your vector
you will need at least 3*(sizeof(double) + sizeof(double*)) plus you will
have to perform indirection, you will have to make sure the pointers are
valid and so on. What do you gain by making them pointers? The size of
the vector object itself is less by (3*(sizeof(double)-sizeof(double*)).
Is that a smart move? I don't think so.

You could do a bit better: keep them as a single pointer to three doubles
(an array) and provide some kind of fast and lean allocation mechanism to
avoid going through the generic 'new[]' and 'delete[]' all the time.
That takes the size of your vector down by as much as
(3*sizeof(double) - sizeof(double*)), which can be viewed as significant.
You still need to perform indirection, and allocation, and deallocation,
and validation, and copying is not as trivial as with straight doubles,
and so on.

V
a02hansj@NOstudent.SPAMhis.se
Guest
 
Posts: n/a
#5: Jul 23 '05

re: To use a pointer or not.


ben <benhongh@hotmail.com> wrote:[color=blue]
> Oops, sorry! I must be sleeping.
>
> Keep x, y and z private members and wrap them up with proper member
> functions. This way even if you later decide to change them to pointers, you
> won't break user code (all encapsulation is about).
>
> ben
>[color=green]
>> What do you mean "members of the vector pointers"? Does a pointer have a
>> member??
>>
>> ben
>>
>>[/color]
>
>[/color]

Yeah I'm sorry. I meant "members of the vector _class_".
And the members are private and I have public getters and setters.

--
Hans
a02hansj@NOstudent.SPAMhis.se
Guest
 
Posts: n/a
#6: Jul 23 '05

re: To use a pointer or not.


Victor Bazarov <v.Abazarov@comacast.net> wrote:[color=blue]
> a02hansj@NOstudent.SPAMhis.se wrote:[color=green]
>> I've created a simple three-dimensional vector class for a small vector
>> math library. The class members are three doubles 'x', 'y' and 'z'.
>> The idea is to represent polygon vertices as an array of pointers to 3d
>> vectors.[/color]
>[color=green]
> > My question is, should I make the x, y and z members of the
>> vecctor pointers?[/color]
>
> No.
>[color=green]
> > What are the general guidelines of when to use
>> pointers or just plain variables?[/color]
>
> Common sense. To keep three doubles directly in your vector you will
> need 3*sizeof(double) bytes. To keep them as pointers in your vector
> you will need at least 3*(sizeof(double) + sizeof(double*)) plus you will
> have to perform indirection, you will have to make sure the pointers are
> valid and so on. What do you gain by making them pointers? The size of
> the vector object itself is less by (3*(sizeof(double)-sizeof(double*)).
> Is that a smart move? I don't think so.[/color]

Well, what I was thinking might be in favor of making them pointers was
that they end up on the heap, rather than the stack. And if I have a
huge honking lot of these little things, I'd rather not fill up the
stack with that.


--
Hans
Karl Heinz Buchegger
Guest
 
Posts: n/a
#7: Jul 23 '05

re: To use a pointer or not.


a02hansj@NOstudent.SPAMhis.se wrote:[color=blue]
>[color=green]
> > Common sense. To keep three doubles directly in your vector you will
> > need 3*sizeof(double) bytes. To keep them as pointers in your vector
> > you will need at least 3*(sizeof(double) + sizeof(double*)) plus you will
> > have to perform indirection, you will have to make sure the pointers are
> > valid and so on. What do you gain by making them pointers? The size of
> > the vector object itself is less by (3*(sizeof(double)-sizeof(double*)).
> > Is that a smart move? I don't think so.[/color]
>
> Well, what I was thinking might be in favor of making them pointers was
> that they end up on the heap, rather than the stack.[/color]

But the pointers will still be 'on the stack'.
[color=blue]
> And if I have a
> huge honking lot of these little things, I'd rather not fill up the
> stack with that.[/color]

Don't worry, You won't have much of them 'on the stack'.
You can always allocate the whole vector object dynamically.

In general - allocate dynamically if:
* you don't know in advance how many
(which is not the case in your case. You know that your vector
contains exactly 3 doubles)
* you need polymorphism and have a need to polymorphic objects
in a container
(which is not the case. The doubles in that class will stay doubles
for a long time)
* your class is extremely large.
(which is not the case. 3 doubles is *not* large. 300000 would be, but
3 is definitly not)

Otherwise prefer the cleanest and simplest design. And that is: If you want
3 doubles, then make them 3 doubles.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Victor Bazarov
Guest
 
Posts: n/a
#8: Jul 23 '05

re: To use a pointer or not.


a02hansj@NOstudent.SPAMhis.se wrote:[color=blue]
> [...]
> Well, what I was thinking might be in favor of making them pointers was
> that they end up on the heap, rather than the stack. And if I have a
> huge honking lot of these little things, I'd rather not fill up the
> stack with that.[/color]

You may be a bit confused. If you have "a huge honking lot of these
little things", they are _all_ probably on the heap, so their contents
are on the heap along with them...

V
a02hansj@NOstudent.SPAMhis.se
Guest
 
Posts: n/a
#9: Jul 23 '05

re: To use a pointer or not.


Victor Bazarov <v.Abazarov@comacast.net> wrote:[color=blue]
> a02hansj@NOstudent.SPAMhis.se wrote:[color=green]
>> [...]
>> Well, what I was thinking might be in favor of making them pointers was
>> that they end up on the heap, rather than the stack. And if I have a
>> huge honking lot of these little things, I'd rather not fill up the
>> stack with that.[/color]
>
> You may be a bit confused. If you have "a huge honking lot of these
> little things", they are _all_ probably on the heap, so their contents
> are on the heap along with them...
>
> V[/color]

See, I didn't know that, that a pointer to a class puts the whole of the
class on the heap.

--
Hans
Victor Bazarov
Guest
 
Posts: n/a
#10: Jul 23 '05

re: To use a pointer or not.


a02hansj@NOstudent.SPAMhis.se wrote:[color=blue]
> Victor Bazarov <v.Abazarov@comacast.net> wrote:
>[color=green]
>>a02hansj@NOstudent.SPAMhis.se wrote:
>>[color=darkred]
>>>[...]
>>>Well, what I was thinking might be in favor of making them pointers was
>>>that they end up on the heap, rather than the stack. And if I have a
>>>huge honking lot of these little things, I'd rather not fill up the
>>>stack with that.[/color]
>>
>>You may be a bit confused. If you have "a huge honking lot of these
>>little things", they are _all_ probably on the heap, so their contents
>>are on the heap along with them...
>>
>>V[/color]
>
>
> See, I didn't know that, that a pointer to a class puts the whole of the
> class on the heap.[/color]

I am again not sure whether you understood my point.

If you do

class vector
{
double x,y,z;
...
};

void somefunction()
{
vector avector;
}

then 'avector' and all its contents (x,y,z) are "on the stack" (as we
often refer to automatic variables).

If you do

const size_t A_LOT_OF_THEM = 1000000;

void somefunction()
{
vector *hugehonkinglot = new vector[A_LOT_OF_THEM];
}

then all vector objects are allocated in the free store ("heap") and
their contents are there too. The pointer to the vectors in that case
is not on the heap, it's an automatic variable, and as such is "on the
stack".

V
msalters
Guest
 
Posts: n/a
#11: Jul 23 '05

re: To use a pointer or not.



Karl Heinz Buchegger wrote:
[color=blue]
> In general - allocate dynamically if:
> * you don't know in advance how many
> * you need polymorphism and have a need to have
> polymorphic objects in a container
> * your class is extremely large.[/color]

* the contained member cannot or should not be declared
in the header file (common case for pimpl idiom)
* the member must outlive its initial owner or be created
earlier than the owner.

Closed Thread