www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/25/05:12:54

Message-Id: <199908250534.BAA23515@delorie.com>
From: "Dan Gold" <TedMat AT CoastNet DOT com>
To: <djgpp AT delorie DOT com>
Subject: Help with array of structures in which I will make an array of one dimensional row pointers to...
Date: Thu, 26 Aug 1999 06:21:38 -0700
X-MSMail-Priority: Normal
X-Priority: 3
X-Mailer: Microsoft Internet Mail 4.70.1155
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

Hey, it's a little bit long but it's written clearly so...

I have a TILE structure for a game.  I have a MAP structure that has a TILE
*dat member that will be a big allocated array of all the tiles in the map.
 Is a pointer to a structure only and index to the start of it in memory or
a structure which replaces every member as a pointer to that member? here
is what I mean.

here we have my tile struct as it is so far:

typedef struct TILE {
     int type;
     int src_x, src_y; 
} TILE;

Would a pointer to TILE point to the member "type" and add the offset
accordingly down the line as you reference the other members (meaning it
only used one 4 byte pointer) OR does it make 3 pointers each one to a
different member? It's a newbie question, I know how to make it work but
now I have memory concers...=) I believe a structure is just like an array
and is always based on a single pointer index, but??

now If I allocate the data like so for all the tiles...
MAP * the_map;
the_map->dat = (TILE *)malloc(w * h * sizeof(TILE));

And since I want to be able to reference the data like a two dimensional
array I need a bunch of line pointers to get the correct offset for the
start of each row. So I then add a TILE * tile[0]; member to the MAP
structure so I can dynamically allocate all the line pointers at run time
when creating the memory for the MAP structure to hold the data as so (this
is done before the allocation of TILE data ofcourse).

the_map = (MAP *)malloc(sizeof(MAP) + (sizeof(TILE) * h));  

Now once I've assigned all the line pointers to the tile data at their
appropriote positions is I reference the tiles as: 
the_map->tile[3][2];  // would the second array bracket offset by a TILE
meaning it would jump to the second tile. Or would it offset the size of a
char like the line pointers in Allegro?.  If they do offset to the next
tile, would it be faster to reference them by creating a pointer to every
single TILE in the map data, considering the compiler would have to offset
[2] with a multiplication of the size of TILE.  
Would compiler optimization change my struct to a size which could be bit
shifted, by the column value for the address? Can you view compiler
optimized code with the -s extension when compiling?

Thanks so much for the help, This question was a bit long for what I asked
but I had to make sure people would understand.

from ((--((--((Dan Gold))--))--))



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019