Your Variables are Not Real.

  Рет қаралды 16,914

Jacob Sorber

Jacob Sorber

Жыл бұрын

Patreon ➤ / jacobsorber
Courses ➤ jacobsorber.thinkific.com
Website ➤ www.jacobsorber.com
---
Your program's variables are not real // They are not containers for bits or buckets for data. Variables, like so many computing topics are abstractions. This video helps you see what's actually going on under the hood.
Related Videos:
Pointers and Arrays: • Arrays, Pointers, and ...
Makefile-related Videos: • Learn make in 60 seconds.
***
Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
More about me and what I do:
www.jacobsorber.com
people.cs.clemson.edu/~jsorber/
persist.cs.clemson.edu/
To Support the Channel:
+ like, subscribe, spread the word
+ contribute via Patreon --- [ / jacobsorber ]
Source code is also available to Patreon supporters. --- [jsorber-youtube-source.heroku...]

Пікірлер: 89
@mk72v2oq
@mk72v2oq Жыл бұрын
Yes, they are not real, they are integers.
@swavek_wlodkowski
@swavek_wlodkowski Жыл бұрын
No; not always. Sequences of bits are not equivalent to integers, the same way phone numbers are not integers, but sequences of (usually) decimal digits: try to remove leading zeros in some of them without breaking them, or get a valid phone number by multiplying two other phone numbers. Whether 32 bits are an integer, a float, a block of 32 single-bit flags, a 4 ASCII character array, or for example a complex number of two short unsigned integers, all of this is derived from a high-level abstraction data type that permits certain instructions and values. A variable is a named storage of a certain data type (and thus size), interpreted as an integer, or otherwise.
@chindianajones3742
@chindianajones3742 Жыл бұрын
@@swavek_wlodkowski i think you missed the point of his comment. OP was making the joke that variables are not real numbers (as in the set of real numbers) but rather they are integers (as in the set of integers) because that is how we generally interpret their physical configuration in memory -- through abstraction as you pointed out. At least I think that's what op meant lol
@markojojic6223
@markojojic6223 Жыл бұрын
All integers are real, but i get that you are referring to the real numbers that are nut integer.
@fders938
@fders938 Жыл бұрын
Integers are a subset of real numbers :)
@leofigoboh1611
@leofigoboh1611 Жыл бұрын
In Fortran, GOD is REAL (unless declared INTEGER).
@ItzBreakfast
@ItzBreakfast Жыл бұрын
Variables aren't real, they can't hurt you. Variables: Segmentation fault
@brianb2308
@brianb2308 Жыл бұрын
Overview: Declared variables often exist close to each other in memory space, which can make certain bugs hard to track down, especially if you're writing to out-of-bounds indices in a stack array because that can overwrite the other local variables that are declared.
@voidwalker7774
@voidwalker7774 Жыл бұрын
... well never had this bugs in Rust 😎
@smartkorean1
@smartkorean1 Жыл бұрын
The bugs that result from these are very confusing indeed! Once spent an entire night trying to track down why one array was being overwritten Turns out an adjacent array in memory was being written to out of bounds.... Thankful for GDB!
@dthe3
@dthe3 Жыл бұрын
@@voidwalker7774Any sane C++ developer would avoid doing this mistake too.
@chrissaltmarsh6777
@chrissaltmarsh6777 Жыл бұрын
It's a lot of fun when you do that against mapped hardware controller memory. Understanding what's going on under the hood is important.
@don.hinton
@don.hinton Жыл бұрын
Really enjoy watching your videos. I'm a c/c++ developer, but never really had the pleasure of taking a lot of cs courses -- I can only really think of 3 or 4. Your simplified, bite-sized, demos and explanations, are fantastic.
@swavek_wlodkowski
@swavek_wlodkowski Жыл бұрын
Great explanation. To add to this discussion, a variable does not even have to reside in addressable memory. A variable is just an abstraction of a named storage location. That's it. A compiler may put it in some location in primary memory, or inside a CPU register, if possible. The latter case is important to understand why some multi-threaded code behaves the way it does. Lastly, let's not forget about variables declared as references (C++), which sometimes can be handled under the hood as pointers, but often do not have to be mapped to any additional storage location of their own at all. // Thanks for the video!
@finmat95
@finmat95 Жыл бұрын
This is tricky
@zvxcvxcz
@zvxcvxcz Жыл бұрын
Even regular non-reference variables can be totally optimized away by the compiler on the more aggressive optimization settings.
@tk36_real
@tk36_real Жыл бұрын
some things I wanna add: - using the address (e.g. to print it) might be what's responsible for spilling on the stack in the first place and the idea of a memory blob is not really useful for values that are only in registers - 7:30 the compiler is technically allowed to call `atoi(argv[1])` and assume it returns 0 and consequently initialize d to `{42}` from the get-go
@llamallama1509
@llamallama1509 Жыл бұрын
I'm not sure the compiler decides the memory location of variables as much as the linker and OS.
@your-mom-irl
@your-mom-irl Жыл бұрын
the compiler can decide pretty much the entire memory layout if you want to, but usually it doesn't because PIE allows the kernel to randomize addresses for security reasons. if you compile the following: '#include int main() { printf("main: %p ",(void*)main); return 0; }' with the -no-pie flag (for gcc) it will always output the same address. of course that address is picked by the linker, but thats part of the compiler in my opinion. (and you can of course write a linker script to tell the linker exactly where you want your code, if thats important to you. but the kernel might not like some address you are trying to get if its reserved for kernel use) anyway, i think that what the video referred to is that the compiler decides the layout of variables on the stack, not the absolute address of the variables, just their relative offset to each other. same for statically allocated variables, the compiler decides their relative order and padding and all that on the .data or .bss or .rodata sections
@maxaafbackname5562
@maxaafbackname5562 Жыл бұрын
The order is defined by the compiler. Defining the exact location depends on different things and will be assigned by the compiler or the linker.
@joeyoest1105
@joeyoest1105 Жыл бұрын
Sounds to me like ‘C’ stands for ‘clobber memory’, lol. Thanks for all the great content!
@creepr524
@creepr524 Жыл бұрын
Jacob. Since in the von neumann architechture code is memory, can you memcpy a function into a buffer and execute that buffer?
@case_sensitive
@case_sensitive Жыл бұрын
That sorta sounds like a buffer overflow attack. Say you have an input field, the attacker essentially write enough data into that field that it reaches the code section of memory. This can be used to run some code
@creepr524
@creepr524 Жыл бұрын
@@case_sensitive depends on where the buffer is located. If the executable buffer is in the heap theres no guarantee that the input can overflow into said buffer. Especially not when the input is on the stack
@69k_gold
@69k_gold Жыл бұрын
"Size doesn't really matter here" what a chad 🗿
@JLSXMK8
@JLSXMK8 Жыл бұрын
I recommend that you delve into this just a bit further. Is "Watch" on a variable useful for something like this? How so?
@DominoPivot
@DominoPivot 2 ай бұрын
I would add that in a language like C++, a variable is an abstraction that associates a location in memory, a type, and a name in a given scope. The data in the memory is just a bunch of bits; it's the type of the variable that determines how that data is interpreted and manipulated, that conveys meaning to those bits. This is a fundamental difference with dynamic languages where the type is part of the value itself, not the variable, so thinking about variables as boxes for values tends to be less of an issue.
@SteelHorseRider74
@SteelHorseRider74 Жыл бұрын
Therapist: "... is that 'pointer to the variable' in the room with us now?" Me: ... *shudder*
@GRHmedia
@GRHmedia Жыл бұрын
This issue is why I disagree with them no longer teaching ASM as the first language. When I went started college they expected you to take ASM before you could take C. I've never known a programmer who came from that system that had issues with understanding these and other more complex issues. Sure there were lots of people that dropped because they couldn't handle ASM. I honestly think the programming world is better for that.
@thorham1346
@thorham1346 6 ай бұрын
Assembly language is a terrible first language.
@philipphortnagl2486
@philipphortnagl2486 Жыл бұрын
What would be a quick fix for this? Defining a const variable ?
@ahmadalastal5303
@ahmadalastal5303 Жыл бұрын
const variable is just a syntactic sugar by the compiler to prevent the programmer from changing that variable before doing compilation, where in real life nothing is "const" the const variable can change if you change the location of memory it holds, the same goes for private members of a class and everything else, everything is changeable if you reach their memory address
@rustycherkas8229
@rustycherkas8229 Жыл бұрын
Perhaps a recursive function would provide a demonstration that "there's not a single bucket labelled A".
@swavek_wlodkowski
@swavek_wlodkowski Жыл бұрын
Until you choose some tail-recursion examples where it actually can be. If you start thinking about variables as anything more than an abstraction of storage (to be resolved by a compiler) with a specific life cycle, there is likely to be some edge case where your simplification no longer holds true.
@LogicEu
@LogicEu Жыл бұрын
Great video! Also, variables are just zeros and ones, if we all agreed on a new arbitrary way of representing numbers in binary, the same variables that seem so clear today would become nonsense. We know 0001 == 1 because we all agree on it. That's the importance of standards
@ryanunknown4181
@ryanunknown4181 Жыл бұрын
Hi Jacob! I really would appreciate an answer on this. I am just curious why is it you make all this C/C++ content? Seems like C isn't that popular nowadays to talk about and you have kind of made it your thing. Don't get me wrong, I love your content and I personally find C to be my favorite programming language.
@ebebebeb7283
@ebebebeb7283 Жыл бұрын
IMO you can't properly understand programming without understanding C
@meluobote7664
@meluobote7664 Жыл бұрын
nice video, maybe we can regard all variable as a pointer that point to some places in memory. then we must know which places we want it to points to and make true it do not point to the places we don't want it to point to
@meluobote7664
@meluobote7664 Жыл бұрын
a is zero * pointer. *a is one * pointer. **a is two * pointer. my english is not good, hope it is clear
@packmandudefake
@packmandudefake Жыл бұрын
Reject reality, free() your life.
@_modiX
@_modiX Жыл бұрын
I think it is counter-intuitive to not refer the black board as a "stack". Also it would've been useful to expand on the idea by explaining objects and the heap, because a variable always points to memory on the stack, but the value on the stack contains all information to find the object on the heap, like address and size.
@your-mom-irl
@your-mom-irl Жыл бұрын
The stack is just a piece of memory just like the rest of it. You can allocate a chunk of memory with Malloc and then just use that as your new stack
@maxaafbackname5562
@maxaafbackname5562 Жыл бұрын
Not true. The story holds also for global variables. They are not on the stack. Besides that, the compiler can store variables into CPU registers in stead of memory.
@_modiX
@_modiX Жыл бұрын
@@maxaafbackname5562 You are right, the heap is also used for data that gonna outlive the call stack, my bad.
@xcoder1122
@xcoder1122 Жыл бұрын
Variables don't have to exist in memory at all. If the compiler can use a CPU register all the way to represent a variable, it will do so and no memory is ever assigned to that variable. Of course, this will only wok if you never use `&a` to get the address, as a register has no address and that way you force the compiler to assign memory to that variable. A compiler may even assign neither memory nor a register to a variable if it doesn't have to. E.g. consider this code `int a = getA(); useA(a);` The compiler can turn that into `useA(getA());` and thus won't manifest the variable at all by just passing through the result of one fiction call to another one, and how that works is platform specific; it might use memory or registers or neither. Even if it uses register/memory, that isn't assigned to a variable, just use a temporary storage. Also if two variables never overlap in usage scope, the compiler may use the same memory for both of them to save memory. E.g. "int a = ...; /* 1st code that uses a */ int b = ...; /* 2nd code that uses b but never a */` If there is no way to get from the second code back to the first one, why shouldn't a and b share the same memory? However, most of that will only happen if you build with optimization enabled as a compiler must perform some analysis to know which variable scopes do overlap, which variables can be passed through directly, and which variables won't ever require memory and can just be a register.
@cmdlp4178
@cmdlp4178 Жыл бұрын
Just a little tip: make -B to cleanly build
@packmandudefake
@packmandudefake Жыл бұрын
Be aware: it removed all my CPP code.
@aaaowski7048
@aaaowski7048 Жыл бұрын
-"your variables are not real, they cant hurt you" my brain after stalling for 15 minutes trying to find an adequate variable name: "..."
@amrgaber4400
@amrgaber4400 11 ай бұрын
you know what is even worse to debug? giving a value to a pointer to function (The address of a function)-> check if that value is NULL. you find it NULL THOUGH you gave it a value which is a function name. but if you give a value to a that pointer which is the address function -> call the function once using the pointer -> check if that value is NULL -> now it's not NULL. why? I DON'T KNOW. and this makes me crazy.
@_ab_6490
@_ab_6490 Жыл бұрын
entry(main) == 0x1000 (offset 0x1000) = 255 // 1 memory cell = 1 byte = 8 bit function not real variables not real datatypes not real just description
@TheRealBadarka
@TheRealBadarka Жыл бұрын
Any chance to get that shirt? 😅 its awesome
@JacobSorber
@JacobSorber Жыл бұрын
I'm working on bringing it back.
@packmandudefake
@packmandudefake Жыл бұрын
Oh nooo, my favorite informative technologies teacher, Jacob-san, became insane! What should I do, after all this, I can’t accept the loss of daddy senpai from my life. Is the world be kind to get him up and mentally healthy after every war with pointers we got through together. /ᐠﹷ ‸ ﹷ ᐟ\ノ
@McGewen
@McGewen Жыл бұрын
pointer arithmetic
@mehregankbi
@mehregankbi Жыл бұрын
Well, processes aren't real either. can we have a video about how the actual unit of work in linux is a task, rather than process? then we could get our hands dirty with the clone() syscall. like, how only fd and/or address space is shared etc. how fork is basically a subset of clone. i'd really love that.
@thebigmacd
@thebigmacd Жыл бұрын
He has videos about that.
@mehregankbi
@mehregankbi Жыл бұрын
@@thebigmacd i thought i had seen those, but i couldn't find them. you mind giving me a link?
@alkaratus9189
@alkaratus9189 Жыл бұрын
Variables are real, but it is just word-named RAM adresses. In C++ there are ways to avoid this kind of overloads... but not in C
@gaureesha9840
@gaureesha9840 Жыл бұрын
is this the reason why C/C++ are not type-safe?
@valizeth4073
@valizeth4073 10 ай бұрын
C++ is far more type safe than C and no, this is not the reason.
@saltyowl3229
@saltyowl3229 Жыл бұрын
Gonna be honest this one disappointed me a little, not cause the video was bad! But because I was expecting it to be about how the compiler waits to actually initialize variables until they’re used. Like if you debug a program, have a bunch of variables assigned, then do a few lines of arithmetic, break on the first like of that, then any variables not used on that line just dont exist to the debugger… sometimes at least. Reasonable behavior but a little annoying sometimes. Was hoping to learn more about it
@finmat95
@finmat95 Жыл бұрын
This is dangerous!
@zvxcvxcz
@zvxcvxcz Жыл бұрын
Not helpful, there is no functional distinction between the different analogies you used. What you're saying only makes any sense because I already know what you're trying to say for how it works. I'm honestly still confused about what the problem is with thinking of them as "real" is. I guess I don't get what you mean by 'real' since it all would work identically with the bucket analogy... we start at bucket A and move over one bucket... I think the conceptual thing isn't about 'realness' but about layout and indexing. Don't click your heels, go use sanitizers like Asan, Tsan and Ubsan, and memory analyzers like memstomp and Valgrind. And don't forget regular compiler warnings, get that -pedantic, etc.... And when things are good, we can see if we can make them bad with fuzzers.
@nickhuynh6321
@nickhuynh6321 Жыл бұрын
So is a place in memory real?
@Henry-sv3wv
@Henry-sv3wv Жыл бұрын
your pc uses virtual memory, so the memory your program sees isn't real XD
@georgecop9538
@georgecop9538 Жыл бұрын
Yeah, variables are just references to stack/heap addresses.
@swavek_wlodkowski
@swavek_wlodkowski Жыл бұрын
Not really; I would not say so. Not just the stack (also CPU registers or static segments), and not the heap (because the heap offers dynamic storage duration, and variables do not use it). They are also not references (which are a high-level concept), but I suspect that you meant that they can be translated to a memory address under the hood - this is also not universally true (it does not apply to register storage variables).
@georgecop9538
@georgecop9538 Жыл бұрын
@@swavek_wlodkowski I know, just wanted to underline the fact that variables reside somewhere. And variables in (some)interpreted languages (JavaScript) are on the heap.
@swavek_wlodkowski
@swavek_wlodkowski Жыл бұрын
@@georgecop9538 This could be right, if not for the fact that the video starts with C/C++ in the thumbnail and the author wears a malloc t-shirt. Then someone would argue that there is a environment where what a programmer sees as a variable is stored in a secondary memory of some remote network host ("databases, I am looking at you!").
@ohwow2074
@ohwow2074 Жыл бұрын
They could reference static storage too. That's where thread_local and static come into play.
@zvxcvxcz
@zvxcvxcz Жыл бұрын
@@georgecop9538 Not always, variables may in fact reside nowhere. Create a variable, don't use it, compile with -O3, start GDB and look for your variable, good luck.
@valizeth4073
@valizeth4073 10 ай бұрын
This is the main problem with people looking at languages like C from a low level point of view. Variables are real in C, your use of pointer arithmetic is undefined, C doesn't target your machine but an abstract machine, C is not assembly and there's no 1:1 mapping between the two. Just because your variables might be optimized away or anything else, it doesn't relate to C at all.
@rogo7330
@rogo7330 Жыл бұрын
TL;DR C is not an assembly. It's high-level language that abuses memory concept and allows a lot of optimizations thanks to undefined behaviours (delete code that can't be real if you read it as it written, not as you'll think it will run on CPU).
@valizeth4073
@valizeth4073 10 ай бұрын
This. The whole "*ackshually* x is really just y" tends to be false in 99.9% of cases, and are often just misconceptions because people believe that there's a 1:1 mapping between assembly and C. Just because x can become y when compiled, doesn't really say anything about x or y.
@BinGanzLieb
@BinGanzLieb Жыл бұрын
variables are alias for addresses, nothing else
@Henry-sv3wv
@Henry-sv3wv Жыл бұрын
a c varibale is adress + data type info or the compiler does not know what to do with that start of memory location where the variable begins at
@b213videoz
@b213videoz Жыл бұрын
The universe isn't real, why variables must be ?
@thiagoskapata
@thiagoskapata Жыл бұрын
Floats are not real. Real numbers are associative. Floats don't.
@rogo7330
@rogo7330 Жыл бұрын
Also good way to protect yourself from buffer overruns is to forgot abot dynamic arrays concept. They aren't real. If you really want them, just implement for yourself arenas using realloc and _write_ your code as operations on memory, not in OOP terms. OOP is generally bad idea, you just spending your time to it instead of writing code that does something (like a tool for specific job) and name your functions and types (structs, unions, complex stuff) like folder names, from top to bottom.
@douggrove4686
@douggrove4686 Жыл бұрын
Variable exist in memory and registers. Therefore they are not real. ow totally lame.
@jovialcupid9687
@jovialcupid9687 7 ай бұрын
Honestly I feel like this title is clickbait I already known all u tried to explain here and I still think they are "real". U could use this type of logic to littelary anything. What u see with your eyes is not real! It's just separate waves that your brain process and I can demonstrate that by using glasses. U see, image is different so it's not real! U can still use bucket analogy to explain this. "buckets have different sizes and if u try to add to many water to them they will overflow to next bucket". U littelary removed buckets to add squares that were doing exactly the same thing as bucket. And pointer is just pointing to the bottom of bucket.. Video itself is as always very good, but this metaphor about lack of existence feels silly, silly enough to make it feel like clickbait
@codedsprit
@codedsprit Жыл бұрын
What about making videos on rust.
@devnullsrevenge
@devnullsrevenge Жыл бұрын
This is a great video, unfortunately with a terrible title. I suspect it's for clicks, but bounds checking is such a critical concept. I wish you had named this something different. Just honest feedback, otherwise great work man.
@jenselstner5527
@jenselstner5527 Жыл бұрын
Variables are not real when they're declared int!
@31redorange08
@31redorange08 Жыл бұрын
Variables are very real. They have a name and they hold a value. CS 101.
@smudgerdave1141
@smudgerdave1141 Жыл бұрын
No. Your variables are 'real' fsvo 'real'. Buckets is a perfectly good analogy. Everbody understands that buckets come in different forms and different sizes with different capacities. What you're trying to demonstrate is that your buckets are somewhat fragile and that the integrity of your buckets can be compromised if you get your code wrong. Well that is perfectly true, and a valuable lesson, but I don't see how making out that variables are not 'real' is helpful to a beginner... and you don't define 'real' in any meaningful way anyhow. And your title is annoying clickbait.
What is a function prototype in C
4:38
Jacob Sorber
Рет қаралды 16 М.
Clarifying about literals, macros, and const (still not constant?)
13:14
ОБЯЗАТЕЛЬНО СОВЕРШАЙТЕ ДОБРО!❤❤❤
00:45
Iron Chin ✅ Isaih made this look too easy
00:13
Power Slap
Рет қаралды 36 МЛН
Clown takes blame for missing candy 🍬🤣 #shorts
00:49
Yoeslan
Рет қаралды 47 МЛН
How to Check Your Pointers at Runtime
14:12
Jacob Sorber
Рет қаралды 31 М.
Binary data exercise: how to tell if a file is a jpeg?
17:48
Jacob Sorber
Рет қаралды 13 М.
How Big Is A Character?
7:30
Jacob Sorber
Рет қаралды 9 М.
Why Function Pointers are Awesome
11:11
Jacob Beningo
Рет қаралды 6 М.
WHY IS THE HEAP SO SLOW?
17:53
Core Dumped
Рет қаралды 213 М.
The What, How, and Why of Void Pointers in C and C++?
13:12
Jacob Sorber
Рет қаралды 52 М.
Faster than Rust and C++: the PERFECT hash table
33:52
strager
Рет қаралды 531 М.
find memory errors quickly. (-fsanitize, addresssanitizer)
9:44
Jacob Sorber
Рет қаралды 16 М.
I made it FASTER // Code Review
38:46
The Cherno
Рет қаралды 532 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 778 М.