Question on book Numerical recipes, 2nd ed.: allocation/deallocation of memory for vectors
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).
c numerical-recipes
|
show 2 more comments
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).
c numerical-recipes
1) Numerical Recipes uses first_index=1 indexing ( because it is basically converted FORTRAN code, which used offset=1 indexes) 2) before ANSI /c89 /c90 there was novoid*
and malloc and free used char pointers.
– joop
Nov 19 '18 at 16:32
@joop It doesn't use 1 indexing, the function lets you choose the first index.
– interjay
Nov 19 '18 at 16:32
For nl=0 it returns something different from what malloc returned.
– joop
Nov 19 '18 at 16:36
stackoverflow.com/q/41489210/2235885 for an example of someone assuming zero-based indexing.
– joop
Nov 19 '18 at 17:38
@joop, thanks for the explanation ofchar *
in C before the 1989 standard.
– Dmitry Kabanov
Nov 19 '18 at 18:53
|
show 2 more comments
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).
c numerical-recipes
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).
c numerical-recipes
c numerical-recipes
asked Nov 19 '18 at 16:25
Dmitry Kabanov
136110
136110
1) Numerical Recipes uses first_index=1 indexing ( because it is basically converted FORTRAN code, which used offset=1 indexes) 2) before ANSI /c89 /c90 there was novoid*
and malloc and free used char pointers.
– joop
Nov 19 '18 at 16:32
@joop It doesn't use 1 indexing, the function lets you choose the first index.
– interjay
Nov 19 '18 at 16:32
For nl=0 it returns something different from what malloc returned.
– joop
Nov 19 '18 at 16:36
stackoverflow.com/q/41489210/2235885 for an example of someone assuming zero-based indexing.
– joop
Nov 19 '18 at 17:38
@joop, thanks for the explanation ofchar *
in C before the 1989 standard.
– Dmitry Kabanov
Nov 19 '18 at 18:53
|
show 2 more comments
1) Numerical Recipes uses first_index=1 indexing ( because it is basically converted FORTRAN code, which used offset=1 indexes) 2) before ANSI /c89 /c90 there was novoid*
and malloc and free used char pointers.
– joop
Nov 19 '18 at 16:32
@joop It doesn't use 1 indexing, the function lets you choose the first index.
– interjay
Nov 19 '18 at 16:32
For nl=0 it returns something different from what malloc returned.
– joop
Nov 19 '18 at 16:36
stackoverflow.com/q/41489210/2235885 for an example of someone assuming zero-based indexing.
– joop
Nov 19 '18 at 17:38
@joop, thanks for the explanation ofchar *
in C before the 1989 standard.
– Dmitry Kabanov
Nov 19 '18 at 18:53
1) Numerical Recipes uses first_index=1 indexing ( because it is basically converted FORTRAN code, which used offset=1 indexes) 2) before ANSI /c89 /c90 there was no
void*
and malloc and free used char pointers.– joop
Nov 19 '18 at 16:32
1) Numerical Recipes uses first_index=1 indexing ( because it is basically converted FORTRAN code, which used offset=1 indexes) 2) before ANSI /c89 /c90 there was no
void*
and malloc and free used char pointers.– joop
Nov 19 '18 at 16:32
@joop It doesn't use 1 indexing, the function lets you choose the first index.
– interjay
Nov 19 '18 at 16:32
@joop It doesn't use 1 indexing, the function lets you choose the first index.
– interjay
Nov 19 '18 at 16:32
For nl=0 it returns something different from what malloc returned.
– joop
Nov 19 '18 at 16:36
For nl=0 it returns something different from what malloc returned.
– joop
Nov 19 '18 at 16:36
stackoverflow.com/q/41489210/2235885 for an example of someone assuming zero-based indexing.
– joop
Nov 19 '18 at 17:38
stackoverflow.com/q/41489210/2235885 for an example of someone assuming zero-based indexing.
– joop
Nov 19 '18 at 17:38
@joop, thanks for the explanation of
char *
in C before the 1989 standard.– Dmitry Kabanov
Nov 19 '18 at 18:53
@joop, thanks for the explanation of
char *
in C before the 1989 standard.– Dmitry Kabanov
Nov 19 '18 at 18:53
|
show 2 more comments
1 Answer
1
active
oldest
votes
Suppose you had
nl=1
andNR_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_END
to 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_END
decrements bynl
before incrementing byNR_END
. A correct implementation would bev+NR_END-nl
.
Note that if
nl
only ever has non-negative values, a much simpler implementation would be to simply allocatenh+1
values, and then you don't need any pointer arithmetic aftermalloc
or beforefree
.
Here you can see a quote from the book explaining this, from pages 940-941 of the second edition. Some quotes:
it might
happen in rare cases (and probably only on a segmented machine) that the expression b-1
has no representation at all. If this occurs, then there is no guarantee that the relation
b=(b-1)+1 is satisfied.
[....]
the parameter NR_END is used as a number of extra storage
locations allocated at the beginning of every vector or matrix block, simply for the purpose
of making offset pointer references guaranteed-representable.
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 ofmalloc
is also not needed.
1
I'm not buying your explanation ofNR_END
. What you say is true, but if indeed values ofnl
different from 0 and 1 are not supported then it is sufficient to addNR_END
to the size of the allocation, as indeed is done.vector()
could then simply return the value frommalloc()
without doing any arithmetic on it, andvector_free()
would not need to account for the same.
– John Bollinger
Nov 19 '18 at 17:29
@JohnBollinger It isn't my explanation, it is the book's explanation, See here. I did say in my answer that it would be much simpler to not perform any pointer arithmetic. It is in general quite bad code.
– interjay
Nov 19 '18 at 17:30
@interjay, thanks for the explanation! I wish I were more patient and looked at the book more carefully before asking my question here :-) I accept your answer.
– Dmitry Kabanov
Nov 19 '18 at 19:19
@interjay, regarding your third paragraph (starting with 'Note that if nl only ever has non-negative values'). Are you sure what you say is correct? I think, it is applicable only if you whant array to be indexed by 0, 1, ..., nh (inclusive). This routine expects the caller to index an array by nl, nl+1, ..., nh (inclusive).
– Dmitry Kabanov
Nov 19 '18 at 19:20
1
Thanks for the assumption of bad faith
– Tim Randall
Nov 19 '18 at 21:39
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378855%2fquestion-on-book-numerical-recipes-2nd-ed-allocation-deallocation-of-memory-f%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Suppose you had
nl=1
andNR_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_END
to 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_END
decrements bynl
before incrementing byNR_END
. A correct implementation would bev+NR_END-nl
.
Note that if
nl
only ever has non-negative values, a much simpler implementation would be to simply allocatenh+1
values, and then you don't need any pointer arithmetic aftermalloc
or beforefree
.
Here you can see a quote from the book explaining this, from pages 940-941 of the second edition. Some quotes:
it might
happen in rare cases (and probably only on a segmented machine) that the expression b-1
has no representation at all. If this occurs, then there is no guarantee that the relation
b=(b-1)+1 is satisfied.
[....]
the parameter NR_END is used as a number of extra storage
locations allocated at the beginning of every vector or matrix block, simply for the purpose
of making offset pointer references guaranteed-representable.
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 ofmalloc
is also not needed.
1
I'm not buying your explanation ofNR_END
. What you say is true, but if indeed values ofnl
different from 0 and 1 are not supported then it is sufficient to addNR_END
to the size of the allocation, as indeed is done.vector()
could then simply return the value frommalloc()
without doing any arithmetic on it, andvector_free()
would not need to account for the same.
– John Bollinger
Nov 19 '18 at 17:29
@JohnBollinger It isn't my explanation, it is the book's explanation, See here. I did say in my answer that it would be much simpler to not perform any pointer arithmetic. It is in general quite bad code.
– interjay
Nov 19 '18 at 17:30
@interjay, thanks for the explanation! I wish I were more patient and looked at the book more carefully before asking my question here :-) I accept your answer.
– Dmitry Kabanov
Nov 19 '18 at 19:19
@interjay, regarding your third paragraph (starting with 'Note that if nl only ever has non-negative values'). Are you sure what you say is correct? I think, it is applicable only if you whant array to be indexed by 0, 1, ..., nh (inclusive). This routine expects the caller to index an array by nl, nl+1, ..., nh (inclusive).
– Dmitry Kabanov
Nov 19 '18 at 19:20
1
Thanks for the assumption of bad faith
– Tim Randall
Nov 19 '18 at 21:39
|
show 3 more comments
Suppose you had
nl=1
andNR_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_END
to 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_END
decrements bynl
before incrementing byNR_END
. A correct implementation would bev+NR_END-nl
.
Note that if
nl
only ever has non-negative values, a much simpler implementation would be to simply allocatenh+1
values, and then you don't need any pointer arithmetic aftermalloc
or beforefree
.
Here you can see a quote from the book explaining this, from pages 940-941 of the second edition. Some quotes:
it might
happen in rare cases (and probably only on a segmented machine) that the expression b-1
has no representation at all. If this occurs, then there is no guarantee that the relation
b=(b-1)+1 is satisfied.
[....]
the parameter NR_END is used as a number of extra storage
locations allocated at the beginning of every vector or matrix block, simply for the purpose
of making offset pointer references guaranteed-representable.
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 ofmalloc
is also not needed.
1
I'm not buying your explanation ofNR_END
. What you say is true, but if indeed values ofnl
different from 0 and 1 are not supported then it is sufficient to addNR_END
to the size of the allocation, as indeed is done.vector()
could then simply return the value frommalloc()
without doing any arithmetic on it, andvector_free()
would not need to account for the same.
– John Bollinger
Nov 19 '18 at 17:29
@JohnBollinger It isn't my explanation, it is the book's explanation, See here. I did say in my answer that it would be much simpler to not perform any pointer arithmetic. It is in general quite bad code.
– interjay
Nov 19 '18 at 17:30
@interjay, thanks for the explanation! I wish I were more patient and looked at the book more carefully before asking my question here :-) I accept your answer.
– Dmitry Kabanov
Nov 19 '18 at 19:19
@interjay, regarding your third paragraph (starting with 'Note that if nl only ever has non-negative values'). Are you sure what you say is correct? I think, it is applicable only if you whant array to be indexed by 0, 1, ..., nh (inclusive). This routine expects the caller to index an array by nl, nl+1, ..., nh (inclusive).
– Dmitry Kabanov
Nov 19 '18 at 19:20
1
Thanks for the assumption of bad faith
– Tim Randall
Nov 19 '18 at 21:39
|
show 3 more comments
Suppose you had
nl=1
andNR_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_END
to 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_END
decrements bynl
before incrementing byNR_END
. A correct implementation would bev+NR_END-nl
.
Note that if
nl
only ever has non-negative values, a much simpler implementation would be to simply allocatenh+1
values, and then you don't need any pointer arithmetic aftermalloc
or beforefree
.
Here you can see a quote from the book explaining this, from pages 940-941 of the second edition. Some quotes:
it might
happen in rare cases (and probably only on a segmented machine) that the expression b-1
has no representation at all. If this occurs, then there is no guarantee that the relation
b=(b-1)+1 is satisfied.
[....]
the parameter NR_END is used as a number of extra storage
locations allocated at the beginning of every vector or matrix block, simply for the purpose
of making offset pointer references guaranteed-representable.
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 ofmalloc
is also not needed.
Suppose you had
nl=1
andNR_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_END
to 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_END
decrements bynl
before incrementing byNR_END
. A correct implementation would bev+NR_END-nl
.
Note that if
nl
only ever has non-negative values, a much simpler implementation would be to simply allocatenh+1
values, and then you don't need any pointer arithmetic aftermalloc
or beforefree
.
Here you can see a quote from the book explaining this, from pages 940-941 of the second edition. Some quotes:
it might
happen in rare cases (and probably only on a segmented machine) that the expression b-1
has no representation at all. If this occurs, then there is no guarantee that the relation
b=(b-1)+1 is satisfied.
[....]
the parameter NR_END is used as a number of extra storage
locations allocated at the beginning of every vector or matrix block, simply for the purpose
of making offset pointer references guaranteed-representable.
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 ofmalloc
is also not needed.
edited Nov 19 '18 at 17:39
answered Nov 19 '18 at 17:09
interjay
83.6k16202210
83.6k16202210
1
I'm not buying your explanation ofNR_END
. What you say is true, but if indeed values ofnl
different from 0 and 1 are not supported then it is sufficient to addNR_END
to the size of the allocation, as indeed is done.vector()
could then simply return the value frommalloc()
without doing any arithmetic on it, andvector_free()
would not need to account for the same.
– John Bollinger
Nov 19 '18 at 17:29
@JohnBollinger It isn't my explanation, it is the book's explanation, See here. I did say in my answer that it would be much simpler to not perform any pointer arithmetic. It is in general quite bad code.
– interjay
Nov 19 '18 at 17:30
@interjay, thanks for the explanation! I wish I were more patient and looked at the book more carefully before asking my question here :-) I accept your answer.
– Dmitry Kabanov
Nov 19 '18 at 19:19
@interjay, regarding your third paragraph (starting with 'Note that if nl only ever has non-negative values'). Are you sure what you say is correct? I think, it is applicable only if you whant array to be indexed by 0, 1, ..., nh (inclusive). This routine expects the caller to index an array by nl, nl+1, ..., nh (inclusive).
– Dmitry Kabanov
Nov 19 '18 at 19:20
1
Thanks for the assumption of bad faith
– Tim Randall
Nov 19 '18 at 21:39
|
show 3 more comments
1
I'm not buying your explanation ofNR_END
. What you say is true, but if indeed values ofnl
different from 0 and 1 are not supported then it is sufficient to addNR_END
to the size of the allocation, as indeed is done.vector()
could then simply return the value frommalloc()
without doing any arithmetic on it, andvector_free()
would not need to account for the same.
– John Bollinger
Nov 19 '18 at 17:29
@JohnBollinger It isn't my explanation, it is the book's explanation, See here. I did say in my answer that it would be much simpler to not perform any pointer arithmetic. It is in general quite bad code.
– interjay
Nov 19 '18 at 17:30
@interjay, thanks for the explanation! I wish I were more patient and looked at the book more carefully before asking my question here :-) I accept your answer.
– Dmitry Kabanov
Nov 19 '18 at 19:19
@interjay, regarding your third paragraph (starting with 'Note that if nl only ever has non-negative values'). Are you sure what you say is correct? I think, it is applicable only if you whant array to be indexed by 0, 1, ..., nh (inclusive). This routine expects the caller to index an array by nl, nl+1, ..., nh (inclusive).
– Dmitry Kabanov
Nov 19 '18 at 19:20
1
Thanks for the assumption of bad faith
– Tim Randall
Nov 19 '18 at 21:39
1
1
I'm not buying your explanation of
NR_END
. What you say is true, but if indeed values of nl
different from 0 and 1 are not supported then it is sufficient to add NR_END
to the size of the allocation, as indeed is done. vector()
could then simply return the value from malloc()
without doing any arithmetic on it, and vector_free()
would not need to account for the same.– John Bollinger
Nov 19 '18 at 17:29
I'm not buying your explanation of
NR_END
. What you say is true, but if indeed values of nl
different from 0 and 1 are not supported then it is sufficient to add NR_END
to the size of the allocation, as indeed is done. vector()
could then simply return the value from malloc()
without doing any arithmetic on it, and vector_free()
would not need to account for the same.– John Bollinger
Nov 19 '18 at 17:29
@JohnBollinger It isn't my explanation, it is the book's explanation, See here. I did say in my answer that it would be much simpler to not perform any pointer arithmetic. It is in general quite bad code.
– interjay
Nov 19 '18 at 17:30
@JohnBollinger It isn't my explanation, it is the book's explanation, See here. I did say in my answer that it would be much simpler to not perform any pointer arithmetic. It is in general quite bad code.
– interjay
Nov 19 '18 at 17:30
@interjay, thanks for the explanation! I wish I were more patient and looked at the book more carefully before asking my question here :-) I accept your answer.
– Dmitry Kabanov
Nov 19 '18 at 19:19
@interjay, thanks for the explanation! I wish I were more patient and looked at the book more carefully before asking my question here :-) I accept your answer.
– Dmitry Kabanov
Nov 19 '18 at 19:19
@interjay, regarding your third paragraph (starting with 'Note that if nl only ever has non-negative values'). Are you sure what you say is correct? I think, it is applicable only if you whant array to be indexed by 0, 1, ..., nh (inclusive). This routine expects the caller to index an array by nl, nl+1, ..., nh (inclusive).
– Dmitry Kabanov
Nov 19 '18 at 19:20
@interjay, regarding your third paragraph (starting with 'Note that if nl only ever has non-negative values'). Are you sure what you say is correct? I think, it is applicable only if you whant array to be indexed by 0, 1, ..., nh (inclusive). This routine expects the caller to index an array by nl, nl+1, ..., nh (inclusive).
– Dmitry Kabanov
Nov 19 '18 at 19:20
1
1
Thanks for the assumption of bad faith
– Tim Randall
Nov 19 '18 at 21:39
Thanks for the assumption of bad faith
– Tim Randall
Nov 19 '18 at 21:39
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378855%2fquestion-on-book-numerical-recipes-2nd-ed-allocation-deallocation-of-memory-f%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1) Numerical Recipes uses first_index=1 indexing ( because it is basically converted FORTRAN code, which used offset=1 indexes) 2) before ANSI /c89 /c90 there was no
void*
and malloc and free used char pointers.– joop
Nov 19 '18 at 16:32
@joop It doesn't use 1 indexing, the function lets you choose the first index.
– interjay
Nov 19 '18 at 16:32
For nl=0 it returns something different from what malloc returned.
– joop
Nov 19 '18 at 16:36
stackoverflow.com/q/41489210/2235885 for an example of someone assuming zero-based indexing.
– joop
Nov 19 '18 at 17:38
@joop, thanks for the explanation of
char *
in C before the 1989 standard.– Dmitry Kabanov
Nov 19 '18 at 18:53