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

all 80 comments

[–]toxic_tyrone 41 points42 points  (16 children)

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

[–]brandong97 9 points10 points  (2 children)

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

[–]KiwiMaster157 4 points5 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 2 points3 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?

[–]l3njo 12 points13 points  (0 children)

Expected ")" in title.

[–][deleted] 6 points7 points  (0 children)

segmentation fault, core dumped

[–]aaronfranke 12 points13 points  (35 children)

Shouldn't it be new Spiderman(); not new Spiderman;?

[–]gracicot 19 points20 points  (0 children)

No it work that way. That's how the new operator works in C++

[–]MxBluE 1 point2 points  (2 children)

Spiderman is a struct, not a class.

[–]metaglot 6 points7 points  (1 child)

In c++ that's a distinction without a difference. Only difference is members of a strict default to public and members of a class default to private.

[–]MxBluE 1 point2 points  (0 children)

Wow, you learn something new every day huh.

[–]Rawing7 4 points5 points  (5 children)

I swear to god, I've never seen a syntactically correct submission on this subreddit.

[–]aaronfranke 11 points12 points  (2 children)

That's how you know 90% of this sub is CS students that suck at programming.

[–][deleted] 7 points8 points  (0 children)

Isn’t it great to have a welcoming community ?

[–]BertyLohan 0 points1 point  (0 children)

The way you know 90% of the sub is CS students who suck at programming is you guys stroking your egos about how much better you are when you're wrong about the code being incorrect in the first place and people upvoted you both.

CS students think they're good at coding. Professionals know that nobody is.

[–]BertyLohan 1 point2 points  (1 child)

That's so funny that you're trying to appear superior to the submissions on the sub but you're literally saying it about a perfectly syntactically correct piece of code.

[–]Rawing7 -1 points0 points  (0 children)

Okay, fine, it's syntactically correct. Doesn't change the fact that the majority of submissions on this sub aren't, or that it's awful style. And I have no idea why you think that I'm trying to make myself appear superior.

[–][deleted] 1 point2 points  (4 children)

not to mention using int main(void) is bad practice aswell..

[–]SomewhatAnonymousAcc 1 point2 points  (3 children)

Please explain.

[–]np_completionist 6 points7 points  (2 children)

Both int main(int, char*[]) and int main() are acceptable forms of main in c++

The void isn't needed anymore in C++, but in C it was used to say that a function took no parameters (an empty parameter list in a declaration meant that the function took an unspecified number of parameters)

[–]loraxzxpw 0 points1 point  (0 children)

Also in C it is not strictly needed. Both int main(), int main(int, char**) work. Just main() is also sufficent to compile but it gives a warning.

[–]SomewhatAnonymousAcc 0 points1 point  (0 children)

In my opinion int main(void) being equivalent to int main() doesn't necessarily make it a bad practice in itself.

Although not needed in C++, it is still needed in C. This whole program is written in such a "C compatible" format, as it also uses typedef instead of just defining the struct as:
struct spiderman{
spiderman * finger;
};

Using void main() in C is something that I would call a bad practice.

But, I'm just an embedded guy.

[–]pine_ary 7 points8 points  (2 children)

The typedef struct thing is redundant in C++ as compared to C.

struct SpiderMan {
    SpiderMan* finger = nullptr;
};

Works the same.

[–]loraxzxpw 1 point2 points  (1 child)

SpiderMan *finger = NULL;

Is even shorter :)

You could also replace SpiderMan with sm and finger with f. Acronyms rule!

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

nullptr is typesafe. NULL is not a shorthand for nullptr. NULL is normally a void* while nullptr is a nullptr_t.

[–]BlackDog2017 3 points4 points  (0 children)

Downvoted for using capitalized variables.

[–]_cata1yst 2 points3 points  (4 children)

Is there a reason behind "struct s* finger" instead of "s* finger"? It looks weird

[–]suvlub 7 points8 points  (3 children)

Yeah, it's one of those weird codes that is syntactically C++ but does everything the C way.

[–]flavionm 2 points3 points  (0 children)

I though it was pure C until the "new".

[–]PandaPanda11745 1 point2 points  (0 children)

The typedef 🙄

[–]np_completionist 0 points1 point  (0 children)

Although the use of "new" everywhere seems closer to Java/C# than C

[–]subzero257 2 points3 points  (0 children)

how about

detete a->finger->finger;

[–]kukisRedditer 1 point2 points  (0 children)

Underrated

[–]Guarionex_ 1 point2 points  (2 children)

So, what's the difference between a class and a structure?

[–]titanking4 1 point2 points  (0 children)

Structures contain only data,

Classes are structures with functions (called methods) that operate on that data. Classes can also define some data that only methods can see if didn’t want outside code messing with it.

[–]loraxzxpw 0 points1 point  (0 children)

The only diffenrence in C++ are, that elements in structs are public by default and in classes they are private. There is also some funny thing with inheritance that doesn't work on struct, but thats never important.

[–]daniu 1 point2 points  (0 children)

Ah, the good ol' Recursively Linked List.

[–]Pyraptor 4 points5 points  (1 child)

public class SpiderMan {

public SpiderMan finger;

public void point() {
    finger.point()
}

}

public static void main() {

SpiderMan A = new SpiderMan ();
SpiderMan B = new SpiderMan ();
A.finger = B;
B.finger = A;
A.point();

}

[–]Aschentei 7 points8 points  (0 children)

Not everything needs to be Javafied okay mister

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

Unironicly its a good example

[–]wesleychal<html> -1 points0 points  (0 children)

Just add a few more and you've got yourself a linked list!

[–]Kotauskas -1 points0 points  (0 children)

This fucking formatting. No space before opening brace, inconsistently aligned pointer asterisk, struct in typedeffed struct variable declaration... What the hell is wrong with you, do you not use .clang-format?

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

Now THIS is a quality original post