0

I'm not sure the proper way to handle the casting of types when using malloc. I'm coming from Objective C where the following is perfectly legal:

ALuint * sources;
sources=malloc(sizeof(ALuint)*32);

However, in C++ the compiler says "Assigning to ALuint * from incompatible type void *".

I get that the memory returned from malloc is not casted as my particular type, and I get that C++ is strict with types.

Now, I could do this:

 sources=(ALuint*)malloc(sizeof(ALuint)*32);

But I have read much wiser coders than myself say never to cast in such a manner. Why not? And if not, when or how is the best time or method to make this work?

johnbakers
  • 24,158
  • 24
  • 130
  • 258
  • 3
    Simple answer: Don't use `malloc`. – chris Feb 02 '13 at 04:56
  • Unless you're target pointer is `void *`, I can't think of a time you *don't* need the cast. See @chris' answer for the proper approach =P – WhozCraig Feb 02 '13 at 04:57
  • 1
    [Here's a question](http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-and-reinterpret-cast-be-used) for when to use which C++ casts. – chris Feb 02 '13 at 04:58
  • @chris The accepted answer in that question is outstanding. Added to my bookmark list. – WhozCraig Feb 02 '13 at 04:59

1 Answers1

3
  1. Avoid using malloc in C++.
  2. If you must #1, You must cast the return type of malloc for your code to compile.
  3. Only exception to #2 is if your target pointer is a void pointer.
  4. Not casting the return of malloc is (debatebly)considered as good practice in C.

So, In C++ a cast is necessary. The type of cast to use is also important though. It should definitely not be the c-style cast. The above links were demonstration of need of cast only.


Now, I could do this:

sources=(ALuint*)malloc(sizeof(ALuint)*32);

But I have read much wiser coders than myself say never to cast in such a manner. Why not?


Because in C++ a c-style cast is defined by the standard to map to C++ cast in following order. The first one to succeed will be used:

  1. const_cast
  2. static_cast
  3. static_cast, then const_cast
  4. reinterpret_cast
  5. reinterpret_cast, then const_cast

As you see a c-styled cast might in fact result in reinterpret_cast which is potentially dangerous as it can cast between incompatible pointer types. So if you must use malloc use:

sources=static_cast<ALuint*>malloc(sizeof(ALuint)*32);
Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Someone definitely thinks this is wrong enough for a downvote, So please enlighten the reasoning. And please do so only if the reasoning is technical. – Alok Save Feb 02 '13 at 05:01
  • Forgive my ignorance, but if you are allocating an array of `ALuint`s (or any other such types) on the heap, for which you need a pointer, how else to do it besides `malloc`? Or should I just use `new` in this case? – johnbakers Feb 02 '13 at 05:01
  • You should use new in that case @SebbyJohanns. – Aniket Inge Feb 02 '13 at 05:03
  • @SebbyJohanns: Neither. `std::vector v(n);` – Lightness Races in Orbit Feb 02 '13 at 05:03
  • @SebbyJohanns: What Lightness says. Don't use `malloc` or `new` for that matter unless you really have to. – Alok Save Feb 02 '13 at 05:04
  • 2
    I wish folks wouldn't always circumvent questions on here by simply pointing out that `vector` exists. It doesn't really answer the question. I have specific reasons, unrelated to this question, why I am not using `vector`. – johnbakers Feb 02 '13 at 05:05
  • 2
    @SebbyJohanns, If you tag it C++, but can't use parts of core C++, it at least deserves a mention. Otherwise, it's the obvious solution. `new` is generally preferred to `malloc` in C++, though. – chris Feb 02 '13 at 05:07
  • @SebbyJohanns: You have not let known of your *Specific reasons* or your inability to use `std::vector` anywhere in the Q or in the comments.`std::vector` is the best way to go about it and its a natural answer, You should be grateful to get wise answers not answers which just solve problems. – Alok Save Feb 02 '13 at 05:07
  • http://stackoverflow.com/questions/655065/when-should-i-use-the-new-keyword-in-c see this for when to and not to use `new` – Aniket Inge Feb 02 '13 at 05:10