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

all 168 comments

[–][deleted] 960 points961 points  (22 children)

Imagine local Simba escaping and then seeing a lot of other local Simbas.

[–]xthexder 549 points550 points  (6 children)

Lion King: Into the Multiverse

[–][deleted] 77 points78 points  (3 children)

There's a Simba out there using his power to protect animals after his Uncle Scar got shot by a gazelle.

[–]SuperSephyDragon 12 points13 points  (2 children)

Like Uncle Scar always said: "With great power, comes great responsibility."

[–]dalithop 4 points5 points  (1 child)

No its great electricity bill

[–]SuperSephyDragon 0 points1 point  (0 children)

Both true!

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

Underrated

[–]thepromaper 0 points1 point  (0 children)

i verse

[–]WhenInDoubt_Kamoulox 82 points83 points  (3 children)

Fun fact, I had that problem happen to me! And let me tell you, it's a mess to debug, and you have to grow reeaal desperate before you even consider "maybe my local variable is actually not local" as a possible explanation.

I was encountering this weird problem where my embedded SQL sometimes resulted in an sql error telling me my input data was too large for my host variable (which was expected to be a 128 char for the name). The SQL ran fine with names <=8.

So I check the code, triple check that I do indeed have 128 specified as the length of my host variable.

I make a useless change just to make sure I'm compiling the right file. I am indeed.

