gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Array of pointers - how?

#69178 - Janekxx - Sun Jan 29, 2006 12:38 am

Hi, I'm coding my 3D game for ARM platform (not GBA, but old Sony Jxx phones) and I ecountered first problem I can't solve :)

I have few 3D models to use in scene.
To transform them (rotate, etc.) I pass pointer to array in every function.
Here is the problem:

Can I create array that contain pointers and how?

It's needed because I don't want to write:
do_sth(int *adr_of_model);

... for every model, but I want to use loop like this:
for (a=5; a>0; a--){
tmp = array_of_pointers[a];
do_sth(int *tmp);
}


Is that possible? What are the mistakes in my example?

I'm new in C, sorry for lame question ;)
_________________
sony.int.pl

#69189 - keldon - Sun Jan 29, 2006 2:14 am

You would normally do the following:

(1) Declare a large set of memory for data as allocation takes time
(2) Manage your own data in a layer on top of the available allocation routines

One thing to remember is that when you tranform you usually do not want to keep these transformations for the next frame so they are usually temporary. However there are some global transfomorations that must be applied to the global T matrix for the object.

I am not sure how you have worked with 3d graphics before but remember the memory management issues with C/C++/asm.

One way to create array of 'pointers':
Code:

   void **ptrArray = malloc (sizeof(void *) * ARRAYLEN);


Just remember that pointers only point to addresses. They are 4 bytes long since the only data they store is the address of where the data is stored. You are likely to allocate an array of structs with empty data and later assign and set data to the elements in that array.

I would suggest having a think about memory management and how that effects your current routine and way of working. Figure out where memory would leak without garbage collection, and know what garbage collection is and how it generally works.

Actually what is your current knowledge of 3d graphics - and how did you learn and use it. That may direct the information to you better.

EDIT: changed * to **


Last edited by keldon on Sun Jan 29, 2006 9:22 am; edited 1 time in total

#69194 - poslundc - Sun Jan 29, 2006 4:08 am

Array of integers:

Code:
int     a = 5;
int     b = 6;
int     c;

int     array[2];

array[0] = a;
array[1] = b;

c = array[0] + array[1];


Array of pointers to integers:


Code:
int     a = 5;
int     b = 6;
int     c;

int     *array[2];

array[0] = &a;
array[1] = &b;

c = *array[0] + *array[1];


Note that when you are using pointers there are many more opportunities for silent errors to manifest themselves. Such as, if in the above example a and b had been declared inside a function, and array was used after the completion of that function, then the pointers stored in array would no longer be valid, although you could still dereference them with undefined results.

Dan.

#69215 - keldon - Sun Jan 29, 2006 10:06 am

Note the difference between mine and postlundc's (Dan) example. In Dan's example the array is declared in a static way (if that is the correct term). With my example your array is allocated after compilation and on the fly - although an array declared in a function is done on the fly it is removed when the function returns.

You will need a temporary array of vertices when you perform your transformations. You can have an array declared in a function - which may be better. However your data structure may not make it easy to just allocate an array of vertices in a function - especially if you store them in a struct/class allowing different sizes for your vertex array, which you probably will.

code to create an array of vertices using malloc and new
Code:
   Vertex *vertexArray = malloc (sizeof(Vertex) * ARRAYLEN);
   // or
   Vertex *vertexArray = new Vertex[ARRAYLEN];


code to create an array of pointers without using void, like I showed you before using malloc and new
Code:
   Vertex **vertexArrayPtr = malloc (sizeof(Vertex*) * ARRAYLEN);
   // or
   Vertex **vertexArrayPtr = new Vertex*[ARRAYLEN];

#69263 - Janekxx - Sun Jan 29, 2006 6:14 pm

Thanks a lot!
Now I understand how simply it can be done. :)
_________________
sony.int.pl