5

I have some C code with malloc statements in it that I want to merge with some C++ code.

I was wondering when and why is typecasting a call to malloc neccessary in C++?

For example:

char *str = (char*)malloc(strlen(argv[1]) * sizeof(char));
2

5 Answers 5

18

when and why is typecasting a call to malloc neccessary in C++?

Always when not assigning to a void *, since void * doesn't convert implicitly to other pointer types, the way it does in C. But the true answer is you shouldn't ever use malloc in C++ in the first place.


I am not suggesting you should use new instead of malloc. Modern C++ code should use new sparingly, or avoid it altogether if possible. You should hide all use of new or use non-primitive types (like std::vector mentioned by Xeo). I'm not really qualified to give advice in this direction due to my limited experience but this article along with searching for "C++ avoid new" should help. Then you'll want to look into:

8
  • "Ever" is so strong..
    – Inisheer
    Feb 27, 2013 at 18:05
  • 4
    @Inisheer It is, but by removing it I would feel like weaseling out.
    – cnicutar
    Feb 27, 2013 at 18:07
  • 3
    @Inisheer: It's not only strong, it's the correct thing to do.
    – Xeo
    Feb 27, 2013 at 18:09
  • Btw, maybe you should give some advice as what to use instead (hint: std::vector and friends).
    – Xeo
    Feb 27, 2013 at 18:12
  • Why is using malloc in C++ so bad? This was originally intended to be a purely C program, until I needed to merge in some C++ code. It would be a hassle to convert all those mallocs to news Feb 27, 2013 at 18:13
2

Compile your C library. Compile your C++ library. Make them play nice in whatever "main" program that uses them. Point is if your maintaining a mixed code base, you probably want to isolate the pure C stuff from the C++ stuff. Otherwise your C stuff turns into C++ stuff that only looks like C.

1

First, in almost all circumstances just don't use malloc in a C++ program, but prefer new instead because it will make sure that constructors are called when needed, etc.

However if for legacy reasons you're trying to avoid as much rewrite as possible - you'll need to cast any malloc call that's not assigned to a void* pointer.

1

If you can change that code its probably better to use new instead so it would look like this

char* str = new char;

this means you don't need to do any casting like the C way and you don't need to specify how large the memory you need. Also if this was an object like a std::string then you WILL not call the constructor when you use malloc, this merely reserves the memory for use with the pointer str so best always use new with C++ if you can also when you reclaim memory always use the appropriate way, if you new then you delete and if you malloc you free. If you use free on memory that has been new'd then you won't call that objects destructor.

0

malloc always returns a void* so you need to cast everything (because C++ has stronger type checking than C and don't this automatically)

When I am using C, I also cast everything, for code clarity.

Also, feel free to keep using malloc() in C++, it is there for a good reason.

Converting all the C code to C++ by rewriting every single malloc() to new is very prone to introduce lots of errors in your code, unless you have the time to keep reading the code you are merging to find every single instance of malloc(), free(), calloc(), etc... on it.

Just don't mix malloc() with delete or new with free() or things break.

7
  • 1
    My approach is not using malloc
    – 111111
    Feb 27, 2013 at 18:01
  • "When I am using C, I also cast everything, for code clarity." - Actually, you cast it for ambiguity. It is an error to cast the return value of malloc() in C.
    – user529758
    Feb 27, 2013 at 18:02
  • 4
    @speeder Well, about the new part... the really good C++ doesn't use new either
    – cnicutar
    Feb 27, 2013 at 18:03
  • I have been doing it this way for years, from before I learned C++, because seriously, not casting to see if you get <stlib.h> is just silly. And unless you have crazy type names it won't make your code much longer.
    – speeder
    Feb 27, 2013 at 18:06
  • @speeder But come on, casts are ugly and redundant. I rather read int *ptr = malloc(size); than int *ptr = (int *)malloc(size);.
    – user529758
    Feb 27, 2013 at 18:07

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