Hi all:

The function malloc returns a void* pointer, and so I need to explicitly use type cast e.g.: (struct*)malloc(sizeof(Message_from_server));
I am wondering if it is a legal expression or at least, I should give it a tag name like (struct*message_from_server)malloc(sizeof(Message_from_server));

Thanks

Dani AI

Generated

raised a common C vs C++ question about casting the result of malloc. The replies from and are on point and correctly flagged that malloc is not the idiomatic tool in C++. Below are concise, practical clarifications and safe patterns to follow.

In C: do not cast the void* returned by malloc. Casting can mask a missing prototype or forgotten #include <stdlib.h>, which historically produced subtle bugs. Compile with warnings enabled (for example -Wall -Wextra) so a missing header is caught. Use the safer size idiom to avoid mismatches, for example ptr = malloc(sizeof *ptr);. If you prefer not to write struct everywhere, use a typedef for the struct name.

In C++: void* does not convert implicitly, so a cast would be required if you use malloc. Prefer instead C++ allocation and RAII: use new or, better, smart pointers and containers (for example auto p = std::make_unique<YourType>();). malloc does not call constructors or destructors, so it is unsafe for non-POD/class types unless you plan to use placement new and manage lifetimes yourself. If interoperating with a C API and you must use malloc, keep allocation/deallocation paired and prefer static_cast for converting void* to an object pointer rather than a C-style cast.

Practical cautions: never mix new/delete with malloc/free; free memory with the matching deallocation function. For authoritative references see the C malloc page and the C++ memory/new and smart pointer pages on cppreference: malloc — cppreference, operator new — cppreference, unique_ptr — cppreference.

Recommended Answers

All 5 Replies

No you cannot do that and you shouldn't even typecast malloc.

If you are compiling a C program as C++ (with .cpp extension instead of .c extention), then you will have to typecase malloc because the compiler is compiling it as a c++ program, and c++ requires the typecase. If, on the otherhand, the file is *.c extension, then no typecase is necessary (permissible but not necessary).

The correct typecast is (see place ment of the asterisk)

// C example
(struct message_from_server*)malloc(sizeof(struct Message_from_server)); 

// C++ example (keyword struct not necessary)
(message_from_server*)malloc(sizeof(Message_from_server));
Member Avatar for Member #46692

Question?

Should you be using malloc in c++, isn't it frowned upon?

Question?

Should you be using malloc in c++, isn't it frowned upon?

Yes -- but it can be much more serious problem that just "frowned upon" because malloc() does not call class constructors or destructors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.