The book Numerical recipes, 2nd edition (http://numerical.recipes) uses the following code to allocate/deallocate a memory for a vector v with subscripts [nl..nh]:
#define NR_END 1
#define FREE_ARG char*
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;
}
void free_vector(float *v, long nl, long nh)
/* free a float vector allocated with vector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
Question 1: What is the purpose of adding/subtracting NR_END elements?
Question 2: What is the purpose of converting float * to char * in free_vector?
I understand that +1 in malloc is due to the inclusive right boundary of the array (which is non-inclusive usually in C).
Suppose you had
nl=1andNR_END=0. Then the returned pointer would be out of bounds (it points before the allocated block). This is undefined behavior and can lead to incorrect results, although it is unlikely to cause problems on major compilers because the pointer would be incremented back before it is dereferenced.To avoid this undefined behavior, you can set
NR_ENDto the maximum expected value ofnl(which is 1 in the book). This guarantees that the returned pointer is valid. However, the implementation given in the question is still incorrect, becausev-nl+NR_ENDdecrements bynlbefore incrementing byNR_END. A correct implementation would bev+NR_END-nl.Note that if
nlonly ever has non-negative values, a much simpler implementation would be to simply allocatenh+1values, and then you don't need any pointer arithmetic aftermallocor beforefree.Here you can see a quote from the book explaining this, from pages 940-941 of the second edition. Some quotes:
[....]
The cast to
char*is not needed in any standardized version of C. It may have been needed in ancient versions. Casting the return value ofmallocis also not needed.