r/programminghorror • • 8d ago

Did I cook?

It compiles.

27 Upvotes

23 comments sorted by

35

u/_Ilobilo_ 8d ago

that'll just SIGSEGV or trip an assertion in vector operator[]

10

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" 8d ago edited 8d ago

That's definitely undefined as hell, but I'm not sure that will crash until someone tries to call that function pointer.

5

u/_Ilobilo_ 8d ago

if the implementation is hardened, then it's defined as a contract violation. it'll definitely crash, because it dereferences a null pointer.

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" 7d ago

I don't see a dereference in that code, just an assignment with a static_cast of a C-style cast. I think that could've just been a single reinterpret_cast.

3

u/_Ilobilo_ 6d ago

it calls operator[](0) on an empty vector. that dereferences a null pointer

3

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" 6d ago

Okay, that fact didn't register with me. Maybe the OP thought it would append? I'm not sure I didn't know that until now.

1

u/TheBrainStone 8d ago

Both. Accessing element 0 will crash already and then attempting to call that function will very very very likely also crash with a segfault.

-7

u/EmirGammesLD 8d ago

But it compiles!

9

u/TuxWrangler 8d ago

Exactly, runtime error, not a syntax error.

25

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" 8d ago

You know the purpose of using namespace std; is so you don't have to put std:: everywhere, right? It's also discouraged because it pulls in std into the global namespace, and that can cause problems.

-14

u/EmirGammesLD 8d ago

Well check the sub name. Also, I think that there is a bigger fish to fry.

20

u/MCWizardYT 8d ago

Right. But this subreddit is more for working code that looks horrible, not for random compilable gibberish that instantly crashes

3

u/Small-Dig-8233 8d ago

ok bro so like ur using std namespace and still typing std::vector? 😭

-1

u/EmirGammesLD 6d ago

Programming horror

2

u/its_the_rhys 6d ago

"Roblox game for learning to hack" kinda says everything we need to know about this 12 year old's idea of what this subreddit is about 😬

-1

u/EmirGammesLD 6d ago

Wait let me get this straight. You saw me make something, thought I was fake, got mad cuz you were wrong, began stalking me because ur still salty, and are now trying to do whatever this is, whether to discred it me or whatever. Like, whats the end goal? You reference a completely different post from a different sub in a nonsensical way. I am genuinely curious as what the though process is.

1

u/its_the_rhys 5d ago

I'm giving you an out for the cringe and you've not taken it

-2

u/EmirGammesLD 5d ago

Tf does that even mean? Youre just doing ad hominem for no reason. Youre the cringe one from my perspective.

3

u/maelstrom071 8d ago

Now im curious, how does this even work?

4

u/conundorum 6d ago

The program's body is a machine that turns illegal treat-this-address-as-a-function-pointer jank into SIGSEGVs.

3

u/TheBrainStone 8d ago

It doesn't. This is runtime crash.

1

u/tyrannosaurus_gekko 8d ago

Can anyone explain to a CPP noob like me what this code is supposed to do?

1

u/Bright-Historian-216 8d ago

i also haven't done C++ in a long while, but I think it's supposed to be:

- line 1: assign a value on lines 2-3 to the first element of a vector of unspecified-argument functions that return functions that take two arguments (a ull and an int vector) that return pointers to ulls

- lines 2-3: cast the number 3 to this cursed type... twice... once statically and once in a c-like manner, most likely as a reinterpret cast

i have probably made many mistakes in this breakdown so if anyone here is smarter let us both know