www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/26/02:15:32

From: DavMac AT iname DOT com (Davin McCall)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help with array of structures in which I will make an array of one dimensional row pointers to...
Date: Wed, 25 Aug 1999 16:05:43 GMT
Organization: Monash Uni
Lines: 68
Distribution: world
Message-ID: <37c411ef.47141100@newsserver.cc.monash.edu.au>
References: <199908250534 DOT BAA23515 AT delorie DOT com>
NNTP-Posting-Host: damcc5.halls.monash.edu.au
X-Trace: towncrier.cc.monash.edu.au 935597072 3828 130.194.198.138 (25 Aug 1999 16:04:32 GMT)
X-Complaints-To: abuse AT monash DOT edu DOT au
NNTP-Posting-Date: 25 Aug 1999 16:04:32 GMT
X-Newsreader: Forte Free Agent 1.1/32.230
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Thu, 26 Aug 1999 06:21:38 -0700, "Dan Gold" <TedMat AT CoastNet DOT com>
wrote:

>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?

Only one four-byte pointer. The other members, when they accessed, are
done so by adding an offset to the pointer (this is done automatically
by the compiler).

>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.

yes

> Or would it offset the size of a
>char like the line pointers in Allegro?

This occurs because the line pointers are char pointers (or possibly
void pointers).

>.  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.

Maybe, maybe not. The multiplication is not likely to cause a
significant speed reduction so I wouldn't bother (especially as you
will be saving memory, which can lead to speed increase in other ways,
such as increased cache effectiveness).

>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?

Not by itself, though I stress that it's not such an important issue.
Incidentally the TILE structure you gave above would normally be of
size 8 bytes, which is a shift-left by three.

Davin.

__________________________________________________________
       *** davmac - sharkin'!! davmac AT iname DOT com ***
my programming page: http://yoyo.cc.monash.edu.au/~davmac/

- Raw text -


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