How should I give the result of malloc in C ++?

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

If I use malloc in C ++, what should I use? The following works.

 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 in my code as much as possible, and I want to use safe coding habits. Please report this.

Thanks.

+6
source share
4 answers

Since malloc returns a pointer to void , there is no reason to use the C ++ style in the pointer: you get a piece of raw memory, with no structure behind it, so the only thing you can do is tell the compiler, adding that you plan to use this memory for data of a certain kind. The compiler must agree with you on this because it does not have additional information to double check your solution. Neither static_cast<T> nor reinterpret_cast<T> have a particular advantage over C-style casting, and C-casting is shorter.

In a personal perspective, I looked at a lot of C ++ code, but I never saw the C ++ butt used with malloc , only C-style.

+14
source

I would rather use static_cast , because what you are doing is converting void* to a pointer to some other type of object that is well defined in C ++.

C-style casting should not be used in C ++, since the compiler will not perform any type checks - if you use the C-style cast, you will lose all type safety. The idea is to limit your throws to just what you need, and no more.

reinterpret_cast has another purpose - to re-interpret the bits of the object as some other type. void not an object, so this clearly does not apply to void* / malloc .

+6
source

Adhering to the principle to always use the "least violent", I would recommend static_cast .

However, it would be even better to work with a wrapper, for example

 template <typename T> T* mnew(std::size_t count = 1) { return static_cast<T*>(malloc(sizeof(T) * count)); } 
+2
source

malloc returns a void * pointer, which you can point to any desired pointer using C-style, but C ++ introduced 4 new translation operators to overcome one drawback associated with C-style casts, and that they cannot be easily detected with using an IDE or any other tools like grep.

Link 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 find all instances of static_cast or reinterpret_cast, but if it were c style cast, then you would have no choice but to just watch or delve into the code and spend sleepless nights. Believe me, error casting is very difficult, and they undermined the strong typing guarantee offered by the C and C ++ languages. But these are some genuine cases when such a cast is required.

Consequently, C ++ allows C-style, but it insulates the reinterpret_cast statement, which does nothing but the operation with the effect of style C.

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

0
source

Source: https://habr.com/ru/post/954596/