Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we cast return value of malloc? [duplicate]

Tags:

c

malloc

Could someone explain to me why do some programmers use (char*) in front of the malloc? I know that it returns void but why do I want it to return just char memory? I'm sorry, I'm just a newbie in programming. Thank you

like image 380
user3012799 Avatar asked Nov 20 '13 11:11

user3012799


3 Answers

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 C1. 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:

  • Including the cast allows a program or function to compile as C++.
  • The cast allows for pre-1989 versions of malloc that originally returned a char *.
  • Casting can help the developer identify inconsistencies in type sizing should the destination pointer type change, particularly if the pointer is declared far from the malloc() call.

Disadvantages to casting:

  • Under the ANSI C standard, the cast is redundant.
  • Adding the cast may mask failure to include the header 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.
  • If the type of the pointer is changed, one must fix all code lines where malloc was called and cast (unless it was cast to a typedef).

1. Emphases are mine.

like image 194
haccks Avatar answered Oct 29 '22 09:10

haccks


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.

like image 43
Paul92 Avatar answered Oct 29 '22 08:10

Paul92


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.

like image 44
Thomas Padron-McCarthy Avatar answered Oct 29 '22 07:10

Thomas Padron-McCarthy