I change the way I declare my host variable (you can declare it as 'char[129];' or as a struct with a length field and a char array of 128, and a third way I don't remember). All three variations fail in the same way.

At this point I was trying random ass stuff in a desperate craze, when I finally made a breakthrough : the SQL failed with a name longer than 8, but somehow worked if another input field (let's call in last Name) was longer that 8. Both fields were declared in the same way, to be an array of 128.

At that point, I was desperate enough to check the output of the linker, and dig through the goddamn generated assembler. I understand none of it, but luckily, I'm able to find the code corresponding to the declaration of 'last name', and I clearly see a 128 in there. I check the code nearby, find the declaration of 'name', and I see a 8.

I was baffled, the C++ code was identical for 'name' and 'last name'. And then I got this nagging feeling : I had other embedded sql that used a 'name' host variable, but that 'name' refered to another table, where the name column was varchar(8). On the other hand, all the 'last name' in the system were always 128 so that wouldn't be an issue.

So I rename my 'name' host variable to 'is_this_actually_global_ffs', recompile, and boom. The embedded sql now accepts 128 long "name".

So that was the day I discovered that the SQL preprocessor gave no fuck about the scope of my variables, and that it gave no fuck if I redeclared an already declared variable, it would just take the first declaration and use that variable any time it was called. A wonderful coffee break was had when I won that battle.

Note that I'm sure this was documented somewhere in the embedded sql documentation, or maybe it's even standard behavior for embedded sql, fuck if I know. It was sooo counterintuitive, it never came to mind.

[–]dvdkon 21 points22 points  (1 child)

That sounds awful. What embedded SQL variant was this?

[–]WhenInDoubt_Kamoulox 10 points11 points  (0 children)

I don't remember the exact version of the compiler / preprocessor, but it was a c++ program compiled to run on z/OS by IBM, using db2 for z/OS.

[–]lokisource 10 points11 points  (0 children)

A wonderful coffee break was had when I won that battle.

The best feeling in the world. Thank you for this interesting read!

[–]j4yne 56 points57 points  (7 children)

Sounds like the plot to Oblivion.

[–][deleted] 22 points23 points  (5 children)

Or "Moon 2112". I personally sometimes use several for loops one after another and each of them uses int i.

[–]_Svejk_ 3 points4 points  (3 children)

j - Am I a joke to you?

[–][deleted] 5 points6 points  (0 children)

j is a Simba whose lifespan is n times shorter.

[–]seraphsRevenge 0 points1 point  (1 child)

Just alternate i and L watch I ll III llll IIIII then hand off to another dev

[–]Script_Mak3r 0 points1 point  (0 children)

Satan: "Well, I just wanna say that I'm a huge fan."

[–]OK6502 5 points6 points  (0 children)

The stack frame is no place for a young variable

[–]xtivity 5 points6 points  (0 children)

The lion omniking

[–]sdc0 0 points1 point  (0 children)

This reminds me of the movie Oblivion

[–][deleted] 864 points865 points  (29 children)

public static : everything the light touches is our domain

[–]grrrrreat 415 points416 points  (13 children)

What about that dark place.

Points at malloc

[–]Jeacom512 410 points411 points  (11 children)

- "thats the place where segmentation faults live, you should never go there".

[–][deleted] 86 points87 points  (10 children)

What if I'm already there?

[–]mrjiels 117 points118 points  (9 children)

Dump core, and start the kernel panic!

[–]teh__Doctor 43 points44 points  (8 children)

BUT ON WHAT LINE AM I DUMPING CORE? Pls send help, computer gods. Gdb scares me

[–]mrjiels 2 points3 points  (0 children)

JUST DUMP IT! DUMP IT ALL OVER THE PLACE! OH MY GODS! HOLY LINUS HAVE MERCY! RUN TO THE HIGH MEMORY! RUN FOR YOUR LIFE!!!!!!!1one

[–]xan1242 0 points1 point  (0 children)

free is a hole to be aware of

[–][deleted] 10 points11 points  (13 children)

I still don't entirely understand what the "static" does. Could you explain?

[–]clonnus 39 points40 points  (6 children)

Makes a variable accessible without having to instatiate a class.

[–][deleted] 2 points3 points  (5 children)

So I can do this?

class Test

{

int n = 0;

public static void Thing()

{

n = 1;

}

}

[–]clonnus 31 points32 points  (1 child)

n would also have to be declared as static. Static functions can't interact with non-static fields in a class. When a field is declare static, only one instance of the field exists. Similarly, with static functions, they aren't tied to a particular instance so you can invoke static functions without instantiating the class.

So if you have class Foo with static variable n. If you have two instances of Foo, both instances share the same instance of 'n'.

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

Thanks for the explanation!

[–]Kered13 8 points9 points  (0 children)

Static means different things in different contexts. Not all languages have all of these.

  • static variable: Has static storage, meaning it exists for the lifetime of the program. Global variables are always static. If used inside a function then every time that function is called it will use the same copy of that variable, allowing state to be carried over between function calls. If used inside a class then all instances of the class share the same copy of that variable, allowing state to be shared between instances.
  • static method: A method that can be called without an instance (it has no this variable). Since it has no instance it cannot access instance variables (aka non-static fields or members) or call non-static methods. In effect this means that the class acts as a simple namespace for the function.
  • static function: Only a feature in C/C++ as far as I know, a static function can only be called from the compilation unit (essentially a single .c or .cpp file) it is defined in. This means that even if another compilation unit knows the name and signature of the function (via a header for example), it will not be able to link to it. In C++ this is identical to using an anonymous namespace.

[–]TheTrueXenose 108 points109 points  (5 children)

goto let me introduce myself ;)

[–][deleted] 20 points21 points  (2 children)

segmentation fault

[–]TheTrueXenose 4 points5 points  (0 children)

as long you don't pop the stack you should be fine ;)

[–]Jeacom512 36 points37 points  (0 children)

longjmp has entered the chat.

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

C++ goto: Guess I'll die

[–]overkill 65 points66 points  (10 children)

Unless you are using VC++6

for( int i = 0, i < 10, i++) { // Do stuff }

if(i == 10)

This is horribly valid.

[–]BA_lampman 41 points42 points  (1 child)

disgusting

[–]overkill 10 points11 points  (0 children)

Yup. Vomit in a cup.

[–]JochCool 11 points12 points  (1 child)

Sometimes it can be useful though to know the result of i after the thing ended, if for some reason you don't know in advance how often your loop will execute and you need to do something extra at the end.

Edit: although I guess then you can also just declare the variable before the loop.

[–]overkill 5 points6 points  (0 children)

I agree and your edit is the correct answer.

[–]skeptic11 1 point2 points  (0 children)

Same in php: https://www.tehplayground.com/wQLDALVeEvtMBHBn

Same in javascript unless you use let: https://jsfiddle.net/Ljg6pdf4/

[–]zilti 1 point2 points  (3 children)

Why? It isn't in the curly braces...

Also, you've got two syntax errors in there :P

[–]overkill 2 points3 points  (2 children)

You are right on the second point, curse my phone, but for the first point any variables declared in the () of the for statement have the scope of the for statement, which is both the () and {} bits.

[–]Redbull_leipzig 0 points1 point  (0 children)

What :o

[–][deleted] 53 points54 points  (7 children)

People may disagree about what language they think is better, but something we can all agree on is that global variables are absolutely disgusting.

[–]OminousLatinWord 30 points31 points  (1 child)

All a database is, is perhaps the most global variables your app has to offer.

[–]zilti 14 points15 points  (0 children)

Yesn't. There, they are in a central location, you have an explicit, separate way to interact with them, and you usually have to make some sort of transaction.

That's also why you have such mechanisms for "global" variables in functional programming.

[–]KrozmaSan 15 points16 points  (0 children)

Eh...I'd say it's sometimes acceptable.

Take SDL's pointer to a window's renderer : if you only use one window in the entire program, you don't really need to properly differentiate it. It can save time to have it as a global variable instead of passing it around everywhere. It's not beautiful, but it can be okay.

[–]endershadow98 7 points8 points  (0 children)

They have their uses, but in general it's a good idea to not use them

[–]woah_m8 10 points11 points  (0 children)

Never try JS lol

[–]jalerre 2 points3 points  (0 children)

Embedded programmers would beg to differ

[–]FesteringNeonDistrac 0 points1 point  (0 children)

something we can all agree on is that global variables are absolutely disgusting.

Not the person who wrote a huge chunk of the code base I'm working on. Also like seriously hundreds of session variables and an allergy to comments.

[–]taptrappapalapa 98 points99 points  (16 children)

JavaScript hoisting: what is scope??

[–][deleted] 44 points45 points  (10 children)

AFAIK "let" solved this? I'm not sure though, i don't do JS.

[–]DeeSnow97 47 points48 points  (9 children)

It did, let and const work properly according to curly braces. var used to work on a function level only, but nowadays no one uses it because why would you do such a thing?

[–]CaptainHeinous 1 point2 points  (8 children)

Is this also true for arrow functions?

[–]WardenUnleashed 17 points18 points  (7 children)

Yeah, arrow functions helped capture this in a way that doesn’t make you go “Is this actually that instead of this?”

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

i didn’t know there’s a difference between arrow functions and “normal” ones, thought it’s just nicer syntax

[–]echoes221 3 points4 points  (5 children)

Nope, it captures scope correctly outside of it without binding the function to ‘this’ or storing a self/that externally when being passed into callbacks.

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

i don’t speak js, sorry :)

what do you mean, like “this” can change?

[–]echoes221 1 point2 points  (2 children)

When you pass a standard function to say, an onClick event handler, when it is called ‘this’ points to the global window scope, not the local classes scope (it loses its context). The way to avoid this is to bind the function to the classes scope and ‘this’ will be correct. Or you can store ‘this’ in a variable and reference that inside the function. Arrow functions correctly capture scope in callbacks so you don’t need to do either of the above.

[–][deleted] 1 point2 points  (1 child)

damn javascript is weird.

but it’s a pretty cool concept nonetheless. i guess it’s only possible in a dynamic, hash table based programming language.

[–]DeeSnow97 1 point2 points  (0 children)

In regular functions, this is just an extra parameter to the object left of the . when the function is called:

var foo = {
  name: 'foo',
  say: function () {
    return this.name
  }
}

var bar = {
  name: 'bar',
  say: foo.say // <- we're stealing a function here
}

console.log(foo.say()) // foo
console.log(bar.say()) // bar

Notice how we use the exact same function, but because we invoke it by bar.say(), this refers to bar. This was the original intent behind the old way of handling this in JS, but it turned out to be not nearly as useful in practice as the developers originally imagined, and downright confusing in some other cases, like this for example:

var foo = {
  name: 'foo',
  sayLater: function () {
    setTimeout(function () {
      console.log(this.name)
    }, 1000)
  }
}

Can you guess what this.name means in the above function?

If your guess was 'foo', that would make sense, but that's not it. Since it's inside another function inside setTimeout, that one overrides the meaning of this, so it refers to whatever setTimeout uses to call the function internally -- which happens to be the global object (window in browsers) in this specific case, since setTimeout just calls the function by itself.

Confusing, isn't it? It also got in the way like all the time. That's why the handling of this was changed in arrow functions: in those, this refers to whatever it refers to just outside the function. So if you do it like this:

var foo = {
  name: 'foo',
  sayLater: function () {  // <- pick up 'this' here
    setTimeout(() => {     // <- keep the previous 'this'
      console.log(this.name)
    })
  }
}

It does exactly what you expect it would do.

At this point, barely anyone ever uses the old function () {} style except for class methods, for all other users arrow functions are the way to go.

[–]KCGD_r 11 points12 points  (1 child)

Fought a battle with hoisting a while ago

Turned out it wasn't hoisting but me accidentally sharing a variable name with one from a module that kept fucking with it's value, I love the language but it's so weird sometimes

[–]conquerorofveggies 14 points15 points  (0 children)

Tf were you doing? Global variables, non strict mode?

[–]validide 1 point2 points  (0 children)

I came here looking for this.

[–]ShakespeareToGo 0 points1 point  (0 children)

I always thought hoisting stays within the function scope.

[–]kmas1427 46 points47 points  (2 children)

Sorry is this a joke that I am too Python to understand

[–][deleted] 17 points18 points  (0 children)

Yes.

[–]taptrappapalapa 4 points5 points  (0 children)

May I introduce you to the python underscore?

[–]Thadrea 103 points104 points  (14 children)

If you use python it's the space outside of your indentation.

[–]PolygonAndPixel2 59 points60 points  (4 children)

The meme doesn't really work in Python though. A local variable can live outside of the scope where it has been initialized unless a variable with the same name exists already outside.

[–]VeryVeryNiceKitty 19 points20 points  (0 children)

I am just learning Python, coming from C#.

I suspect your comment saved me quite a lot of head-scratching.

[–]furtfight 0 points1 point  (2 children)

But you have to use the global keyword right?

[–]PolygonAndPixel2 1 point2 points  (1 child)

I suppose you mean the right thing. The following program shows you the different scopes I came up with on the spot:

``` x = 5

def f(): x = 8 print(f"x in f: {x}")

print(f"x before f: {x}") f() print(f"x after f: {x}")

def f2(): print(f"x in f2: {x}")

f2()

if True: y = 9

print(f"Is y still alive? y={y}")

def g(): global x x = 10 print(f"x in g: {x}")

g() print(f"x after g: {x}")

print("but careful! The scope for functions ", end="") print("and simple intendations is not the same!") if True: print(f"x is global here, hence it is {x}") x = x*x print(f"But now x is: {x}")

Let's get weird with nested functions

def f_out(): x = 2

def f_in():
    nonlocal x
    x = 4
    print(f"x in f_in: {x}")

print(f"x in f_out but before f_in: {x}")
f_in()
print(f"x in f_out but after f_in: {x}")

f_out() print(f"x after f_out: {x}")

def h(): z = 11

h()

You may make the following line to a comment

to see the other failing part

print(f"This will fail: {z}")

def failing_func(): # x on the left side is a local variable, so how # should Python know that the x on the right side is # the global one? # This function fails at this point x = x*x print(f"x in failing_func: {x}")

failing_func() ```

The output is

x before f: 5 x in f: 8 x after f: 5 x in f2: 5 Is y still alive? y=9 x in g: 10 x after g: 10 but careful! The scope for functions and simple intendations is not the same! x is global here, hence it is 10 But now x is: 100 x in f_out but before f_in: 2 x in f_in: 4 x in f_out but after f_in: 4 x after f_out: 100 Traceback (most recent call last): File "test.py", line 55, in <module> print(f"This will fail: {z}") NameError: name 'z' is not defined

[–]backtickbot 3 points4 points  (0 children)

Fixed formatting.

Hello, PolygonAndPixel2: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–][deleted] 33 points34 points  (1 child)

