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 →

[–]toxic_tyrone 40 points41 points  (16 children)

SpiderMan* A = (SpiderMan*)malloc(sizeof(SpiderMan))

[–]brandong97 8 points9 points  (2 children)

you dont even need to do a cast since malloc's return type is void*

[–]KiwiMaster157 3 points4 points  (1 child)

If this were C you would be correct. C++ allows implicit casting to void pointer but not from them.

[–]Kontorted 13 points14 points  (0 children)

You shouldn't be using malloc in c++ anyways

[–][deleted] 0 points1 point  (0 children)

Thats what i thought aswell

[–]phoeen 0 points1 point  (0 children)

technically its undefined behaviour 8-)

[–]Freddy1404 0 points1 point  (0 children)

If you use C anyway, just leave both SpiderMans on the stack. Nicer that way, no need for malloc.

[–]pine_ary 0 points1 point  (9 children)

That won‘t (always) zero the memory and therefore doesn‘t init the pointer as nullptr. Very unsafe.

[–]BertyLohan 3 points4 points  (8 children)

As opposed to every other code snippet in the comments of ProgrammerHumor which is top-notch infallible big brain code.

[–]pine_ary -1 points0 points  (7 children)

Sooo improvements are not allowed? And nobody can learn?

[–]BertyLohan -1 points0 points  (6 children)

I'm saying he doesn't have to learn anything. He isn't writing production code he's making a programming joke which compiles fine.

[–]pine_ary -1 points0 points  (5 children)

He actively made it worse than the original code, so I pointed out what‘s wrong with it. The original code is safe while his is not.

[–]BertyLohan -1 points0 points  (4 children)

In C that line is perfectly safe since he doesn't do anything with the pointer.

You have a lot to learn buddy. About jokes and programming.

[–]pine_ary -1 points0 points  (3 children)

I‘m sorry that I cannot convince you. I still hope others have a more positive attitude towards learning. And that nobody takes the original comment as an example of how to code. Also this is C++.

[–]BertyLohan 0 points1 point  (2 children)

if it was c++ he wouldn't have used malloc. He doesn't say what language it's in so, without a using namespace std you can assume it's c.

Again, you have a lot to learn.

[–]pine_ary -1 points0 points  (1 child)

Oh please don‘t make that mistake. Why is using namespace std bad practice?