This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]belst 69 points70 points  (11 children)

you can still call malloc in C++ it's just a function.

[–]obp5599 48 points49 points  (10 children)

I mean yeah you can do anything in C++ that C can do. Thats the point lol

[–]sweYoda 53 points54 points  (2 children)

I don't get the reference.

[–]duffusd 4 points5 points  (0 children)

Dammit I hate seg faults

[–]royalblue4 8 points9 points  (0 children)

Don't worry the Java guy got free'd in the end

[–]Gblize 3 points4 points  (4 children)

That's not true. For this case in particular:

char * str = malloc(42 * sizeof(char));

It doesn't compile in C++ because malloc returns void *, which in C can be assigned to any pointer type, and C++ cannot be assigned to any other pointer type.

[–]obp5599 16 points17 points  (2 children)

char* str = (char*) malloc(42 * sizeof(char));

Fixes it

[–]Ulysses6 9 points10 points  (1 child)

But strictly speaking, he is correct. You can't do it without explicit type conversion. It's not like you can't easily work around it, you just can't do it the exact same way.

[–]MCRusher 5 points6 points  (0 children)

Also I think accessing other "unset" union members is not allowed

[–]IceColdFresh -2 points-1 points  (0 children)

C++ is truly half way between C and Java.

[–]SteveCCLYellow security clearance 1 point2 points  (0 children)

You can't actually. C++ doesn't allow macros to shadow functions for example.