nge
03-31-2003, 10:33 AM
#define NR_END 1
...
...
float *vector(long nl, long nh)
/* allocate a float vector with subscript range v[nl..nh] */
{
float *v;
v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
if (!v) nrerror("allocation failure in vector()");
return v-nl+NR_END;
}
This code is from NR in C. The thing is I didn't understand the line "return v-nl+NR_END;". As far as I know malloc returns the pointer to the storage allocated. Why do we have to move the pointer position back by subtracting nl+NR_END. Can anyone explain me about this. Thanks.
...
...
float *vector(long nl, long nh)
/* allocate a float vector with subscript range v[nl..nh] */
{
float *v;
v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
if (!v) nrerror("allocation failure in vector()");
return v-nl+NR_END;
}
This code is from NR in C. The thing is I didn't understand the line "return v-nl+NR_END;". As far as I know malloc returns the pointer to the storage allocated. Why do we have to move the pointer position back by subtracting nl+NR_END. Can anyone explain me about this. Thanks.