Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   pointer plus an integer (http://www.velocityreviews.com/forums/t543642-pointer-plus-an-integer.html)

 Ivan K. 10-11-2007 06:42 PM

pointer plus an integer

I am looking at some legacy code, which begins by
allocating a double matrix with the dmatrix()
function from NRC as follows:

double **A, **augin, **augout, **aa;

A = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
aa = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
augin = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
augout = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);

/* ... */

The dmatrix() function is appended below.

At some point in the program, these lines occur.

/* adjust indices rather than reallocate */
for(i=2;i<=nnonm;i++) A[i] = A[i-1] + total + addcol;
for(i=2;i<=nnonm;i++) aa[i] = aa[i-1] + total + addcol;
for(i=2;i<=nnonm;i++) augin[i] = augin[i-1] + total + addcol + 1 +
nsims;
for(i=2;i<=nnonm;i++) augout[i] = augout[i-1] + total + addcol + 1
+ nsims;

and it looks to me that what is going on here is that as A, aa,
augin, and augout are pointers to double dimensioned arrays, then
A[i] is a pointer to a single array and the statement
a pointer to an array.

Is this correct? If so, what is happening
when one adds an integer to a pointer to an array?

Ivan;

double **dmatrix( long nrl, long nrh, long ncl, long nch)
/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch]
*/
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
double **m;

