This question is subject of opinion-based abuse.
Sometimes I notice comments like that:
Don't cast the result of malloc
or
Why you don't cast the result of malloc
on questions where OP uses casting. The comments itself contain a hyperlink to this question.
That is in any possible manner inappropriate and incorrect as well. There is no right and no wrong when it is truly a matter of one's own coding-style.
Why is this happening?
It's based upon two reasons:
This question is indeed opinion-based. Technically, the question should have been closed as opinion-based years ago. A "Do I" or "Don't I" or equivalent "Should I" or "Shouldn't I" question, you just can't answer focused without an attitude of one's own opinion. One of the reason to close a question is because it "might lead to opinion-based answers" as it is well shown here.
Many answers (including the most apparent and accepted answer of @unwind) are either completely or almost entirely opinion-based (f.e. a mysterious "clutter" that would be added to your code if you do casting or repeating yourself would be bad) and show a clear and focused tendency to omit the cast. They argue about the redundancy of the cast on one side but also and even worse argue to solve a bug caused by a bug/failure of programming itself - to not #include <stdlib.h> if one want to use malloc().
I want to bring a true view of some points discussed, with less of my personal opinion. A few points need to be noted especially:
Such a very susceptible question to fall into one's own opinion needs an answer with neutral pros and cons. Not only cons or pros.
A good overview of pros and cons is listed in this answer:
https://stackoverflow.com/a/33047365/12139179
(I personally consider this because of that reason the best answer, so far.)
One reason which is encountered at most to reason the omission of the cast is that the cast might hide a bug.
If someone uses an implicit declared malloc() that returns int (implicit functions are gone from the standard since C99) and sizeof(int) != sizeof(int*), as shown in this question
Why does this code segfault on 64-bit architecture but work fine on 32-bit?
the cast would hide a bug.
While this is true, it only shows half of the story as the omission of the cast would only be a forward-bringing solution to an even bigger bug - not including stdlib.h when using malloc().
This will never be a serious issue, If you,
Use a compiler compliant to C99 or above (which is recommended and should be mandatory), and
Aren't so absent to forgot to include stdlib.h, when you want to use malloc() in your code, which is a huge bug itself.
Some people argue about C++ compliance of C code, as the cast is obliged in C++.
First of all to say in general: Compiling C code with a C++ compiler is not a good practice.
C and C++ are in fact two completely different languages with different semantics.
But If you really want/need to make C code compliant to C++ and vice versa use compiler switches instead of any cast.
Since the cast is with tendency declared as redundant or even harmful, I want to take a focus on these questions, which give good reasons why casting can be useful or even necessary:
- The cast can be non-beneficial when your code, respectively the type of the assigned pointer (and with that the type of the cast), changes, although this is in most cases unlikely. Then you would need to maintain/change all casts too and if you have a few thousand calls to memory-management functions in your code, this can really summarizing up and decrease the maintenance efficiency.
Summary:
Fact is, that the cast is redundant per the C standard (already since ANSI-C (C89/C90)) if the assigned pointer point to an object of fundamental alignment requirement (which includes the most of all objects).
You don't need to do the cast as the pointer is automatically aligned in this case:
"The order and contiguity of storage allocated by successive calls to the aligned_alloc, calloc, malloc, and realloc functions is unspecified. The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object with a fundamental alignment requirement and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated)."
Source: C18, §7.22.3/1
"A fundamental alignment is a valid alignment less than or equal to _Alignof (max_align_t). Fundamental alignments shall be supported by the implementation for objects of all storage durations. The alignment requirements of the following types shall be fundamental alignments:
— all atomic, qualified, or unqualified basic types;
— all atomic, qualified, or unqualified enumerated types;
— all atomic, qualified, or unqualified pointer types;
— all array types whose element type has a fundamental alignment requirement;57)
— all types specified in Clause 7 as complete object types;
— all structure or union types all of whose elements have types with fundamental alignment requirements and none of whose elements have an alignment specifier specifying an alignment that is not a fundamental alignment.
- As specified in 6.2.1, the later declaration might hide the prior declaration."
Source: C18, §6.2.8/2
However, if you allocate memory for an implementation-defined object of extended alignment requirement, the cast would be needed.
An extended alignment is represented by an alignment greater than _Alignof (max_align_t). It is implementation-defined whether any extended alignments are supported and the storage durations for which they are supported. A type having an extended alignment requirement is an over-aligned type.58)
Source. C18, §6.2.8/3
Everything else is a matter of the specific use case and one's own opinion.
Please be careful how you educate yourself.
I recommend you to read all of the answers made so far carefully first (as well as their comments which may point at a failure) and then build your own opinion if you or if you not cast the result of malloc() at a specific case.
Please note:
There is no right and wrong answer to that question. It is a matter of style and you yourself decide which way you choose (if you aren't forced to by education or job of course). Please be aware of that and don't let trick you.
Last note: I voted to lately close this question as opinion-based, which is indeed needed since years. If you got the close/reopen privilege I would like to invite you to do so, too.