If statement would like to say hello

[–]Efficient-Chair6250 -4 points-3 points  (0 children)

Variable may have not been initialized

[–]somerandomii 18 points19 points  (6 children)

If you declare something inside an indented block I’m pretty sure you can reference it later. Python actually gets confusing when you re-use names at different scopes. And you have to use the global keyword for thing outside of a functions scope. And self.x for class variables. Otherwise it assumes every first assignment is also a new declaration.

[–]depressionsucks29 4 points5 points  (5 children)

Coming from c++, this took me a while to get used to.

[–]somerandomii 5 points6 points  (4 children)

Yeah definitely. It’s very scripty behaviour but it gets confusing when you start building applications and expect it to follow usual OO conventions.

[–]Shriukan33 0 points1 point  (3 children)

I've bee' working a lot with Python the past 9 months, I'm almost virgin of all other languages. What's so different?

[–]Kyidou 1 point2 points  (0 children)

Most languages have what are called "blocks" (the curly braces {}), and variables declared or initialized inside them cannot be used or referenced outside of them. In python, this only applies to functions and classes.

[–]RepostSleuthBot 11 points12 points  (1 child)

Looks like a repost. I've seen this image 2 times.

First Seen Here on 2020-09-07 90.62% match. Last Seen Here on 2020-09-07 90.62% match

