www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/26/20:07:36

Date: Thu, 26 Aug 1999 15:28:26 +0200
From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Message-Id: <199908261328.PAA32184@acp3bf.physik.rwth-aachen.de>
To: djgpp AT delorie DOT com
Subject: Re: Struct Array Allocation, Line Pointers, Automatic Offseting?
Newsgroups: comp.os.msdos.djgpp
Organization: RWTH Aachen, III. physikalisches Institut B
X-Newsreader: TIN [version 1.2 PL2]
Reply-To: djgpp AT delorie DOT com

[All this is not really on-topic for c.o.m.djgpp. It's a rather
general C question, so it should be handled over in comp.lang.c or
it's .moderated version]

In article <199908260412 DOT AAA09411 AT delorie DOT com> you wrote:
> I made an 1 dimensional array of a struct and made line pointers to the
> start of each row to make it like a 2 dimensional array.  If I want to
> reference the line pointer as ->line[4][5], it will start on the row index
> 4, but will the 5 jump 5 tiles structs ahead as I allocated or just bytes. 

Depends on how, exactly, you've set up those row pointers. The general
way for a dynamic 2D array of type 'element', using the row-pointer
method, is this:

	element **array = malloc(rows * sizeof(element *));
	int i;

	array[0] = malloc (rows*columns * sizeof(element);
	for (i=1; i<columns; i++) 
	   array[i] = array[i-1] + columns;

The basic trick is that array[0] is a pointer to 'element', so it will
jump around in units of 'sizeof(element)', if increased/decreased.

> If it does jump ahead structs for me, is there any speed concern when
> multiplying the address location 5 times by the size of the struct? [4th
> row] + [5 * sizeof TILE] 

No speed concern you should worry too much about. If there's anything
to be gained by that, 'gcc' will generally be smart enough to
automatically see that it could you a running pointer into the matrix,
to avoid all those multiplications.

> One more thing.  Does a struct pointer equal one 32bit address or an
> address for each member of the struct?

One pointer only. But that question was already answered, before, in 
another article.

--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


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