Test errno for ENOMEM, instead of comparing a series of malloc calls to NULLHow detect malloc failure?Make malloc() return NULL instead of crashing the program?Correct use of EXIT_FAILURE in C program?Implementing malloc with gcc in an arm7 problems : malloc return NULLAllocating memory and freeing them. Should we set them to NULL?What happens when blocks are freed from heap by free()?Does memory allocated in a function still stay allocated after the function returns?malloc unable to allocate memorymemory request LinuxHow to deal with assert() in a function, when you have dynamically allocated memory in main?Create a exit function
What is this unknown executable on my boot volume? Is it Malicious?
Why is the T-1000 humanoid?
Should I leave the first authorship of our paper to the student who did the project whereas I solved it?
How unbalanced coaxial cables are used for broadcasting TV signals without any problems?
Can I disable a battery powered device by reversing half of its batteries?
Why island and not light?
What exactly is a marshrutka (маршрутка)?
How do EVA suits manage water excretion?
Where can I get an anonymous Rav Kav card issued?
Could a Scotland-NI bridge break Brexit impasse?
How are aircraft depainted?
Is English tonal for some words, like "permit"?
How seriously should I take a CBP interview where I was told I have a red flag and could only stay for 30 days?
What is a realistic time needed to get a properly trained army?
How can I discourage sharing internal API keys within a company?
Why do sellers care about down payments?
Do ibuprofen or paracetamol cause hearing loss?
Leaving out pronouns in informal conversation
Is "you will become a subject matter expert" code for "you'll be working on your own 100% of the time"?
Where does the expression "triple-A" come from?
Are Democrats more likely to believe Astrology is a science?
Maintenance tips to prolong engine lifespan for short trips
Telling my mother that I have anorexia without panicking her
Why isn't `typename` required for a base class that is a nested type?
Test errno for ENOMEM, instead of comparing a series of malloc calls to NULL
How detect malloc failure?Make malloc() return NULL instead of crashing the program?Correct use of EXIT_FAILURE in C program?Implementing malloc with gcc in an arm7 problems : malloc return NULLAllocating memory and freeing them. Should we set them to NULL?What happens when blocks are freed from heap by free()?Does memory allocated in a function still stay allocated after the function returns?malloc unable to allocate memorymemory request LinuxHow to deal with assert() in a function, when you have dynamically allocated memory in main?Create a exit function
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Normally, one tests for the result of malloc
to not be NULL
to known whether memory allocation succeeded. With a series of malloc
calls, this becomes a lengthy or tedious set of comparisons.
Instead, could one set errno = 0
at the top of the series of malloc
calls, and then test for errno == ENOMEM
at the end?
This assumes if any allocation fails, the program or function can't proceed and has to return/bail out. It also assumes the malloc
calls are sequential and continuous, and that, as per the manual, malloc
can only set errno
to ENOMEM
.
An example would be something like the following code:
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#define N (1 << 20)
int main()
double *a = NULL;
double *b = NULL;
double *c = NULL;
errno = 0;
a = malloc(N * sizeof *a);
b = malloc(N * sizeof *b);
c = malloc(N * sizeof *c);
if (errno == ENOMEM)
perror(NULL);
free(a);
free(b);
free(c);
return EXIT_FAILURE;
errno = 0;
/* Do interesting stuff */
free(a);
free(b);
free(c);
return EXIT_SUCCESS;
(The example uses main()
, but it could also be another function that simply can't be run, but the program might proceed otherwise, and no actual exit from the program happens, and the free()
calls are necessary.)
I don't see any reason why this can't be done safely, but it's not an idiom I have come across, hence the question.
c
|
show 10 more comments
Normally, one tests for the result of malloc
to not be NULL
to known whether memory allocation succeeded. With a series of malloc
calls, this becomes a lengthy or tedious set of comparisons.
Instead, could one set errno = 0
at the top of the series of malloc
calls, and then test for errno == ENOMEM
at the end?
This assumes if any allocation fails, the program or function can't proceed and has to return/bail out. It also assumes the malloc
calls are sequential and continuous, and that, as per the manual, malloc
can only set errno
to ENOMEM
.
An example would be something like the following code:
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#define N (1 << 20)
int main()
double *a = NULL;
double *b = NULL;
double *c = NULL;
errno = 0;
a = malloc(N * sizeof *a);
b = malloc(N * sizeof *b);
c = malloc(N * sizeof *c);
if (errno == ENOMEM)
perror(NULL);
free(a);
free(b);
free(c);
return EXIT_FAILURE;
errno = 0;
/* Do interesting stuff */
free(a);
free(b);
free(c);
return EXIT_SUCCESS;
(The example uses main()
, but it could also be another function that simply can't be run, but the program might proceed otherwise, and no actual exit from the program happens, and the free()
calls are necessary.)
I don't see any reason why this can't be done safely, but it's not an idiom I have come across, hence the question.
c
My best guess to an answer is either that this is indeed safe, or that malloc setting errno to ENOMEM is less standard across malloc implementations than malloc returning NULL.
– 0 0
Mar 28 at 10:07
1
If malloc for a succeded but failed for b and c, wouldn't free(b) and free(c) give error? I know it does not give error for c++ but I am not sure about C compilers.
– Suven Pandey
Mar 28 at 10:10
1
If yourmalloc
implementation setserrno
upon error (which may not be the case, it's platform dependent), then your code is safe.
– Jabberwocky
Mar 28 at 10:11
2
@suvenpandey Ifmalloc
fails, it will also returnNULL
. Callingfree
with aNULL
parameter is safe in C.
– Gerhardh
Mar 28 at 10:16
1
@user3629249 I can answer that last question myself, I realise: check forfp == NULL
, then check the value oferrno
to see what the actual error is.
– 0 0
Mar 28 at 21:05
|
show 10 more comments
Normally, one tests for the result of malloc
to not be NULL
to known whether memory allocation succeeded. With a series of malloc
calls, this becomes a lengthy or tedious set of comparisons.
Instead, could one set errno = 0
at the top of the series of malloc
calls, and then test for errno == ENOMEM
at the end?
This assumes if any allocation fails, the program or function can't proceed and has to return/bail out. It also assumes the malloc
calls are sequential and continuous, and that, as per the manual, malloc
can only set errno
to ENOMEM
.
An example would be something like the following code:
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#define N (1 << 20)
int main()
double *a = NULL;
double *b = NULL;
double *c = NULL;
errno = 0;
a = malloc(N * sizeof *a);
b = malloc(N * sizeof *b);
c = malloc(N * sizeof *c);
if (errno == ENOMEM)
perror(NULL);
free(a);
free(b);
free(c);
return EXIT_FAILURE;
errno = 0;
/* Do interesting stuff */
free(a);
free(b);
free(c);
return EXIT_SUCCESS;
(The example uses main()
, but it could also be another function that simply can't be run, but the program might proceed otherwise, and no actual exit from the program happens, and the free()
calls are necessary.)
I don't see any reason why this can't be done safely, but it's not an idiom I have come across, hence the question.
c
Normally, one tests for the result of malloc
to not be NULL
to known whether memory allocation succeeded. With a series of malloc
calls, this becomes a lengthy or tedious set of comparisons.
Instead, could one set errno = 0
at the top of the series of malloc
calls, and then test for errno == ENOMEM
at the end?
This assumes if any allocation fails, the program or function can't proceed and has to return/bail out. It also assumes the malloc
calls are sequential and continuous, and that, as per the manual, malloc
can only set errno
to ENOMEM
.
An example would be something like the following code:
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#define N (1 << 20)
int main()
double *a = NULL;
double *b = NULL;
double *c = NULL;
errno = 0;
a = malloc(N * sizeof *a);
b = malloc(N * sizeof *b);
c = malloc(N * sizeof *c);
if (errno == ENOMEM)
perror(NULL);
free(a);
free(b);
free(c);
return EXIT_FAILURE;
errno = 0;
/* Do interesting stuff */
free(a);
free(b);
free(c);
return EXIT_SUCCESS;
(The example uses main()
, but it could also be another function that simply can't be run, but the program might proceed otherwise, and no actual exit from the program happens, and the free()
calls are necessary.)
I don't see any reason why this can't be done safely, but it's not an idiom I have come across, hence the question.
c
c
asked Mar 28 at 10:05
0 00 0
2,4221 gold badge8 silver badges19 bronze badges
2,4221 gold badge8 silver badges19 bronze badges
My best guess to an answer is either that this is indeed safe, or that malloc setting errno to ENOMEM is less standard across malloc implementations than malloc returning NULL.
– 0 0
Mar 28 at 10:07
1
If malloc for a succeded but failed for b and c, wouldn't free(b) and free(c) give error? I know it does not give error for c++ but I am not sure about C compilers.
– Suven Pandey
Mar 28 at 10:10
1
If yourmalloc
implementation setserrno
upon error (which may not be the case, it's platform dependent), then your code is safe.
– Jabberwocky
Mar 28 at 10:11
2
@suvenpandey Ifmalloc
fails, it will also returnNULL
. Callingfree
with aNULL
parameter is safe in C.
– Gerhardh
Mar 28 at 10:16
1
@user3629249 I can answer that last question myself, I realise: check forfp == NULL
, then check the value oferrno
to see what the actual error is.
– 0 0
Mar 28 at 21:05
|
show 10 more comments
My best guess to an answer is either that this is indeed safe, or that malloc setting errno to ENOMEM is less standard across malloc implementations than malloc returning NULL.
– 0 0
Mar 28 at 10:07
1
If malloc for a succeded but failed for b and c, wouldn't free(b) and free(c) give error? I know it does not give error for c++ but I am not sure about C compilers.
– Suven Pandey
Mar 28 at 10:10
1
If yourmalloc
implementation setserrno
upon error (which may not be the case, it's platform dependent), then your code is safe.
– Jabberwocky
Mar 28 at 10:11
2
@suvenpandey Ifmalloc
fails, it will also returnNULL
. Callingfree
with aNULL
parameter is safe in C.
– Gerhardh
Mar 28 at 10:16
1
@user3629249 I can answer that last question myself, I realise: check forfp == NULL
, then check the value oferrno
to see what the actual error is.
– 0 0
Mar 28 at 21:05
My best guess to an answer is either that this is indeed safe, or that malloc setting errno to ENOMEM is less standard across malloc implementations than malloc returning NULL.
– 0 0
Mar 28 at 10:07
My best guess to an answer is either that this is indeed safe, or that malloc setting errno to ENOMEM is less standard across malloc implementations than malloc returning NULL.
– 0 0
Mar 28 at 10:07
1
1
If malloc for a succeded but failed for b and c, wouldn't free(b) and free(c) give error? I know it does not give error for c++ but I am not sure about C compilers.
– Suven Pandey
Mar 28 at 10:10
If malloc for a succeded but failed for b and c, wouldn't free(b) and free(c) give error? I know it does not give error for c++ but I am not sure about C compilers.
– Suven Pandey
Mar 28 at 10:10
1
1
If your
malloc
implementation sets errno
upon error (which may not be the case, it's platform dependent), then your code is safe.– Jabberwocky
Mar 28 at 10:11
If your
malloc
implementation sets errno
upon error (which may not be the case, it's platform dependent), then your code is safe.– Jabberwocky
Mar 28 at 10:11
2
2
@suvenpandey If
malloc
fails, it will also return NULL
. Calling free
with a NULL
parameter is safe in C.– Gerhardh
Mar 28 at 10:16
@suvenpandey If
malloc
fails, it will also return NULL
. Calling free
with a NULL
parameter is safe in C.– Gerhardh
Mar 28 at 10:16
1
1
@user3629249 I can answer that last question myself, I realise: check for
fp == NULL
, then check the value of errno
to see what the actual error is.– 0 0
Mar 28 at 21:05
@user3629249 I can answer that last question myself, I realise: check for
fp == NULL
, then check the value of errno
to see what the actual error is.– 0 0
Mar 28 at 21:05
|
show 10 more comments
2 Answers
2
active
oldest
votes
Instead, could one set errno = 0 at the top of the series of malloc calls, and then test for errno == ENOMEM at the end?
Yes you could as long as your implementation of malloc
is documented to set errno
to ENOMEM
. The specification in the C11 standard (§ 7.22.3.4) only mentions that the pointer returned will be NULL
, not that errno
will be set, so your code is technically not portable.
The default implementation of malloc
in macOS, Windows and in Linux does set errno
so that's most of the computers in the World covered. However, if true portability is required, at the end just write
if (a == NULL || b == NULL || c == NULL)
// Handle the failure
Addendum: There's no need to reset errno
back to zero after the malloc
s.
Just for completeness, is there a link to the relevant section in the standard, or is that not possible?
– 0 0
Mar 28 at 10:31
@9769953 here.
– Alexey Frunze
Mar 28 at 10:34
@9769953 I've added the section number. There's no handy HTML version of the standard available on the Internet without handing over a wodge of cash. I have a PDF of a late draft which I got here and is, to all intents and purposes identical.
– JeremyP
Mar 28 at 10:36
@AlexeyFrunze and JeremyP: Thanks for the addition/link!
– 0 0
Mar 28 at 10:38
add a comment
|
No, you can not safely do this in a portable manner.
Per 7.5 Errors <errno.h>
, paragraph 3 of the C standard:
The value of
errno
in the initial thread is zero at program startup (the initial value of errno in other threads is an indeterminate value), but is never set to zero by any library function. The value oferrno
may be set to nonzero by a library function call whether or not there is an error, provided the use oferrno
is not documented in the description of the function in this International Standard.
Since malloc()
is not documented to set errno
by the C standard, it's free to munge errno
on success.
add a comment
|
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/4.0/"u003ecc by-sa 4.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%2f55394858%2ftest-errno-for-enomem-instead-of-comparing-a-series-of-malloc-calls-to-null%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead, could one set errno = 0 at the top of the series of malloc calls, and then test for errno == ENOMEM at the end?
Yes you could as long as your implementation of malloc
is documented to set errno
to ENOMEM
. The specification in the C11 standard (§ 7.22.3.4) only mentions that the pointer returned will be NULL
, not that errno
will be set, so your code is technically not portable.
The default implementation of malloc
in macOS, Windows and in Linux does set errno
so that's most of the computers in the World covered. However, if true portability is required, at the end just write
if (a == NULL || b == NULL || c == NULL)
// Handle the failure
Addendum: There's no need to reset errno
back to zero after the malloc
s.
Just for completeness, is there a link to the relevant section in the standard, or is that not possible?
– 0 0
Mar 28 at 10:31
@9769953 here.
– Alexey Frunze
Mar 28 at 10:34
@9769953 I've added the section number. There's no handy HTML version of the standard available on the Internet without handing over a wodge of cash. I have a PDF of a late draft which I got here and is, to all intents and purposes identical.
– JeremyP
Mar 28 at 10:36
@AlexeyFrunze and JeremyP: Thanks for the addition/link!
– 0 0
Mar 28 at 10:38
add a comment
|
Instead, could one set errno = 0 at the top of the series of malloc calls, and then test for errno == ENOMEM at the end?
Yes you could as long as your implementation of malloc
is documented to set errno
to ENOMEM
. The specification in the C11 standard (§ 7.22.3.4) only mentions that the pointer returned will be NULL
, not that errno
will be set, so your code is technically not portable.
The default implementation of malloc
in macOS, Windows and in Linux does set errno
so that's most of the computers in the World covered. However, if true portability is required, at the end just write
if (a == NULL || b == NULL || c == NULL)
// Handle the failure
Addendum: There's no need to reset errno
back to zero after the malloc
s.
Just for completeness, is there a link to the relevant section in the standard, or is that not possible?
– 0 0
Mar 28 at 10:31
@9769953 here.
– Alexey Frunze
Mar 28 at 10:34
@9769953 I've added the section number. There's no handy HTML version of the standard available on the Internet without handing over a wodge of cash. I have a PDF of a late draft which I got here and is, to all intents and purposes identical.
– JeremyP
Mar 28 at 10:36
@AlexeyFrunze and JeremyP: Thanks for the addition/link!
– 0 0
Mar 28 at 10:38
add a comment
|
Instead, could one set errno = 0 at the top of the series of malloc calls, and then test for errno == ENOMEM at the end?
Yes you could as long as your implementation of malloc
is documented to set errno
to ENOMEM
. The specification in the C11 standard (§ 7.22.3.4) only mentions that the pointer returned will be NULL
, not that errno
will be set, so your code is technically not portable.
The default implementation of malloc
in macOS, Windows and in Linux does set errno
so that's most of the computers in the World covered. However, if true portability is required, at the end just write
if (a == NULL || b == NULL || c == NULL)
// Handle the failure
Addendum: There's no need to reset errno
back to zero after the malloc
s.
Instead, could one set errno = 0 at the top of the series of malloc calls, and then test for errno == ENOMEM at the end?
Yes you could as long as your implementation of malloc
is documented to set errno
to ENOMEM
. The specification in the C11 standard (§ 7.22.3.4) only mentions that the pointer returned will be NULL
, not that errno
will be set, so your code is technically not portable.
The default implementation of malloc
in macOS, Windows and in Linux does set errno
so that's most of the computers in the World covered. However, if true portability is required, at the end just write
if (a == NULL || b == NULL || c == NULL)
// Handle the failure
Addendum: There's no need to reset errno
back to zero after the malloc
s.
edited Mar 28 at 10:34
answered Mar 28 at 10:26
JeremyPJeremyP
76.2k14 gold badges109 silver badges150 bronze badges
76.2k14 gold badges109 silver badges150 bronze badges
Just for completeness, is there a link to the relevant section in the standard, or is that not possible?
– 0 0
Mar 28 at 10:31
@9769953 here.
– Alexey Frunze
Mar 28 at 10:34
@9769953 I've added the section number. There's no handy HTML version of the standard available on the Internet without handing over a wodge of cash. I have a PDF of a late draft which I got here and is, to all intents and purposes identical.
– JeremyP
Mar 28 at 10:36
@AlexeyFrunze and JeremyP: Thanks for the addition/link!
– 0 0
Mar 28 at 10:38
add a comment
|
Just for completeness, is there a link to the relevant section in the standard, or is that not possible?
– 0 0
Mar 28 at 10:31
@9769953 here.
– Alexey Frunze
Mar 28 at 10:34
@9769953 I've added the section number. There's no handy HTML version of the standard available on the Internet without handing over a wodge of cash. I have a PDF of a late draft which I got here and is, to all intents and purposes identical.
– JeremyP
Mar 28 at 10:36
@AlexeyFrunze and JeremyP: Thanks for the addition/link!
– 0 0
Mar 28 at 10:38
Just for completeness, is there a link to the relevant section in the standard, or is that not possible?
– 0 0
Mar 28 at 10:31
Just for completeness, is there a link to the relevant section in the standard, or is that not possible?
– 0 0
Mar 28 at 10:31
@9769953 here.
– Alexey Frunze
Mar 28 at 10:34
@9769953 here.
– Alexey Frunze
Mar 28 at 10:34
@9769953 I've added the section number. There's no handy HTML version of the standard available on the Internet without handing over a wodge of cash. I have a PDF of a late draft which I got here and is, to all intents and purposes identical.
– JeremyP
Mar 28 at 10:36
@9769953 I've added the section number. There's no handy HTML version of the standard available on the Internet without handing over a wodge of cash. I have a PDF of a late draft which I got here and is, to all intents and purposes identical.
– JeremyP
Mar 28 at 10:36
@AlexeyFrunze and JeremyP: Thanks for the addition/link!
– 0 0
Mar 28 at 10:38
@AlexeyFrunze and JeremyP: Thanks for the addition/link!
– 0 0
Mar 28 at 10:38
add a comment
|
No, you can not safely do this in a portable manner.
Per 7.5 Errors <errno.h>
, paragraph 3 of the C standard:
The value of
errno
in the initial thread is zero at program startup (the initial value of errno in other threads is an indeterminate value), but is never set to zero by any library function. The value oferrno
may be set to nonzero by a library function call whether or not there is an error, provided the use oferrno
is not documented in the description of the function in this International Standard.
Since malloc()
is not documented to set errno
by the C standard, it's free to munge errno
on success.
add a comment
|
No, you can not safely do this in a portable manner.
Per 7.5 Errors <errno.h>
, paragraph 3 of the C standard:
The value of
errno
in the initial thread is zero at program startup (the initial value of errno in other threads is an indeterminate value), but is never set to zero by any library function. The value oferrno
may be set to nonzero by a library function call whether or not there is an error, provided the use oferrno
is not documented in the description of the function in this International Standard.
Since malloc()
is not documented to set errno
by the C standard, it's free to munge errno
on success.
add a comment
|
No, you can not safely do this in a portable manner.
Per 7.5 Errors <errno.h>
, paragraph 3 of the C standard:
The value of
errno
in the initial thread is zero at program startup (the initial value of errno in other threads is an indeterminate value), but is never set to zero by any library function. The value oferrno
may be set to nonzero by a library function call whether or not there is an error, provided the use oferrno
is not documented in the description of the function in this International Standard.
Since malloc()
is not documented to set errno
by the C standard, it's free to munge errno
on success.
No, you can not safely do this in a portable manner.
Per 7.5 Errors <errno.h>
, paragraph 3 of the C standard:
The value of
errno
in the initial thread is zero at program startup (the initial value of errno in other threads is an indeterminate value), but is never set to zero by any library function. The value oferrno
may be set to nonzero by a library function call whether or not there is an error, provided the use oferrno
is not documented in the description of the function in this International Standard.
Since malloc()
is not documented to set errno
by the C standard, it's free to munge errno
on success.
answered Mar 28 at 12:42
Andrew HenleAndrew Henle
22.3k3 gold badges15 silver badges37 bronze badges
22.3k3 gold badges15 silver badges37 bronze badges
add a comment
|
add a comment
|
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.
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%2f55394858%2ftest-errno-for-enomem-instead-of-comparing-a-series-of-malloc-calls-to-null%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
My best guess to an answer is either that this is indeed safe, or that malloc setting errno to ENOMEM is less standard across malloc implementations than malloc returning NULL.
– 0 0
Mar 28 at 10:07
1
If malloc for a succeded but failed for b and c, wouldn't free(b) and free(c) give error? I know it does not give error for c++ but I am not sure about C compilers.
– Suven Pandey
Mar 28 at 10:10
1
If your
malloc
implementation setserrno
upon error (which may not be the case, it's platform dependent), then your code is safe.– Jabberwocky
Mar 28 at 10:11
2
@suvenpandey If
malloc
fails, it will also returnNULL
. Callingfree
with aNULL
parameter is safe in C.– Gerhardh
Mar 28 at 10:16
1
@user3629249 I can answer that last question myself, I realise: check for
fp == NULL
, then check the value oferrno
to see what the actual error is.– 0 0
Mar 28 at 21:05