/* allocate pointers to rows */
m=(double **) malloc((unsigned int)((nrow
+NR_END)*sizeof(double*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;

/* allocate rows and set pointers to them */
m[nrl]=(double *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof
(double)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;

for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;

/* return pointer to array of pointers to rows */
return m;
}

 Jack Klein 10-12-2007 02:43 AM

Re: pointer plus an integer

On Thu, 11 Oct 2007 11:42:00 -0700, "Ivan K." <ivan_521521@yahoo.com>
wrote in comp.lang.c:

>
> I am looking at some legacy code, which begins by
> allocating a double matrix with the dmatrix()
> function from NRC as follows:
>
> double **A, **augin, **augout, **aa;
>
> A = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
> aa = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
> augin = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
> augout = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
>
> /* ... */
>
> The dmatrix() function is appended below.
>
> At some point in the program, these lines occur.
>
> /* adjust indices rather than reallocate */
> for(i=2;i<=nnonm;i++) A[i] = A[i-1] + total + addcol;
> for(i=2;i<=nnonm;i++) aa[i] = aa[i-1] + total + addcol;
> for(i=2;i<=nnonm;i++) augin[i] = augin[i-1] + total + addcol + 1 +
> nsims;
> for(i=2;i<=nnonm;i++) augout[i] = augout[i-1] + total + addcol + 1
> + nsims;
>
> and it looks to me that what is going on here is that as A, aa,
> augin, and augout are pointers to double dimensioned arrays, then
> A[i] is a pointer to a single array and the statement
> a pointer to an array.
>
> Is this correct? If so, what is happening
> when one adds an integer to a pointer to an array?

No, that isn't correct. A is not a pointer to a "double dimensioned
array". A is not a pointer to any sort of array.

A is exactly what it is defined to be, namely a pointer to (one or
more) pointer(s) to (one or more) double(s).

> Thank you for your help;
> Ivan;
>
> double **dmatrix( long nrl, long nrh, long ncl, long nch)
> /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch]
> */
> {
> long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
> double **m;
>
> /* allocate pointers to rows */
> m=(double **) malloc((unsigned int)((nrow
> +NR_END)*sizeof(double*)));
> if (!m) nrerror("allocation failure 1 in matrix()");
> m += NR_END;
> m -= nrl;
>
> /* allocate rows and set pointers to them */
> m[nrl]=(double *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof
> (double)));
> if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
> m[nrl] += NR_END;
> m[nrl] -= ncl;
>
> for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
>
> /* return pointer to array of pointers to rows */
> return m;
> }

This is hideous code, it looks like somebody tried translating FORTRAN
to C, with the indices beginning at 1 instead of 0. And once the
pointers have been modified, they can't be free()'d, so the program
almost certainly creates a huge memory leak.

Adding an integer to a pointer to any complete type works exactly the
same way. The integer is scaled by the size of the type pointed to,
in this case sizeof double. The result of the addition is still a
pointer to double, and still not any kind of array.

For a simple illustration, let's forget about how the memory is
allocated and assume that A is a pointer to two pointers to double,
and each one of them points to three doubles.

A[0] points to 1.0, 2.0, 3.0
A[1] points to 4.0, 5.0, 6.0

Now if we execute the statement:

A[1] = A[0] + 1;

....then A[1] now points to 2.0, 3.0.

As long as the new value of A[1] is not use to access past the end of
the original allocation, all is well. That is, A[1][0] and A[1][1]
are accessible, but A[2] is past the end of the allocated memory.

But there is now no way to free() the allocated memory that A[1]
originally pointed to, unless a copy of the original pointer was saved
somewhere else.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

 Ernie Wright 10-12-2007 06:58 AM

Re: pointer plus an integer

Jack Klein wrote:

> On Thu, 11 Oct 2007 11:42:00 -0700, "Ivan K." <ivan_521521@yahoo.com>
> wrote in comp.lang.c:
>
>>I am looking at some legacy code, which begins by
>>allocating a double matrix with the dmatrix()
>>function from NRC as follows:
>>
>> double **A, **augin, **augout, **aa;
>>
>> A = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
>> aa = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
>> augin = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
>> augout = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
>>
>> /* ... */
>>
>>The dmatrix() function is appended below.
>>
>>At some point in the program, these lines occur.
>>
>> /* adjust indices rather than reallocate */
>> for(i=2;i<=nnonm;i++) A[i] = A[i-1] + total + addcol;
>> for(i=2;i<=nnonm;i++) aa[i] = aa[i-1] + total + addcol;
>> for(i=2;i<=nnonm;i++) augin[i] = augin[i-1] + total + addcol + 1 + nsims;
>> for(i=2;i<=nnonm;i++) augout[i] = augout[i-1] + total + addcol + 1 + nsims;
>>

The first part creates matrices of maximum size.

The second part fiddles with pointers to make the matrices appear to be
some other (smaller) size, presumably the actual size needed at that
point in the code.

This is a common Fortran idiom. It's unnecessary in C, since you can
allocate exactly the amount of memory you need at run time. In Fortran,
an array of fixed maximum size has to be created at compile time.

>>and it looks to me that what is going on here is that as A, aa,
>>augin, and augout are pointers to double dimensioned arrays, then
>>A[i] is a pointer to a single array and the statement
>>a pointer to an array.
>>
>>Is this correct?

Pretty much, except that the terminology is wrong. But let's ignore the
terminology problem for a minute.

>> If so, what is happening when one adds an integer to a pointer to
>> an array?

Adding an integer to a pointer increments the pointer. If p points to
the first element in an array, then p + 2 points to the third element.

A simplified, pseudocode version of what happens in dmatrix() would be

double **A;

A = malloc( storage for row pointers );
A[ 0 ] = malloc( enough memory for the entire 2D array );
A[ 1 ] = A[ 0 ] + ( the number of doubles in one row );
A[ 2 ] = A[ 1 ] + ( the number of doubles in one row );
...

A[0] points to the start of the memory block. A[n] points to the start
of the n-th row.

The part of the code after the dmatrix() calls just sets the A[n]'s
using a different number of doubles per row.

> No, that isn't correct. A is not a pointer to a "double dimensioned
> array". A is not a pointer to any sort of array.
>
> A is exactly what it is defined to be, namely a pointer to (one or
> more) pointer(s) to (one or more) double(s).

Well, OK, but does that add to or subtract from the OP's confusion?
Conceptually, A is not *merely* a pointer to a pointer to double. In
this context, it denotes a dynamically allocated 2D "array," except that
maybe we can't, strictly speaking, call it that. What can we call it?

In K&R2, we have references like (5.9):

Given the definitions

int a[10][20];
int *b[10];

....For b, however, the definition only allocates 10 pointers and
does not initialize them; initialization must be done explicitly,
either statically or with code. Assuming that each element of b
does point to a twenty-element array...

So K&R were OK with saying that b[n] is a pointer to an array.

>>double **dmatrix( long nrl, long nrh, long ncl, long nch)
>>/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */

>
> This is hideous code, it looks like somebody tried translating FORTRAN
> to C,

That's precisely what it is. This is adapted from a utility function
by the same name in the first edition of Numerical Recipes in C,

http://www.nr.com/

which was a straight translation to K&R C from the original Numerical
Recipes written in Fortran.

> with the indices beginning at 1 instead of 0.

Actually, the row indices will range between nrl and nrh, and the column
indices will range between ncl and nch. Fortran allows you to specify
any minimum and maximum integer index when you declare an array.

> And once the pointers have been modified, they can't be free()'d, so
> the program almost certainly creates a huge memory leak.

Only one block of memory is allocated per matrix. The pointer to it is
stored as the pointer to the first row, which isn't modified in the
fiddling with the pointers, and recovered later in a matching call to
free_dmatrix(), which must also take nrl as an argument so that it can
undo the indexing offset.

It's still hideous, but it only leaks if they forget to free_dmatrix(),
or (possibly) if they pass it the wrong nrl.

- Ernie http://home.comcast.net/~erniew

 JimS 10-12-2007 10:37 PM

Re: pointer plus an integer

On Thu, 11 Oct 2007 21:43:52 -0500, Jack Klein <jackklein@spamcop.net>
wrote:

>On Thu, 11 Oct 2007 11:42:00 -0700, "Ivan K." <ivan_521521@yahoo.com>
>wrote in comp.lang.c:
>
>>
>> I am looking at some legacy code, which begins by
>> allocating a double matrix with the dmatrix()
>> function from NRC as follows:
>>
>> double **A, **augin, **augout, **aa;
>>
>> A = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
>> aa = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
>> augin = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
>> augout = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
>>
>> /* ... */
>>
>> The dmatrix() function is appended below.
>>
>> At some point in the program, these lines occur.
>>
>> /* adjust indices rather than reallocate */
>> for(i=2;i<=nnonm;i++) A[i] = A[i-1] + total + addcol;
>> for(i=2;i<=nnonm;i++) aa[i] = aa[i-1] + total + addcol;
>> for(i=2;i<=nnonm;i++) augin[i] = augin[i-1] + total + addcol + 1 +
>> nsims;
>> for(i=2;i<=nnonm;i++) augout[i] = augout[i-1] + total + addcol + 1
>> + nsims;
>>
>> and it looks to me that what is going on here is that as A, aa,
>> augin, and augout are pointers to double dimensioned arrays, then
>> A[i] is a pointer to a single array and the statement
>> a pointer to an array.
>>
>> Is this correct? If so, what is happening
>> when one adds an integer to a pointer to an array?

>
>No, that isn't correct. A is not a pointer to a "double dimensioned
>array". A is not a pointer to any sort of array.
>
>A is exactly what it is defined to be, namely a pointer to (one or
>more) pointer(s) to (one or more) double(s).
>
>> Thank you for your help;
>> Ivan;
>>
>> double **dmatrix( long nrl, long nrh, long ncl, long nch)
>> /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch]
>> */
>> {
>> long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
>> double **m;
>>
>> /* allocate pointers to rows */
>> m=(double **) malloc((unsigned int)((nrow
>> +NR_END)*sizeof(double*)));
>> if (!m) nrerror("allocation failure 1 in matrix()");
>> m += NR_END;
>> m -= nrl;
>>
>> /* allocate rows and set pointers to them */
>> m[nrl]=(double *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof
>> (double)));
>> if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
>> m[nrl] += NR_END;
>> m[nrl] -= ncl;
>>
>> for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
>>
>> /* return pointer to array of pointers to rows */
>> return m;
>> }