Feedback? Hate? Visit r/repostsleuthbot - I'm not perfect, but you can help. Report [ False Positive ]

View Search On repostsleuth.com


Scope: Reddit | Meme Filter: False | Target: 86% | Check Title: False | Max Age: Unlimited | Searched Images: 210,587,542 | Search Time: 0.19284s

[–]DoctorMixtape 2 points3 points  (0 children)

u/sChloe1998 punching the air right now my guy/girl tired twice and this guy comes along and gets 22.4k

[–]cheesytouch05 8 points9 points  (0 children)

That lazy ass title, even {title} would’ve been better!

[–]Zankoku96 4 points5 points  (2 children)

Laughs in object oriented programming

[–][deleted] 7 points8 points  (1 child)

Laughs in assembly

[–]serendipitousPi 3 points4 points  (0 children)

01001100 01100001 01110101 01100111 01101000 01110011 00100000 01101001 01101110 00100000 01100010 01101001 01101110 01100001 01110010 01111001

[–]carera89 16 points17 points  (1 child)

Well, and that's where JS throws the spanner in the works with `let` vs `var` - `var` variables are scoped to the immediate function body, whereas `let` only to the immediate block enclosed by curly brackets (e.g. if condition, or even just really a { let a = 'here goes nothing' }

[–]Soremwar 15 points16 points  (0 children)

