13

Please note this question is not about malloc in C or malloc vs new/smart pointers in C++.

If I use malloc in C++, what kind of cast should I use? The following all work.

int *a = (int *)malloc(sizeof (int));
int *b = static_cast<int *>(malloc(sizeof (int)));
int *c = reinterpret_cast<int *>(malloc(sizeof (int)));

Live example: http://ideone.com/lzfxcm

I prefer to use C++ style casts in my code as much as possible and I want to adopt safe coding habits. Please advise with this in mind.

Thank you.

11
  • for malloc, a C style cast is no less safe than reintprerpt_cast.
    – tenfour
    Sep 25, 2013 at 12:28
  • 4
    to be safe you should use new and not malloc
    – AndersK
    Sep 25, 2013 at 12:29
  • 3
    @claptrap I specifically stated this question is NOT about malloc vs new. Sometimes malloc is used in C++. It happens.
    – Neil Kirk
    Sep 25, 2013 at 12:30
  • (int *) does the job right, less to type, easy to read.
    – dmitri
    Sep 25, 2013 at 12:32
  • 3
    @NeilKirk: "Sometimes malloc is used in C++" When, exactly? Give at least one scenario.
    – SigTerm
    Sep 25, 2013 at 12:46

4 Answers 4

13

Since malloc returns a pointer to void, there is no reason to use a C++ - style cast on the pointer: you get a chunk of raw memory, with no structure behind it, so the only thing your can tell the compiler by adding a cast is that you plan to use this memory for data of a particular kind. Compiler must agree with you on that, because it has no additional information to double-check your decision. Neither static_cast<T> nor the reinterpret_cast<T> offer a particular advantage over the C-style cast, and the C-style cast is shorter.

From the personal perspective, I looked at a lot of C++ code, but I've never seen a C++ - style cast used with malloc, only the C-style.

1
  • 3
    After all malloc is C! C++ has new. Sep 16, 2016 at 9:21
13

I would prefer to use static_cast, as what you are doing is converting a void* to a pointer to some other object type, which is well defined in C++.

C-style cast should not be used in C++, as the compiler will perform no type checking whatsoever - by using a C-style cast, you lose all type safety. The idea is to restrict your casts to only what is needed and no more.

reinterpret_cast has a different purpose - to reinterpret the bits of an object as some other type. void is not an object, so this clearly doesn't apply to void*/malloc.

5
  • (+1) I was about to post a similar answer :-) This is a relevant link on SO. Sep 25, 2013 at 12:38
  • 1
    C-style cast should not be used in C++ There should be better justifications than that, or there are none?
    – dmitri
    Sep 25, 2013 at 12:39
  • 2
    @dmitri: I've updated my answer. Besides, I am a C++ purist.
    – Jesse Good
    Sep 25, 2013 at 12:43
  • Personal preferences are OK but a forum like this is more about technical merits, don't you think? malloc returns void*, that doesn't carry type information so compiler can't do much of type checking here. Am I missing something?
    – dmitri
    Sep 25, 2013 at 12:49
  • @dmitri: Yes, the compiler cannot do much type checking in this case, but in general you should use the strictest cast possible, which means static_cast would be preferred in this situation.
    – Jesse Good
    Sep 25, 2013 at 12:59
2

Sticking to the principle to always use the "least-violent" cast, I would recommend static_cast.

However, even better would be a wrapper function like

template <typename T>
T* mnew(std::size_t count = 1)
{
    return static_cast<T*>(malloc(sizeof(T) * count));
}
2
  • Why would you use this function, and not just operator new?
    – tenfour
    Sep 25, 2013 at 12:38
  • 5
    Because it (1) returns NULL instead of calling the new-handler and/or throwing std::bad_alloc in case of errors, (2) the returned pointer must be freed with free() instead of delete[] or delete and (3) it does not call any constructors.
    – Oberon
    Sep 25, 2013 at 12:40
0

malloc returns void* pointer which you can cast it to any desired pointer using C style casts but C++ introduced 4 new cast operator to overcome one disadvantage assoicated with C style casts and that is they can-not be easily detected using IDE or any other tools like grep.

Conside your example

int *a = (int *)malloc(sizeof (int));
int *b = static_cast<int *>(malloc(sizeof (int)));
int *c = reinterpret_cast<int *>(malloc(sizeof (int)));

Here you can easily search for all instances of static_cast or reinterpret_cast but had it been c style cast then you got no other way but to simply look or dig into code and spent sleepless nights. Believe me casting bugs are very hard and they undermined strong typing guarantee offered by the C and C++ style languages. But their are some genuine cases where in such cast is needed.

Hence C++ allows C style casting but it intoduce reinterpret_cast operator which does nothing but C style cast operation.

http://www.stroustrup.com/bs_faq2.html#void-ptr

Not the answer you're looking for? Browse other questions tagged or ask your own question.