r/programminghorror • • 4d ago

Did I cook?

It compiles.

22 Upvotes

23 comments sorted by

33

u/_Ilobilo_ 4d ago

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

9

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 4d ago edited 4d 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_ 4d 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” 3d 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_ 2d 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” 2d 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 3d 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 4d ago

But it compiles!

9

u/TuxWrangler 4d ago

Exactly, runtime error, not a syntax error.

22

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 4d 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 4d ago

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

17

u/MCWizardYT 3d 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 3d ago

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

-1

u/EmirGammesLD 2d ago

Programming horror

1

u/its_the_rhys 1d 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 1d 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 15h ago

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

-1

u/EmirGammesLD 15h ago

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

3

u/maelstrom071 3d ago

Now im curious, how does this even work?

4

u/conundorum 2d ago

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

3

u/TheBrainStone 3d ago

It doesn't. This is runtime crash.

1

u/tyrannosaurus_gekko 3d ago

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

1

u/Bright-Historian-216 3d 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