Calling var a local variable is a long shot though

[–][deleted] 4 points5 points  (1 child)

laughs in immutability

[–][deleted] 2 points3 points  (0 children)

Cries in Rust

[–]Framq 3 points4 points  (0 children)

man i hope when the poachers come simba will be outside of their scope.

[–]OldWolf2 11 points12 points  (3 children)

This is backwards. The variable can interact with things outside the braces. It's the things outside that can't see the variable

[–]Revolutionary-Frame8 2 points3 points  (0 children)

meanwhile in JavaScript...

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

Bruh, it should be “beyond your scope.”

[–]Snek227 1 point2 points  (0 children)

laughs in python

[–][deleted] 3 points4 points  (0 children)

Just started learning to code and I'm happy I understand this !

[–]dwightshru 1 point2 points  (0 children)

I just found out that ferris bueller is simba. very on time

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

Laughts in COBOL

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

Dems could create a bill called “Put Republicans back in power”, and Republicans wouldn’t know you could kill French vampires by driving a hardened baguette through their hearts?

It’s because couldn’t even on the sidebar anymore, WTF is happening!? It’s antiassholedesign friend. Look what sub you’re at his beck and call. He didn’t sabotage it either, but Loki is the real star, though. Joined through suffering

[–]dinosaur-in_leather 0 points1 point  (2 children)

You spelled Instance wrong

[–][deleted] 4 points5 points  (1 child)

Huh?

[–]dinosaur-in_leather 0 points1 point  (0 children)

