Reason for (type) in (type)function() if the output of function() is ignored
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I was looking through the Linux kernel source code and in the file init/main.c
I saw the line
(void) ksys_dup(0);
I was wondering why (void)
is there. I've seen code like
char *str = (char *)malloc(len);
This makes sense, as malloc is declared to return a void pointer but the memory it allocates here needs to be pointed to by a char pointer. This does not explain the ksys_dup()
line, as its return value isn't recorded.
ksys_dup()
returns an integer, and there are function calls around that line whose returns also aren't recorded but don't have a type specified.
c types
add a comment |
I was looking through the Linux kernel source code and in the file init/main.c
I saw the line
(void) ksys_dup(0);
I was wondering why (void)
is there. I've seen code like
char *str = (char *)malloc(len);
This makes sense, as malloc is declared to return a void pointer but the memory it allocates here needs to be pointed to by a char pointer. This does not explain the ksys_dup()
line, as its return value isn't recorded.
ksys_dup()
returns an integer, and there are function calls around that line whose returns also aren't recorded but don't have a type specified.
c types
3
it is used to say explicitly that the return value is ignored
– shan
Nov 23 '18 at 4:13
1
This makes sense, as malloc is declared to return a void pointer but the memory it allocates here needs to be pointed to by a char pointer. – no, especially with*alloc()
it makes no sense sincevoid*
is implicitly convertible to all other pointer types. It just adds noise with no function.
– Swordfish
Nov 23 '18 at 4:26
See Should I cast the result ofmalloc()
? for diverging opinions on the merits or otherwise of the cast for a memory allocation function. Suffice to say, there are strong disagreements, but the majority is probably on the "don't cast" side (but the "it might hide that you've not declaredmalloc()
by including<stdlib.h>
" excuse is pathetic, and has been ever since C99 mandated that functions be declared before use — that's a problem with using compilers that only conform to archaic versions of the C standard).
– Jonathan Leffler
Nov 23 '18 at 7:02
@Swordfish I've never casted malloc() in my own code, but I've seen other developers do that and I was just using it as an example.
– Billy
Nov 23 '18 at 15:59
Precisely, the value returned is not used. This is the use of the(void)
cast, to avoid some compilers warning about a function returning a value that is dropped. The case ofmalloc()
I'm afraid is different, you are hidding the actual type returned with a cast (the type of malloc will be assumed by the compiler to be anint
if you make the mistake of not including the proper header) and that will hide the error shutting down the compiler warning (which makes the warning useles) See other comments for Why should you never cast the value returned by malloc(3).
– Luis Colorado
Nov 27 '18 at 8:38
add a comment |
I was looking through the Linux kernel source code and in the file init/main.c
I saw the line
(void) ksys_dup(0);
I was wondering why (void)
is there. I've seen code like
char *str = (char *)malloc(len);
This makes sense, as malloc is declared to return a void pointer but the memory it allocates here needs to be pointed to by a char pointer. This does not explain the ksys_dup()
line, as its return value isn't recorded.
ksys_dup()
returns an integer, and there are function calls around that line whose returns also aren't recorded but don't have a type specified.
c types
I was looking through the Linux kernel source code and in the file init/main.c
I saw the line
(void) ksys_dup(0);
I was wondering why (void)
is there. I've seen code like
char *str = (char *)malloc(len);
This makes sense, as malloc is declared to return a void pointer but the memory it allocates here needs to be pointed to by a char pointer. This does not explain the ksys_dup()
line, as its return value isn't recorded.
ksys_dup()
returns an integer, and there are function calls around that line whose returns also aren't recorded but don't have a type specified.
c types
c types
asked Nov 23 '18 at 4:09
BillyBilly
111219
111219
3
it is used to say explicitly that the return value is ignored
– shan
Nov 23 '18 at 4:13
1
This makes sense, as malloc is declared to return a void pointer but the memory it allocates here needs to be pointed to by a char pointer. – no, especially with*alloc()
it makes no sense sincevoid*
is implicitly convertible to all other pointer types. It just adds noise with no function.
– Swordfish
Nov 23 '18 at 4:26
See Should I cast the result ofmalloc()
? for diverging opinions on the merits or otherwise of the cast for a memory allocation function. Suffice to say, there are strong disagreements, but the majority is probably on the "don't cast" side (but the "it might hide that you've not declaredmalloc()
by including<stdlib.h>
" excuse is pathetic, and has been ever since C99 mandated that functions be declared before use — that's a problem with using compilers that only conform to archaic versions of the C standard).
– Jonathan Leffler
Nov 23 '18 at 7:02
@Swordfish I've never casted malloc() in my own code, but I've seen other developers do that and I was just using it as an example.
– Billy
Nov 23 '18 at 15:59
Precisely, the value returned is not used. This is the use of the(void)
cast, to avoid some compilers warning about a function returning a value that is dropped. The case ofmalloc()
I'm afraid is different, you are hidding the actual type returned with a cast (the type of malloc will be assumed by the compiler to be anint
if you make the mistake of not including the proper header) and that will hide the error shutting down the compiler warning (which makes the warning useles) See other comments for Why should you never cast the value returned by malloc(3).
– Luis Colorado
Nov 27 '18 at 8:38
add a comment |
3
it is used to say explicitly that the return value is ignored
– shan
Nov 23 '18 at 4:13
1
This makes sense, as malloc is declared to return a void pointer but the memory it allocates here needs to be pointed to by a char pointer. – no, especially with*alloc()
it makes no sense sincevoid*
is implicitly convertible to all other pointer types. It just adds noise with no function.
– Swordfish
Nov 23 '18 at 4:26
See Should I cast the result ofmalloc()
? for diverging opinions on the merits or otherwise of the cast for a memory allocation function. Suffice to say, there are strong disagreements, but the majority is probably on the "don't cast" side (but the "it might hide that you've not declaredmalloc()
by including<stdlib.h>
" excuse is pathetic, and has been ever since C99 mandated that functions be declared before use — that's a problem with using compilers that only conform to archaic versions of the C standard).
– Jonathan Leffler
Nov 23 '18 at 7:02
@Swordfish I've never casted malloc() in my own code, but I've seen other developers do that and I was just using it as an example.
– Billy
Nov 23 '18 at 15:59
Precisely, the value returned is not used. This is the use of the(void)
cast, to avoid some compilers warning about a function returning a value that is dropped. The case ofmalloc()
I'm afraid is different, you are hidding the actual type returned with a cast (the type of malloc will be assumed by the compiler to be anint
if you make the mistake of not including the proper header) and that will hide the error shutting down the compiler warning (which makes the warning useles) See other comments for Why should you never cast the value returned by malloc(3).
– Luis Colorado
Nov 27 '18 at 8:38
3
3
it is used to say explicitly that the return value is ignored
– shan
Nov 23 '18 at 4:13
it is used to say explicitly that the return value is ignored
– shan
Nov 23 '18 at 4:13
1
1
This makes sense, as malloc is declared to return a void pointer but the memory it allocates here needs to be pointed to by a char pointer. – no, especially with
*alloc()
it makes no sense since void*
is implicitly convertible to all other pointer types. It just adds noise with no function.– Swordfish
Nov 23 '18 at 4:26
This makes sense, as malloc is declared to return a void pointer but the memory it allocates here needs to be pointed to by a char pointer. – no, especially with
*alloc()
it makes no sense since void*
is implicitly convertible to all other pointer types. It just adds noise with no function.– Swordfish
Nov 23 '18 at 4:26
See Should I cast the result of
malloc()
? for diverging opinions on the merits or otherwise of the cast for a memory allocation function. Suffice to say, there are strong disagreements, but the majority is probably on the "don't cast" side (but the "it might hide that you've not declared malloc()
by including <stdlib.h>
" excuse is pathetic, and has been ever since C99 mandated that functions be declared before use — that's a problem with using compilers that only conform to archaic versions of the C standard).– Jonathan Leffler
Nov 23 '18 at 7:02
See Should I cast the result of
malloc()
? for diverging opinions on the merits or otherwise of the cast for a memory allocation function. Suffice to say, there are strong disagreements, but the majority is probably on the "don't cast" side (but the "it might hide that you've not declared malloc()
by including <stdlib.h>
" excuse is pathetic, and has been ever since C99 mandated that functions be declared before use — that's a problem with using compilers that only conform to archaic versions of the C standard).– Jonathan Leffler
Nov 23 '18 at 7:02
@Swordfish I've never casted malloc() in my own code, but I've seen other developers do that and I was just using it as an example.
– Billy
Nov 23 '18 at 15:59
@Swordfish I've never casted malloc() in my own code, but I've seen other developers do that and I was just using it as an example.
– Billy
Nov 23 '18 at 15:59
Precisely, the value returned is not used. This is the use of the
(void)
cast, to avoid some compilers warning about a function returning a value that is dropped. The case of malloc()
I'm afraid is different, you are hidding the actual type returned with a cast (the type of malloc will be assumed by the compiler to be an int
if you make the mistake of not including the proper header) and that will hide the error shutting down the compiler warning (which makes the warning useles) See other comments for Why should you never cast the value returned by malloc(3).– Luis Colorado
Nov 27 '18 at 8:38
Precisely, the value returned is not used. This is the use of the
(void)
cast, to avoid some compilers warning about a function returning a value that is dropped. The case of malloc()
I'm afraid is different, you are hidding the actual type returned with a cast (the type of malloc will be assumed by the compiler to be an int
if you make the mistake of not including the proper header) and that will hide the error shutting down the compiler warning (which makes the warning useles) See other comments for Why should you never cast the value returned by malloc(3).– Luis Colorado
Nov 27 '18 at 8:38
add a comment |
0
active
oldest
votes
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%2f53440552%2freason-for-type-in-typefunction-if-the-output-of-function-is-ignored%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53440552%2freason-for-type-in-typefunction-if-the-output-of-function-is-ignored%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
3
it is used to say explicitly that the return value is ignored
– shan
Nov 23 '18 at 4:13
1
This makes sense, as malloc is declared to return a void pointer but the memory it allocates here needs to be pointed to by a char pointer. – no, especially with
*alloc()
it makes no sense sincevoid*
is implicitly convertible to all other pointer types. It just adds noise with no function.– Swordfish
Nov 23 '18 at 4:26
See Should I cast the result of
malloc()
? for diverging opinions on the merits or otherwise of the cast for a memory allocation function. Suffice to say, there are strong disagreements, but the majority is probably on the "don't cast" side (but the "it might hide that you've not declaredmalloc()
by including<stdlib.h>
" excuse is pathetic, and has been ever since C99 mandated that functions be declared before use — that's a problem with using compilers that only conform to archaic versions of the C standard).– Jonathan Leffler
Nov 23 '18 at 7:02
@Swordfish I've never casted malloc() in my own code, but I've seen other developers do that and I was just using it as an example.
– Billy
Nov 23 '18 at 15:59
Precisely, the value returned is not used. This is the use of the
(void)
cast, to avoid some compilers warning about a function returning a value that is dropped. The case ofmalloc()
I'm afraid is different, you are hidding the actual type returned with a cast (the type of malloc will be assumed by the compiler to be anint
if you make the mistake of not including the proper header) and that will hide the error shutting down the compiler warning (which makes the warning useles) See other comments for Why should you never cast the value returned by malloc(3).– Luis Colorado
Nov 27 '18 at 8:38