指针加一个整数 [英] pointer plus an integer

查看:114
本文介绍了指针加一个整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在查看一些遗留代码,这些代码以

为例,从NRC分配带有dmatrix()

函数的双矩阵如下:


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);


/ * ... * /


下面附有dmatrix()函数。


在程序的某个时刻,会出现这些行。


/ *调整指数而不是重新分配* /

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;


它在我看来这里发生的事情就是A,aa,

augin,而augout是指向双尺寸数组的指针,然后

A [i]是指向a的指针单个数组和声明

" A [i-1] + total + addcol"是一个整数到

一个指向数组的指针。


这是正确的吗?如果是这样,当一个人向一个数组的指针添加一个整数时,会发生什么?



感谢您的帮助;

Ivan;


double ** dmatrix(long nrl,long nrh,long ncl,long nch)

/ *分配带有下标范围的双矩阵m [nrl..nrh] [ncl..nch]

* /

{

long i,nrow = nrh-nrl + 1 ,ncol = nch-ncl + 1;

double ** m;


/ *分配行指针* /

m =(double **)malloc((unsigned int)((nrow

+ NR_END)* sizeof(double *)));

if(!m)nrerror( 矩阵()中的分配失败1);

m + = NR_END;

m - = nrl;


/ *分配行并设置指针* /

m [nrl] =(double *)malloc((unsigned int)((nrow * ncol + NR_END)* sizeof

(double)));

if(!m [nrl])nrerror(矩阵()中的分配失败2);

m [nrl] + = NR_END;

m [nrl] - = ncl;


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


/ *返回指向行指针数组的指针* /

返回m;

}


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[i-1] + total + addcol" is adding an integer to
a pointer to an array.

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

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;
}

推荐答案

2007年10月11日星期四11:42:00 -0700 ,Ivan K. < iv ********* @ yahoo.com>

在comp.lang.c中写道:
On Thu, 11 Oct 2007 11:42:00 -0700, "Ivan K." <iv*********@yahoo.com>
wrote in comp.lang.c:

> ;

我正在查看一些遗留代码,它开始于

从NRC分配带有dmatrix()

函数的双矩阵,如下所示:


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);


/ * ... * /


下面附有dmatrix()函数。


在程序中指出,这些行发生。


/ *调整指数而不是重新分配* /

for(i = 2; i< = nnonm; i ++ )A [i] = A [i-1] + total + addcol;

for(i = 2; i< = nnonm; i ++)aa [i] = aa [i-1] +总+ 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 ;


它在我看来这里发生的是A,aa,

augin和augout指向双尺寸数组的指针,然后

A [i]是指向单个数组的指针和声明

A [i-1] + total + addcol是一个整数到

一个指向数组的指针。


这是正确的吗?如果是这样,当一个人向一个数组的指针添加一个整数时,会发生什么?

>
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[i-1] + total + addcol" is adding an integer to
a pointer to an array.

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



不,这不对。 A不是指向双尺寸

数组的指针。 A不是指向任何类型数组的指针。


A正是它的定义,即指向(一个或

以上)的指针指针(一个或多个)double(s)。

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


感谢您的帮助;

Ivan;


double ** dmatrix(long nrl,long nrh,long ncl,long nch)

/ *分配一个带有下标范围m的双矩阵[nrl..nrh] [ncl..nch]

* /

{

long i,nrow = nrh-nrl + 1,ncol = nch-ncl + 1;

double ** m;


/ *分配行指针* /

m =(double **)malloc ((unsigned int)((nrow

+ NR_END)* sizeof(double *)));

if(!m)nrerror(" matrix failure 1 in matrix ()");

m + = NR_END;

m - = nrl;


/ *分配行并设置指针他们* /

m [nrl] =(double *)malloc((unsigned int)((nrow * ncol + NR_END)* sizeof

(double)));

if(!m [ nrl])nrerror(矩阵()中的分配失败2);

m [nrl] + = NR_END;

m [nrl] - = ncl;


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


/ *返回指向行指针数组的指针* /

返回m;

}
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;
}