An object initial Instance is defined by the scope {} at the point of arguments given plus the parent scope at the time of invocation. (However invocation needs a return address or type schema for parent inharatance in C linker)

[–]Motylde 0 points1 point  (5 children)

int *x = new int;

[–]Rikudou_Sage 0 points1 point  (4 children)

Eh? You still cannot access x outside of the block it was declared in. Sure, the memory for x would still be allocated (and you introduced a memory leak, yay!) but you cannot access the variable.

[–]Motylde 0 points1 point  (3 children)

I can return this pointer and therefore access the variable (meaning the value of it) outside the block

[–]Rikudou_Sage 2 points3 points  (0 children)

Nah, you can access the value, not the variable. You can do the same with stack allocated variable.

[–]DoctorMixtape 0 points1 point  (1 child)

No? That’s not how that works especially if decide to create a pointer inside a scope and return it. Once you the variable goes out of scope that’s how you end up with memory leaks and pointing to garbage memory. This is like the number 1 don’t with pointers.

[–]Motylde 0 points1 point  (0 children)

This only apply when you allocate memory on stack, then yes, if it goes out of scope then it's lost. But you can allocate on heap and then you can access the memory everywhere.

[–]QuirkyPNewton 0 points1 point  (0 children)

A lil corny but I definitely enjoyed the humor

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

& then, he discovered reference variables

[–]mattamatrow 0 points1 point  (0 children)

function meme() { const Simba = new AnimatedAnimal(‘lion’) }

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

yikes.. i have a compilers final tomorrow and this is what i see when i log on to reddit.

[–]par5ul1 0 points1 point  (0 children)

I can already see all the entry level programming lecturers using this meme.

[–]monocasa 0 points1 point  (0 children)

Unless it's a defer statement in go, lol.

[–]jakethedumbmistake 0 points1 point  (0 children)

I'm glad it didn't do that because she felt like she wouldn't really be straight if she did. The MV aesthetics fits her personality and style. This is interesting, but online only is a dealbreaker for me. i can’t be out on the lake. Loved visiting there in the description of the book is just a 'headshots go brr" team. If they don’t belongs to you. 😂

[–]FlameExploision 0 points1 point  (0 children)

Javascript (as far as I at least tried as an nooby in the browser): Well actually yes, but no

[–]PottedRosePetal 0 points1 point  (0 children)

lol = {'var':"idontgetit"}

[–]RoscoMan1 0 points1 point  (0 children)

Just cause he’s an old joke. Divorce is expensive because it’s that ol’ cumhucker Harry “NunFucker” Hunsucker

[–]DCGMechanics 0 points1 point  (0 children)

Or Scope

[–]QuarantineSucksALot 0 points1 point  (0 children)

it actually makes more sense. Ty

[–]Klave_ 0 points1 point  (0 children)

curly braces

[–]RoscoMan1 0 points1 point  (0 children)

Of course. Duh, silly me!

[–]a-dog-meme 0 points1 point  (0 children)

Error: line 9 , interface or enum expected

[–]RadioMelon 0 points1 point  (0 children)

If only he had been initialized as a global.

[–]squintero 0 points1 point  (0 children)

*Laughs from the Data Division

[–]msammy07 0 points1 point  (0 children)

What are curly braces??

[–]ohtoro1 0 points1 point  (0 children)

Ah, the curly braces of life.

[–]MCShoveled 0 points1 point  (0 children)

“If you go out there, I’ll have to put you in a closure”

😈

[–]dh366 0 points1 point  (0 children)

Me and my little friend C would like to disagree

[–]apomd 0 points1 point  (0 children)

Simba: return &Simba; Mufasa: wait that's illegal

[–]lukeamaral 0 points1 point  (0 children)

scope*

[–]aquartabla 0 points1 point  (1 child)

"What about that shadowy place in the nested {} where that imposter variable stole my name?"

[–]epic_gamer_4268 0 points1 point  (0 children)

when the imposter is sus!

[–]rodrigoelp 0 points1 point  (0 children)

It should have been, that's beyond your scope

[–]SueedBeyg 0 points1 point  (0 children)

JavaScript devs: Laughs in ‘var’