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 10 1302 My question is, should I make the x, y and z members of the vecctor pointers?
What do you mean "members of the vector pointers"? Does a pointer have a
member??
ben
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 What do you mean "members of the vector pointers"? Does a pointer have a member??
ben
a0******@NOstudent.SPAMhis.se wrote: 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?
No.
What are the general guidelines of when to use pointers or just plain variables?
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
ben <be******@hotmail.com> wrote: 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
What do you mean "members of the vector pointers"? Does a pointer have a member??
ben
Yeah I'm sorry. I meant "members of the vector _class_".
And the members are private and I have public getters and setters.
--
Hans
Victor Bazarov <v.********@comacast.net> wrote: a0******@NOstudent.SPAMhis.se wrote: 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?
No.
What are the general guidelines of when to use pointers or just plain variables?
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.
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 a0******@NOstudent.SPAMhis.se wrote: 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. 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.
But the pointers will still be 'on the stack'.
And if I have a huge honking lot of these little things, I'd rather not fill up the stack with that.
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 kb******@gascad.at a0******@NOstudent.SPAMhis.se wrote: [...] 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.
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
Victor Bazarov <v.********@comacast.net> wrote: a0******@NOstudent.SPAMhis.se wrote: [...] 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.
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
See, I didn't know that, that a pointer to a class puts the whole of the
class on the heap.
--
Hans a0******@NOstudent.SPAMhis.se wrote: Victor Bazarov <v.********@comacast.net> wrote:
a0******@NOstudent.SPAMhis.se wrote:
[...] 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.
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
See, I didn't know that, that a pointer to a class puts the whole of the class on the heap.
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
Karl Heinz Buchegger wrote: 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.
* 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Carsten Spieß |
last post by:
Hello all,
i have a problem with a template constructor
I reduced my code to the following (compiled with gcc 2.7.2) to show
my problem:
// a base class
class Base{};
// two derived...
|
by: Mr A |
last post by:
Hi!
I've been thinking about passing parameteras using references instead
of pointers in order to emphasize that the parameter must be an
object.
Exemple:
void func(Objec& object); //object...
|
by: Bruno van Dooren |
last post by:
Hi All,
i have some (3) different weird pointer problems that have me stumped. i
suspect that the compiler behavior is correct because gcc shows the same
results.
...
|
by: tuko |
last post by:
Hello kind people.
Can someone explain please the following code?
/* Create Storage Space For The Texture */
AUX_RGBImageRec *TextureImage; /* Line 1*/
/* Set The Pointer To NULL...
|
by: junky_fellow |
last post by:
According to Section A6.6 Pointers and Integers (k & R)
" A pointer to one type may be converted to a pointer to another type.
The resulting pointer may cause
addressing exceptions if the...
|
by: Alexei A. Frounze |
last post by:
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2 = {0,1,2,4,9,16};
int aInt3 =...
|
by: aegis |
last post by:
Given the following:
int a = 10;
int *p;
void *p1;
unsigned char *p2;
p = &a;
|
by: bluejack |
last post by:
Ahoy... before I go off scouring particular platforms for specialized
answers, I thought I would see if there is a portable C answer to this
question:
I want a function pointer that, when...
|
by: fieldfallow |
last post by:
Hello all,
Before stating my question, I should mention that I'm fairly new to C.
Now, I attempted a small demo that prints out the values of C's numeric
types, both uninitialised and after...
|
by: Martin Jørgensen |
last post by:
Hi,
"C primer plus" p.382:
Suppose we have this declaration:
int (*pa);
int ar1;
int ar2;
int **p2;
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |