Allegro.cc Forums » Programming Questions » Curious error message

 Edgar Reynaldo Member #8,592 May 2007 The key is to understand the difference between true 1, 2, and 3D matrices.If you allocate the entire matrix in one call to malloc it will be contiguous in memory. Otherwise it will be fragmented. You can use a true "3D" array by using pointers to pointers to pointers but it makes everything so complicated.A. ```int size = width*height*depth; int* array = (int*)malloc(sizeof(int)*size); for (int i = 0 ; i < size ; ++i) {array[i] = 0;} array[z*(width*height) + y*height + x] = x + y + z; ``` (EDIT I don't even know if this will compile) Versus B. ```int*** array3d = (int***)malloc(sizeof(int**)*depth); for (int i = 0 ; i < depth ; ++i) { array3d[i] = (int**)malloc(sizeof(int*)*height); for (int j = 0 ; j < height ; ++j) { array3d[i][j] = (int*)malloc(sizeof(int)*width); for (int k = 0 ; k < width ; ++k) { array3d[i][j][k] = 0; } } } array3d[z][y][x] = x + y + z; ``` Now which one of those structures looks easier to free? If you simply use a little math to access your elements its no problem.One of the best places to learn C and C++ are from the docs :http://en.cppreference.com/w/http://www.cplusplus.com/The second site has a great set of tutorials, good for learning C++.
 Audric Member #907 January 2001 In the C language, arrays have fixed size, known at compile time. There is a kind of dynamic array with [], but you may want to practice with pointers and dynamic allocation (malloc()) before using it.C-style linked list is when an instance of a struct acts as an chain link : Every instance contains a pointer to the next instance of the chain. The last one's pointer will point to NULL, to mark the end. This leads to such code, very common in C: ```// Perform an action on all items of "room" itemData *obj = room->items; while (item != NULL) // This condition is never entered if the room has no item at all (items == NULL), otherwise it will stop after handling the last item : the one whose ->next_item pointer is NULL { doAction(item); item = item->next_item; } // Same code, as a single 'for' loop : for (itemData *item = room->items; obj != NULL; item = item->next_item; doAction(item); ``` At this point I suggest you find a readable tutorial on linked lists. There are more complex structures and variants (which makes it easier and faster to count elements, add/remove, etc), but skipping the basics may cause you to waste a lot of time. Don't hesitate to make diagrams on paper, with arrows to represent "This pointer contains the address of ...".