>
>This is hideous code, it looks like somebody tried translating FORTRAN
>to C, with the indices beginning at 1 instead of 0. And once the
>pointers have been modified, they can't be free()'d, so the program
>almost certainly creates a huge memory leak.

It looks like sample code from Numerical Recipes in C.
To get an array indexed from 1-10 instead of 0-9 they do

double *m = malloc(10* sizeof *m);
m -= 1;

Of course, that's undefined behaviour.

Jim

Re: pointer plus an integer

"Jack Klein" <jackklein@spamcop.net> wrote in message
news:22ntg3h90f7tp8podkbcakjffo3h3bofvg@4ax.com...
> On Thu, 11 Oct 2007 11:42:00 -0700, "Ivan K." <ivan_521521@yahoo.com>
> wrote in comp.lang.c:
>
>>
>> I am looking at some legacy code, which begins by
>> allocating a double matrix with the dmatrix()
>> function from NRC as follows:
>>
>> double **A, **augin, **augout, **aa;
>>
>> A = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
>> aa = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
>> augin = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
>> augout = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
>>
>> /* ... */
>>
>> The dmatrix() function is appended below.
>>
>> At some point in the program, these lines occur.
>>
>> /* adjust indices rather than reallocate */
>> for(i=2;i<=nnonm;i++) A[i] = A[i-1] + total + addcol;
>> for(i=2;i<=nnonm;i++) aa[i] = aa[i-1] + total + addcol;
>> for(i=2;i<=nnonm;i++) augin[i] = augin[i-1] + total + addcol + 1 +
>> nsims;
>> for(i=2;i<=nnonm;i++) augout[i] = augout[i-1] + total + addcol + 1
>> + nsims;
>>
>> and it looks to me that what is going on here is that as A, aa,
>> augin, and augout are pointers to double dimensioned arrays, then
>> A[i] is a pointer to a single array and the statement
>> a pointer to an array.
>>
>> Is this correct? If so, what is happening
>> when one adds an integer to a pointer to an array?

