1

I was wondering what is the difference between the two lines.

char *ptr;
ptr = malloc(10);

and this one.

char *ptr;
ptr = (char *) malloc(10);

Are you like casting the memory returned by malloc?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 3
    Yes, malloc returns a void*, however casting is usually not recommended as the compiler would warn for errors if you don't cast. – jpw Aug 07 '14 at 08:47
  • 4
    Yes the second example is casting the result, [and you should not do it!](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Aug 07 '14 at 08:48
  • No need to cast for C, http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – cshu Aug 07 '14 at 08:50

2 Answers2

1

Yes, in the second example you are casting the return value. However, you should not do that!

For detailed information go to this link: Do I cast the result of malloc?

Community
  • 1
  • 1
ani627
  • 5,578
  • 8
  • 39
  • 45
  • Thank you for that link! But out of curiosity, those two line do the same thing right? –  Aug 07 '14 at 08:59
  • Yes they do the same thing. If you want to compile your code with C++ compiler you need to cast it. – ani627 Aug 07 '14 at 09:07
  • @Ani Very silly answer (the reference) of low-qualified programmer that has no his own brain and does not uses casting only by the reason that in C the type of right-hand expression is void *. Never follow advices of such idiots without brain. – Vlad from Moscow Aug 07 '14 at 09:22
0

In fact they are equivalent but the second one is preferable. moreover the first record will not be compiled in C++. So even taking in account this reason it is better to use the second record. Take the best from C++.

Why is the second record preverable. For example can you say what type of memory there is allocated in the following statement

p = malloc( 10 * sizeof( *p ) );

This statement says nothing to the reader of the code what type of memory is allocated and he has to list the listing to find the definition of p that to say what type of memory is allocated. But even if he will find the definition of variable p he will not be sure that this statement with malloc is correct because he does not know what is the actual type of expression

malloc( 10 * sizeof( *p ) );

As the expression has type void * then it can be assigned to a pointer of any type. This is a source of some kind of errors.

For example it may be that the intention of the author of the code was to allocate an array of characters

( char * )malloc( 10 * sizeof( *p ) );

but you can not say this if you will see only

malloc( 10 * sizeof( *p ) );

But are you sure that there is indeed allocated an array of characters? Maybe the author of the code meant

( char ( * )[2] )malloc( 10 * sizeof( *p ) );

or even

( char ( * )[5] )malloc( 10 * sizeof( *p ) );

or even

( short * )malloc( 10 * sizeof( *p ) );

and so on.

Every code snippet should have as much information that it was clear without any comments what the code snippet is written for.

When the compilers were not so smart as now then they did not report whether the function malloc was defined in the code that is whether the corresponding header was included. In this case by default the compiler considered the function as having the return type int. And if you did not use the casting then the compiler could report an error. But now there is no any sense in such a trick. The compilers report if a function was not declared. Try do the all that the code will be more readable and clear.

YvesHendseth
  • 1,149
  • 1
  • 10
  • 27
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • May i ask what this line means? ( char ( * )[2] )malloc( 10 * sizeof( *p ) ) –  Aug 07 '14 at 09:17
  • @Patrick A good equestion! it confirms that you should always use casting that to know what type of memory is allocated. This record means that there ia allocated a two dimensional array that has two columns. – Vlad from Moscow Aug 07 '14 at 09:19