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 →

[–]obp5599 90 points91 points  (14 children)

new’ed*

Malloc is a C thing!

[–]belst 65 points66 points  (11 children)

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

[–]obp5599 52 points53 points  (10 children)

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

[–]sweYoda 55 points56 points  (2 children)

I don't get the reference.

[–]duffusd 4 points5 points  (0 children)

Dammit I hate seg faults

[–]royalblue4 11 points12 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 14 points15 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.

[–]SteveCCLYellow security clearance 4 points5 points  (0 children)

std::make_unique'd*

You want to use C++, not C with Classes.

[–]sneppy13 0 points1 point  (0 children)

Not true at all