>
> No, that isn't correct. A is not a pointer to a "double dimensioned
> array". A is not a pointer to any sort of array.
>
> A is exactly what it is defined to be, namely a pointer to (one or
> more) pointer(s) to (one or more) double(s).
>
>> Thank you for your help;
>> Ivan;
>>
>> double **dmatrix( long nrl, long nrh, long ncl, long nch)
>> /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch]
>> */
>> {
>> long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
>> double **m;
>>
>> /* allocate pointers to rows */
>> m=(double **) malloc((unsigned int)((nrow
>> +NR_END)*sizeof(double*)));
>> if (!m) nrerror("allocation failure 1 in matrix()");
>> m += NR_END;
>> m -= nrl;
>>
>> /* allocate rows and set pointers to them */
>> m[nrl]=(double *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof
>> (double)));
>> if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
>> m[nrl] += NR_END;
>> m[nrl] -= ncl;
>>
>> for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
>>
>> /* return pointer to array of pointers to rows */
>> return m;
>> }

>
> This is hideous code, it looks like somebody tried translating FORTRAN
> to C, with the indices beginning at 1 instead of 0. And once the
> pointers have been modified, they can't be free()'d, so the program
> almost certainly creates a huge memory leak.

It's equally or more hideous as fortran. I'm guessing matlab or something.
--
"Nicht verzagen, Bruder Grinde fragen."

 James Kuyper Jr. 10-13-2007 03:33 PM

Re: pointer plus an integer

Jack Klein wrote:
> On Thu, 11 Oct 2007 11:42:00 -0700, "Ivan K." <ivan_521521@yahoo.com>
> wrote in comp.lang.c:
>
>> I am looking at some legacy code, which begins by
>> allocating a double matrix with the dmatrix()
>> function from NRC as follows:

....
>> double **dmatrix( long nrl, long nrh, long ncl, long nch)
>> /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch]
>> */
>> {
>> long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
>> double **m;
>>
>> /* allocate pointers to rows */
>> m=(double **) malloc((unsigned int)((nrow
>> +NR_END)*sizeof(double*)));
>> if (!m) nrerror("allocation failure 1 in matrix()");
>> m += NR_END;
>> m -= nrl;
>>
>> /* allocate rows and set pointers to them */
>> m[nrl]=(double *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof
>> (double)));
>> if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
>> m[nrl] += NR_END;
>> m[nrl] -= ncl;
>>
>> for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
>>
>> /* return pointer to array of pointers to rows */
>> return m;
>> }

>
> This is hideous code, it looks like somebody tried translating FORTRAN
> to C, with the indices beginning at 1 instead of 0.

Correct. That's exactly the historical development of the NRC (Numerical
Recipes in C) library. I've translated a fair number of their
"Fortran-inspired C" routines into "native C" style; the effort was
significant; avoiding off-by-one errors in loops required constant
vigilance - I can understand why someone not very familiar with C might
have chosen this awful work-around to avoid having to do that same work.

But someone not very familiar with C shouldn't have been writing
"Numerical Recipes in C". I own the book, and I find it very useful, but
only because it contains detailed information about a lot of algorithms
that are relevant to my interests; not because the source code is well
written.

> And once the
> pointers have been modified, they can't be free()'d, so the program
> almost certainly creates a huge memory leak.

The Numerical Recipes library provides routines for both allocating and
deallocating arrays built in this style. It's precisely as easy to
forget to call the array deallocation function as it is to forget to
call free(). I don't see any increased risk of a memory leak.

Re: pointer plus an integer

