delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/02/08/07:32:41

From: Hans-Bernhard Broeker <broeker AT acp3bf DOT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Memory allocation
Date: 8 Feb 2000 11:30:34 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 27
Message-ID: <87ouoq$jta$1@nets3.rz.RWTH-Aachen.DE>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 1000207183129 DOT 20861D-100000 AT is>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 950009434 20394 137.226.32.75 (8 Feb 2000 11:30:34 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 8 Feb 2000 11:30:34 GMT
User-Agent: tin/1.4-19991113 ("No Labels") (UNIX) (Linux/2.0.0 (i586))
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Eli Zaretskii <eliz AT is DOT elta DOT co DOT il> wrote:

> On Mon, 7 Feb 2000, Martin Chan wrote:
>> transposed_matrix->array=(double**)calloc(row, sizeof(double*);
>> --(transposed_matrix->array);    /*offset pointer*/
[...]

> Also note that arrays in C begin from offset zero, so you should either 
> begin from zero and end at row-1, or use array[i-1] in the loop body.

Not really, due to that old, bad trick of offsetting the pointer. It's
a well-known, but still fundamentally bad idea. It seems to have been
invented by the authors of the otherwise nice book 'Numerical
Recipes', for their C edition.  That's what you get from
Fortran-thinking guys having been forced to write C code --- they were
so used to 1-based array indexing that nothing in the world could have
convinced them to switch to the natural 0-based one, for the C version
of their code. Instead, they move all array start pointers backwards,
by one element, and go on indexing 1-based.

The idea is bad because by definition of the C standard, the program
may segfault at this point. After the subtraction, the pointer value
may have become illegal, and the machine is allowed to trap that and
abort the program.
-- 
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