Why do we cast return value of malloc?
This question already has an answer here:
No need to cast return value of malloc
as its return type is void*
.
Can someone explain why do some programmers use (*char) in front of the malloc?
They are doing wrong (most probably) by casting it (in good programmers opinion).
As wiki says:
malloc
returns a void pointer ( void *
), which indicates that it is a pointer to a region of unknown data type. The use of casting is required in C++ due to the strong type system, whereas this is not the case in C 1. The lack of a specific pointer type returned from malloc
is type-unsafe behavior according to some programmers: malloc
allocates based on byte count but not on type. This is different from the C++ new
operator that returns a pointer whose type relies on the operand. One may "cast" this pointer to a specific type:
int *ptr;
ptr = malloc(10 * sizeof (*ptr)); /* without a cast */
ptr = (int *)malloc(10 * sizeof (*ptr)); /* with a cast */
ptr = reinterpret_cast<int *>(malloc(10 * sizeof (*ptr))); /* with a cast, for C++ */
There are advantages and disadvantages to performing such a cast .
Advantages to casting:
malloc
that originally returned a char *
. malloc()
call. Disadvantages to casting:
stdlib.h
, in which the prototype for malloc
is found . In the absence of a prototype for malloc
, the standard requires that the C compiler assume malloc
returns an int. If there is no cast, a warning is issued when this integer is assigned to the pointer; however, with the cast, this warning is not produced, hiding a bug. On certain architectures and data models (such as LP64 on 64-bit systems, where long and pointers are 64-bit and int is 32-bit), this error can actually result in undefined behavior, as the implicitly declared malloc
returns a 32-bit value whereas the actually defined function returns a 64-bit value. Depending on calling conventions and memory layout, this may result in stack smashing. This issue is less likely to go unnoticed in modern compilers, as they uniformly produce warnings that an undeclared function has been used, so a warning will still appear. For example, GCC's default behavior is to show a warning that reads "incompatible implicit declaration of built-in function" regardless of whether the cast is present or not. malloc
was called and cast (unless it was cast to a typedef
). 1. Emphases are mine.
As the return type of malloc is void*, when you assign the result to a pointer, it is converted implicitly to the new type. So, there is no need for explicit casting. Actually, using an explicit cast is discouraged, as described here.
malloc returns void* , which is a generic pointer that can point to any type of data. The (char*) is an explicit type conversion, converting the pointer returned by malloc from a pointer to anything, to a pointer to char. This is unnecessary in C, since it is done implicitly, and it is actually recommended not to do this, since it can hide some errors.
If you need the code to compile as C++ too, and not just as C, you will however need the explicit conversion, since C++ doesn't perform the implicit conversion.
链接地址: http://www.djcxy.com/p/28420.html上一篇: malloc需要typecast吗?
下一篇: 为什么我们要投入malloc的返回值?