"James Kuyper Jr." <jameskuyper@verizon.net> wrote in message
news:7z5Qi.793\$hI1.195@trndny06...
> Jack Klein wrote:
>> On Thu, 11 Oct 2007 11:42:00 -0700, "Ivan K." <ivan_521521@yahoo.com>
>> wrote in comp.lang.c:
>>
>>> I am looking at some legacy code, which begins by
>>> allocating a double matrix with the dmatrix()
>>> function from NRC as follows:

> ...
>>> double **dmatrix( long nrl, long nrh, long ncl, long nch)
>>> /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch]
>>> */
>>> {
>>> long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
>>> double **m;
>>>
>>> /* allocate pointers to rows */
>>> m=(double **) malloc((unsigned int)((nrow
>>> +NR_END)*sizeof(double*)));
>>> if (!m) nrerror("allocation failure 1 in matrix()");
>>> m += NR_END;
>>> m -= nrl;
>>>
>>> /* allocate rows and set pointers to them */
>>> m[nrl]=(double *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof
>>> (double)));
>>> if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
>>> m[nrl] += NR_END;
>>> m[nrl] -= ncl;
>>>
>>> for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
>>>
>>> /* return pointer to array of pointers to rows */
>>> return m;
>>> }

>>
>> This is hideous code, it looks like somebody tried translating FORTRAN
>> to C, with the indices beginning at 1 instead of 0.

>
> Correct. That's exactly the historical development of the NRC (Numerical
> Recipes in C) library. I've translated a fair number of their
> "Fortran-inspired C" routines into "native C" style; the effort was
> significant; avoiding off-by-one errors in loops required constant
> vigilance - I can understand why someone not very familiar with C might
> have chosen this awful work-around to avoid having to do that same work.

No, incorrect. This pointer-infested slop was never fortran.

The fortran would look more like:
real, allocatable :: A(:,:)
....
allocate (A(rows,columns))
> But someone not very familiar with C shouldn't have been writing
> "Numerical Recipes in C". I own the book, and I find it very useful, but
> only because it contains detailed information about a lot of algorithms
> that are relevant to my interests; not because the source code is well
> written.
>
>> And once the
>> pointers have been modified, they can't be free()'d, so the program
>> almost certainly creates a huge memory leak.

>
> The Numerical Recipes library provides routines for both allocating and
> deallocating arrays built in this style. It's precisely as easy to forget
> to call the array deallocation function as it is to forget to call free().
> I don't see any increased risk of a memory leak.

In fortran, the common C extension, all you need to do is:
deallocate (A)
, no free's, malloc's or leaks.
--
"Nicht verzagen, Bruder Grinde fragen."

 Ernie Wright 10-15-2007 04:22 AM

Re: pointer plus an integer

JimS wrote:

> It looks like sample code from Numerical Recipes in C.

It was clearly derived from that book, but it differs from the original
NRC code.

> To get an array indexed from 1-10 instead of 0-9 they do
>
> double *m = malloc(10* sizeof *m);
> m -= 1;
>
> Of course, that's undefined behaviour.

The book was written before C89.

- Ernie http://home.comcast.net/~erniew

 Ernie Wright 10-15-2007 04:32 AM

Re: pointer plus an integer

> "James Kuyper Jr." <jameskuyper@verizon.net> wrote in message
> news:7z5Qi.793\$hI1.195@trndny06...
>
>>Jack Klein wrote:
>>
>>>This is hideous code, it looks like somebody tried translating FORTRAN
>>>to C, with the indices beginning at 1 instead of 0.

>>
>>Correct.

>
> No, incorrect. This pointer-infested slop was never fortran.
>
> The fortran would look more like:
> real, allocatable :: A(:,:)
> ...
> allocate (A(rows,columns))

That's Fortran 90. The original Numerical Recipes was written using
Fortran 77.

- Ernie http://home.comcast.net/~erniew

 Ben Pfaff 10-15-2007 04:38 AM

Re: pointer plus an integer

Ernie Wright <erniew@comcast.net> writes:

>> It looks like sample code from Numerical Recipes in C.
>> [...]
>> double *m = malloc(10* sizeof *m);
>> m -= 1;
>>
>> Of course, that's undefined behaviour.

>
> The book was written before C89.

The latest edition has a discussion of the issue and a fix for
the code, although the authors do not exactly take a
comp.lang.c-approved attitude toward it.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

All times are GMT. The time now is 12:59 AM.