这个是一个可怕的代码,看起来有人试图将FORTRAN

翻译成C,索引从1开始而不是0.一旦

指针被修改,它们就可以了'是免费的()'',所以程序

几乎肯定会造成巨大的内存泄漏。


在指向任何内容的指针上添加一个整数完整类型与

完全相同。整数由指向的类型的大小缩放,

在这种情况下sizeof double。添加的结果仍然是一个指向双倍的
指针,但仍然没有任何类型的数组。


为了一个简单的插图,让我们忘记关于如何分配内存分配并假设A是两个指针指向双倍的指针,

,每个指向三个双打。


A [0]指向1.0,2.0,3.0

A [1]指向4.0,5.0,6.0



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


....然后A [1 ]现在指向2.0,3.0。


只要A [1]的新值不能用于访问原来的结尾

原始分配,一切都很好。也就是说,A [1] [0]和A [1] [1]

是可访问的,但A [2]超过了已分配内存的末尾。

但是现在没有办法释放()A [1]

最初指向的分配内存,除非保存了原始指针的副本

b其他地方。


-

Jack Klein

主页: http://JK-Technology.Com



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

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


Jack Klein写道:
Jack Klein wrote:

On Thu,2007年10月11日11:42:00 -0700,Ivan K. < iv ********* @ yahoo.com>

在comp.lang.c中写道:
On Thu, 11 Oct 2007 11:42:00 -0700, "Ivan K." <iv*********@yahoo.com>
wrote in comp.lang.c:

> ;>我正在查看一些遗留代码,它首先从NRC分配带有dmatrix()
函数的双矩阵,如下所示:

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);

/ * ... * /

下面附有dmatrix()函数。

在程序的某个时刻,这些行会出现。

/ *调整指数而不是重新分配* /
(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;
>>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;



第一部分创建最大尺寸的矩阵。


第二部分摆弄指针,使矩阵看起来像是b
其他一些(较小的)大小,大概是代码中那个

点所需的实际大小。


这是一个常见的Fortran成语。它在C中是不必要的,因为你可以

在运行时分配你需要的内存量。在Fortran中,

必须在编译时创建一个固定最大大小的数组。

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.


> ;>并且它在我看来这里发生的是A,aa,
augin和augout是指向双尺寸数组的指针,然后
A [i]是指向a的指针单个数组和声明
A [i-1] + total + addcol是添加一个整数来指向一个数组的指针。

这是正确的吗?
>>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[i-1] + total + addcol" is adding an integer to
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?



向指针添加整数会增加指针。如果p指向数组中的第一个元素

,则p + 2指向第三个元素。


简化的伪代码版本dmatrix()将是


double ** A;


A = malloc(行指针存储);

A [0] = malloc(整个2D阵列的内存足够);

A [1] = A [0] +(一行中的双打数量);

A [2] = A [1] +(一行中的双打数量);

...


A [0 ]指向内存块的开始。 A [n]指向第n行的开始




dmatrix()调用之后的代码部分只设置A [n]'s
每行使用不同数量的双打。

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.


不,这不正确。 A不是指向双尺寸

数组的指针。 A不是指向任何类型数组的指针。


A正是它的定义,即指向(一个或

以上)的指针指针指向(一个或多个)double(s)。
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).



嗯,好吧,但这是否会增加或减少OP的混乱?

从概念上讲,A不仅仅是* *指向double的指针。在这个上下文中,它表示动态分配的2D数组。除了

,我们不能严格地称之为。我们可以称之为什么?


在K& R2中,我们有类似(5.9)的引用:


给定定义


int a [10] [20];

int * b [10];


....对于b但是,定义只分配10个指针,而b / b $ b不会初始化它们;初始化必须明确地完成,

