pereges said:
Yes, I'm talking about a structure. For example I want to store a mesh
containing close to 200,000 vertices and 400,000 triangles. Here's my
data structure:
typedef struct vector_struct
{
double x, y, z;
}vector;
typedef vector vertex;
typedef struct triangle_struct
{
int v[3];
vector normal;
}triangle;
/* ======== Data structure for the mesh ========== */
typedef struct object_struct
{
vector *vertexlist;
triangle *trianglelist;
unsigned long int nvert;
unsigned long int ntri;
}object;
In this mesh structure, vertexlist and triangle list are pointers to
the array of vertices and triangles respectively.
If you pass a pointer to the structure to a function you safe
a bit of work in copying it and a bit of memory since no copy
needs to be made (but not that much for such a rather small
structure). The main difference is that you can easily change
all the elements in the original structure without passing it
also back to the caller since you are working on the original
itself and not a copy (although you can prevent that by quali-
fying the pointer as 'const' inthe functions argument list).
I don't see why dealing with a pointer instead of a copy of
the structure would be more difficult, you just have to use
a '->' between the pointer and the elements name you want
to access instead of a '.' between the structures name and
the element.
In the overwhelming majority of programs I have seen pointers
to structures get passed to fucntions instead of the structures
by value, so it's a very common idiom and most people will un-
derstand it quite fine. And, yes it might be a bit more effi-
cient speed- and memory-wise, but I wouldn't expect a doubling
of the speed of your application (or anything near that) just
from using pointers;-)
Regards, Jens