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));
user2052561
  • 1,339
  • 2
  • 15
  • 18

5 Answers5

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:

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • "Ever" is so strong.. – Inisheer Feb 27 '13 at 18:05
  • 4
    @Inisheer It is, but by removing it I would feel like weaseling out. – cnicutar Feb 27 '13 at 18:07
  • 3
    @Inisheer: It's not only strong, it's the correct thing to do. – Xeo Feb 27 '13 at 18:09
  • Btw, maybe you should give some advice as what to use *instead* (hint: `std::vector` and friends). – Xeo Feb 27 '13 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 `malloc`s to `new`s – user2052561 Feb 27 '13 at 18:13
  • It's bad because it doesn't construct objects. And also because it shares every bad that `new` has, which are *MANY*. – Puppy Feb 27 '13 at 18:19
  • @Xeo, DeadMG You're probably better suited for explaining how one should avoid `new`. – cnicutar Feb 27 '13 at 18:20
  • @cnicutar some people would argue against you, but I agree. [see question here](http://stackoverflow.com/questions/15018011/building-a-mixed-c-c-code/15018044) – Josh Petitt Feb 27 '13 at 18:20
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.

Josh Petitt
  • 9,371
  • 12
  • 56
  • 104
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.

Mark B
  • 95,107
  • 10
  • 109
  • 188
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.

David Tr
  • 151
  • 2
  • 9
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.

speeder
  • 6,197
  • 5
  • 34
  • 51
  • 1
    My approach is not using malloc – 111111 Feb 27 '13 at 18:01
  • "When I am using C, I also cast everything, for code clarity." - Actually, [you cast it for ambiguity](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). It is an **error** to cast the return value of `malloc()` in C. –  Feb 27 '13 at 18:02
  • 4
    @speeder Well, about the `new` part... the really good C++ doesn't use `new` either – cnicutar Feb 27 '13 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 is just silly. And unless you have crazy type names it won't make your code much longer. – speeder Feb 27 '13 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);`. –  Feb 27 '13 at 18:07
  • `int *ptr = (int *)malloc(sizeof(int));` might look redundant, but what if you end with `avarboPlex = malloc(MAXSIZEOFBO);` ? – speeder Feb 27 '13 at 18:09
  • That's the sign you should change language to C++ – Bartek Banachewicz Feb 27 '13 at 18:42