静态或代码。假设b

的每个元素确实指向一个二十元素的数组...


所以K& R可以说b [n]是一个指向数组的指针。

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)
/ *分配带下标范围的双矩阵m [nrl..nrh] [ncl..nch] * /
>>double **dmatrix( long nrl, long nrh, long ncl, long nch)
/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */



这是可怕的代码,看起来有人试图将FORTRAN

翻译成C,


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



这就是它的确切含义。这是改编自C版数字食谱第一版中同名的实用功能


http://www.nr.com/

这是对K& RC的直接翻译最初的数字

用Fortran编写的食谱。

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.


,索引从1开始而不是0.
with the indices beginning at 1 instead of 0.



实际上,行索引的范围介于nrl和nrh之间,而列

索引的范围介于ncl和nch之间。 Fortran允许您在声明数组时指定

任何最小和最大整数索引。

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.



每个矩阵只分配一个内存块。指向它的指针是

存储为指向第一行的指针,在指针中没有修改,并且稍后在匹配的调用中恢复

free_dmatrix(),它还必须将nrl作为参数,以便它可以

撤消索引偏移量。


它仍然是可怕的,但只有当他们忘记free_dmatrix(),

或(可能)如果他们传递错误的nrl时才会泄漏。


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


2007年10月11日星期四21:43:52 -0500,Jack Klein< ja ******* @ spamcop.net>

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

> On Thu,2007年10月11日11:42:00 -0700,Ivan K. < iv ********* @ yahoo.com>
在comp.lang.c中写道:
>On Thu, 11 Oct 2007 11:42:00 -0700, "Ivan K." <iv*********@yahoo.com>
wrote in comp.lang.c:

>> <我正在查看一些遗留代码,它首先从NRC分配带有dmatrix()
函数的双矩阵,如下所示:

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);

/ * ... * /

下面附有dmatrix()函数。

在程序的某个时刻,这些行会出现。

/ *调整指数而不是重新分配* /
(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; augin和augout指向双尺寸数组,然后
A [i]是一个指向单个数组的指针和声明
A [i-1] + total + addcol是添加一个整数来指向一个数组的指针。

这是正确的吗?如果是这样,当一个向数组的指针添加一个整数时会发生什么?
>>
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[i-1] + total + addcol" is adding an integer to
a pointer to an array.

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


不,这不对。 A不是指向双尺寸数组的指针。 A不是指向任何类型数组的指针。
它正是它被定义为的指针,即指向(一个或多个)指针的指针(一个或多个)更多)double(s)。


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


>感谢您的帮助;
Ivan;

double ** dmatrix( long nrl,long nrh,long ncl,long nch)
/ *分配一个带有下标范围m的双矩阵[nrl..nrh] [ncl..nch]
* /
{
long i,nrow = nrh-nrl + 1,ncol = nch-ncl + 1;
double ** m;

/ *分配行指针* /
m =(double **)malloc((unsigned int)((nrow
+ NR_END)* sizeof(double *)));
if(!m)nrerror(" allocation failure 1 in matrix()");
m + = NR_END;
m - = nrl;

/ *分配行并设置指针* /
m [ nrl] =(double *)malloc((unsigned int)((nrow * ncol + NR_END)* sizeof
(double)));
if(!m [nrl])nrerror(" allocation矩阵()中的失败2;);
m [nrl] + = NR_END;
m [nrl] - = ncl;
/ *返回指向数组的指针行指针* /
返回m;
}
>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;
}


这是一个可怕的代码,看起来有人试图将FORTRAN翻译成C,用索引从1开始而不是0.一旦
指针被修改,它们就不能自由(),所以程序几乎肯定会造成巨大的内存泄漏。


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.



它看起来像C中的Numerical Recipes的示例代码。

要获得从1-10而不是0-9索引的数组做


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

m - = 1;


当然,那是'未定义的行为。


Jim

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


这篇关于指针加一个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