Тёмный

Clarifying about literals, macros, and const (still not constant?) 

Jacob Sorber
Подписаться 167 тыс.
Просмотров 15 тыс.
50% 1

Опубликовано:

 

26 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 121   
@jackkendall6420
@jackkendall6420 2 года назад
I am literally constantly confused by literals and constants!
@reptilicusrex4748
@reptilicusrex4748 2 года назад
Yes, please make a video explaining "pages, offsets, and how that all works". It's especially interesting how you accessed the page at time index 9:00. A more detailed explanation about that would be appreciated. Great content as usual, thanks.
@_Omni
@_Omni 2 года назад
It's simple, page_address = address - (address % 4096) because a page is 4096 bytes.
@_Omni
@_Omni 2 года назад
@@anon8510 yeah, 4 KiB, 2 MiB or 1 GiB but 4KB is default page size because you don't want to give out 1 GB chunks and waste memory.
@jaopredoramires
@jaopredoramires 2 года назад
yeah, I'm all for it too!
@mk72v2oq
@mk72v2oq 2 года назад
@@anon8510 it's not in stone, but every modern OS uses 4k pages by default. You can use larger page sizes, but you only can do it intentionally and explicitly, OS never changes page size by its own. Also it requires the process to run with root/admin rights in most cases, and OS can be configured to not allow page size changing at all. So doing it in the userspace app is generally a bad idea.
@maxaafbackname5562
@maxaafbackname5562 2 года назад
On an embedded system it is very normal to have const volatile variables. Mostly these are const volatile pointers, for example const volatile uint32t *ptr; These (points to) memory mapped I/O registers that are read-only registers like a status register. No way that the program can change them but the hardware can and will do. From the program point of view the register is read-only/immutable, not constant. Note: this is the same for hardware drivers on a PC. I don't think it is undefined behavior this way, with volatile.
@darvil82
@darvil82 2 года назад
The way you masked off those bits to get the address of the page is extremely interesting! And indeed, I'd love to see more content about stuff like this.
@hansdietrich83
@hansdietrich83 2 года назад
You can actually redefine a #define , without throwing warnings by literally undefining it using #undef before #defining it again
@anon_y_mousse
@anon_y_mousse 2 года назад
Best way really. I also undef macros in my header files that are intended only for the header to avoid leakage of weird effects.
@lennymclennington
@lennymclennington 2 года назад
That's already mentioned at 4:08
@SlideRSB
@SlideRSB 2 года назад
@@lennymclennington No it wasn't.
@lennymclennington
@lennymclennington 2 года назад
@@SlideRSB you're right, I didn't realise that he hadn't undef'd it before. Having that in the video is kind of weird actually since he's showing invalid C code without actually mentioning that it's invalid. The compiler he uses is lenient and just gives a warning but another compiler could just refuse to compile it.
@SlideRSB
@SlideRSB 2 года назад
@@lennymclennington He mentioned a couple of times in his video that not all compilers will work the same way since the behavior is undefined for the situation he's presenting. He also suggestion more than once that we don't do what he's showing in any code we actually care about.
@udhayakumar8259
@udhayakumar8259 2 года назад
defiantly Needed jacob, pages,offset,memory management..
@suhel19690
@suhel19690 10 месяцев назад
wow...analysing const in such a depth...really good...❤
@harshitjoshi3082
@harshitjoshi3082 2 года назад
Just read a few confusing comments about this somewhere and you made a video for it, great timing thanks 🌟
@donaldmickunas8552
@donaldmickunas8552 2 года назад
From what I can see, the compiler is treating a “const” as an unchangable value which is a constant. While it might be possible to change a const using some esoteric method, for those of us interested in writing and maintaining good C code, a const can legitimately be thought of as a constant.
@Spielix
@Spielix 2 года назад
The problem is that the compiler can only do that if the RHS of the definition is a literal expression as discussed. But C23 will add (a version of) constexpr to C which will make this stuff much better.
@styleisaweapon
@styleisaweapon 2 года назад
I think you are only looking at this from the definition and development side. In theory there is no difference between theory and practice, but in practice there is. This stuff has a real world impact on what the compiler can safely optimize. Compilers often do unsafe optimizations when asked, for example most compilers have a switch that assumes no aliasing between arrays. Projects often end up with unsafe optimizations disabled because the assumptions didnt always hold. Its 5 million lines of code now. -Wont Fix.
@JohnDlugosz
@JohnDlugosz Год назад
Not want to use enumeration constant for an array size: we used to! Enums predate `const` and other features, and were abused to provide properly scoped constant names (that is, not preprocessor symbols) for quite a while. If i recall correctly, when `const` was added to C, it still was not a constant expression like in C++, so you could not use a `const int ArraySize=64;` as a (regular) array size. Though now that silently creates a variable-sized array. When I learned K&R C in the 80's, function definitions looked different, there was no `void` keyword, and all structure fields had to be unique rather than the name scoped to that structure! And you could use the wrong name with an -> and it just uses that offset without complaint.
@portblock
@portblock 2 года назад
First let me say, you rock! I know I may have been one of the people who came off as a naysayer, but I think part of it was me stuck on the "not a constant" versus listening to what you were actually talking about, of which I understand now with this video. Kudos fine sir!
@oching4
@oching4 7 месяцев назад
I love computers. Thank you for teaching me so many things, friend!
@primosoma
@primosoma 2 года назад
Thanks, this video was very useful to me. Sometimes, different languages use the same word to indicate different things, or different words to say the same thing, and then it happens that you get confused even on trivial things. Maybe when I work at a low level I should forget all abstractions and just focus on the machine.
@konnosgar
@konnosgar Год назад
Haha nice. Programming comedy, love it. You learn a lot while having fun, like all types of comedy.
@greg4367
@greg4367 2 года назад
Good job Jacob.
@benoitrousseau4137
@benoitrousseau4137 2 года назад
The source of the confusion is likely C++ const. In C++ a const variable CAN be used as a constant expression if it is initialized with a constant expression, so it is a constant. (Or at least as much of a constant as the context allows it to.) Not only that but it is taught and recommended to use const (or constexpr since C++11) over #define because #define can cause issues with overload resolution and other type-sensitive issues. #define constants are pretty much obsolete in C++, but still an important part of the C language.
@JacobSorber
@JacobSorber 2 года назад
I agree.
@Spielix
@Spielix 2 года назад
C23 gets a less powerful (compared to C++) version of constexpr which should make this stuff way less messy.
@tusharsnn
@tusharsnn 2 года назад
In my experiment this also works without having to get the page starting address. Just modify A's memory protection instead of the whole page. void *p = (void *)((uintptr_t)&A); mprotect(p, 2, PROT_READ | PROT_WRITE);
@underdogrobot
@underdogrobot 2 года назад
Thank you for more information, I look forward to more deep dives into why bits bang the way they do ;)
@bonehelm
@bonehelm 2 года назад
Wow fascinating. Thanks!
@anon_y_mousse
@anon_y_mousse 2 года назад
I'm curious what you think of the additions to C coming in C23, constexpr might be interesting, and I kind of love #embed.
@5cover
@5cover Год назад
Const-qualified variables can't be interpreted as constant expressions as the value doesn't have to be provided at compile time. It is for locals for but not for parameters for example. void f(int const n) { // n is readonly, not constant. } int main(void) { f(rand()); } This compiles because regular values can be "promoted" to const.
@RobSwindell
@RobSwindell Год назад
int main(int argc, char **argv) { const int a = 10; *(int*)&a = 5; return printf("%d ", a); // no segfault ('a' is on the stack) }
@zildeos9858
@zildeos9858 2 года назад
this video was just amazing but it left me craving for a video about masking bits
@JacobSorber
@JacobSorber 2 года назад
I did a video about bitmasks a while back. You might check that one out. Of course, let me know if there's stuff you think I missed back then.
@zildeos9858
@zildeos9858 2 года назад
@@JacobSorber you bit I'll check them out (sorry i just couldn't resist)
@69k_gold
@69k_gold Год назад
C programmer: God why can't I have a language with a constant variable Meanwhile assembly programmers having an unspoken argument with 100 other programmers that none of them will change the value of a variable
@sanderbos4243
@sanderbos4243 2 года назад
Excellent video, thank you!
@Error_00101
@Error_00101 2 года назад
Maybe I am remembering wrong, but I think I used "volatile const" for inputregisters on my μController because writing to them would mess up a whole bunch of code and i had to specifically tell the compiler that it had to check every time (yes the compiler was very weak)
@JacobSorber
@JacobSorber 2 года назад
Yes, a read-only register would be a reasonable place to use a volatile const.
@maxaafbackname5562
@maxaafbackname5562 6 месяцев назад
Another case is variable that is changed in an interrupt service routine, but the code that uses the variable only reads the variable
@embeddedbastler6406
@embeddedbastler6406 2 года назад
7:38 Wouldn't that be the job of the loader? The compiler and linker put the constant in the read only section of the binary, but the actual protection of the region has to be done by the operating system (loader?).
@_Omni
@_Omni 2 года назад
yes, that is correct.
@JacobSorber
@JacobSorber 2 года назад
Well, it could be done by the OS (loader) or in the start-up code we get from the compiler and libC. I'm not sure if this varies by machine, but on the machine (Linux) I'm currently working on, the loader starts the process (using the execve system call) and then the new running program (libC) does a bunch of setup stuff before calling main. I just checked, and one of the things it does is call mprotect to make several sections of memory read only. I haven't looked at the libC code, but looking at the addresses, I'm guessing that one is the call stack and that another is the mapped section containing the read-only variables. So, yeah, this could happen in either place.
@styleisaweapon
@styleisaweapon 2 года назад
OS loader isnt that complicated. Hopefully it never is. You will get automatic write protection on your code segment. Thats about it.
@konnosgar
@konnosgar Год назад
This reminds me of my successful efforts to bring properties to C++ from Pascal.
@samuelmartin7319
@samuelmartin7319 2 года назад
I would love a video about “pages, offsets, and how that all works”!
@metal571
@metal571 2 года назад
Also makes me wonder how the behavior would change depending on where the compiler places static const variables...I think that's in bss rather than an rodata region?
@styleisaweapon
@styleisaweapon 2 года назад
I'm wondering how reliable the placement is depending on compiler switches also. Maybe when you optimize for size it put everything in one data segment, protections be damned, and so forth....
@ivanscottw
@ivanscottw Год назад
@7:40 something - (under linux) It has nothing to do with mprotect or mmap (well, actually mmap yes since that's how the elf loader uses an ELF binary) ! It's simply that the constant it located in a read-only segment in the ELF file (usually in the .text segment). And please, please please.. the issue is that you are using a feature of C which is specifically defined as "undefined" : you are breaking the strict aliasing rule ! (turning a const int * into an int *). In C it's as simple as this : symbol with the const qualifier should not and cannot be used as an Rvalue but only as an Lvalue ! Casting of symbols should only be applied to 'void *' - something used to move around opaque structure pointers. Also any reasonable compiler optimizer will just treat a const symbol as a literal if it knows the value. Note that using gcc (and to demonstrate it's VERY undefined.. ) : $ cat t1.c #include static const int A=1; int main() { int *b; b=(int *)&A; *b=2; printf("A=%d,%d ",A,*b); } $ gcc -O3 -Wall t1.c -o t1 $ ./t1 A=1,1 $ * It's obvious that through SSA optimization the C compiler understood we were talking about A all along and just discarded the *b=2 ! Looking at the generated x64 generated code it's just using literal 1 throughout ! $ gcc -Wall t1.c -o t1 $ ./t1 Segmentation fault $ * No optimization.. So it just went ahead.. and wrote to a protected segment ! Then you can make the symbol volatile - hilarity ensues ! Remember.. C is just a glorified assembler.. compilers will attempt to detect through its (somewhat heuristic) logic if you are trying to do something stupid - but you can always fool it ! If you *REALLY* want to shoot yourself in the foot using convoluted and obfuscating methods, it will eventually relent and let you (for example there are times you really intend to do something specific with a very specific compiler on a specific architecture and you know it's going to behave a certain way).
@danielwait779
@danielwait779 2 года назад
Please can someone give advice of how to effectively deprecate functions in a C library which is living in the wild? Love the videos btw, it's really helping me in my journey to become a more aware programmer.
@unperrier5998
@unperrier5998 2 года назад
I think you've confused more people with meddling with memory protection like this :) What I mean is people may think this is how it should be done, that "const" doesn't really mean constant and so it can be modified by finding the page base address and fiddling with its protection bits.
@JacobSorber
@JacobSorber 2 года назад
😅😂 Hopefully, they listen to the parts where I tell them not to.
@unperrier5998
@unperrier5998 2 года назад
@@JacobSorber I find it hazardous to convey this sort of subtle information. Too often people don't focus, they do something else and pick keywords here and there, glossing over the content, getting the gist with minimal effort and missing important details such as this one.
@31redorange08
@31redorange08 2 года назад
@@unperrier5998 Weird reasoning.
@unperrier5998
@unperrier5998 2 года назад
@@31redorange08 to the close minded it appears weird.
@31redorange08
@31redorange08 2 года назад
@@unperrier5998 To them probably as well, yes.
@redjr242
@redjr242 2 года назад
Really interesting, thanks! Would the compiler still put the variable in read only memory if it's volatile const?
@junbird
@junbird 2 года назад
While it is true that a #define can be redefined, that's only true statically. B would actually be constant at runtime, as something like "B = 7 "would not be a valid expression.
@anon_y_mousse
@anon_y_mousse 2 года назад
Unless you redefine it as something that's not constant. Let's say you want to read an error code in a library you implement, so you say, #define errno (*__errno_location()) and the function could be int *__errno_location ( void ); Just an oddball example.
@Spielix
@Spielix 2 года назад
Also it could still differ between different parts of your code.
@maxaafbackname5562
@maxaafbackname5562 2 года назад
The use of the const keyword does not say anything about the constness of an object. It just reflect the intention of the programmee/code not to change that object. If the compiler believes you, it can create an constant object for you (in for example read-only memory). We can have a const pointer to a non const object. You can pass either the address of a const object or the address of a non const object to a function that have a const pointer as argument. For example strlen() What should be the output of the following code: int a; const int *p = &a; a = 10; printf("%d ", *p); a = 5; printf("%d ", *p); Is this correct code? Or should p defined as: const volatile int *p; ? (Did not tried this code (yet).)
@sanderbos4243
@sanderbos4243 2 года назад
This is a good question. I didn't immediately get why your code might need volatile, but you're basically asking whether the compiler is allowed to replace the "const int *p = &a;" line with "const int *p = 0x42" (assuming "&a" equals "0x42"), right? I am not sure whether it'd be allowed to do that.
@maxaafbackname5562
@maxaafbackname5562 2 года назад
@@sanderbos4243 p is a pointer to a. a is a non const object. But p is defined as a pointer to a "const" object. That is perfectly fine as we do not change the object through p. But a itself is changed. How does the compiler know when dereferencing p that a has changed?
@programmertotherescue
@programmertotherescue 2 года назад
Yes this code is correct, and no, you don't need to use `const volatile int *p' for this. A conversion from T * --> const T * will always produce pointer that compares equal to the original one. From n1570 (Pointers): >> For any qualifier q, a pointer to a non-q-qualified type may be converted to a pointer to a q-qualified version of the type; the values stored in the original and converted pointers **shall compare equal**. (Equality operators): >> Two pointers compare equal if and only if both are null pointers, both are pointers to the **same object** [...] So both of them point to the same object, since in this case, none of the other conditions in the paragraph apply. And the address of `a' isn't suddenly going to change after the assignment, nor is `a' going to change into a different object. (Storage duration of objects): >> The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a **constant address**, and retains its last-stored value through its lifetime. [...]
@vectoralphaSec
@vectoralphaSec 2 года назад
I'm not a C programmer. I use C++ so a lot of C stuff I don't know. I watch this channel for its C++ centered content (of which there is very little of, we need more), but it's cool to passively learn about the C language too.
@anon_y_mousse
@anon_y_mousse 2 года назад
Although, it's good to be aware of additions to C which affect C++. Watch Jason Turner's latest video.
@styleisaweapon
@styleisaweapon 2 года назад
Once upon a time I would have said that knowing how C works inside and out would be beneficial for C++ -- but that was at a time when most implementations of C++ was still just as an advanced C preprocessor (fun fact: the pp in cpp originally stood for pre processor .. early implementations were a modification to... a stand alone C pre processor .. source file named cpp.c)
@marcotulioalvesdebarros518
@marcotulioalvesdebarros518 2 года назад
Hi Jacob. Could you please make a video about Red-Black Trees? I'm really struggling with it
@milasudril
@milasudril 2 года назад
What is the definition of a constant in native programming anyway? Mayne only values that only can get folded in as an immediate value in assembly. This means that constexpr in C++ does not define a constant, since it is legal to take the address of such an object. But a consteval (c++20) function is.
@JuusoAlasuutari
@JuusoAlasuutari Год назад
Trivia question: how do you express the decimal integer literal of value zero in C?
@sababugs1125
@sababugs1125 2 года назад
Question: would #define be faster than const since instead of retrieving data from the const variable , the machine has direct access to the value
@maxaafbackname5562
@maxaafbackname5562 2 года назад
That depends on the compiler (options). The compiler may optimize and treat it as an literal. As shown with the print parameter.
@anon_y_mousse
@anon_y_mousse 2 года назад
If the compiler doesn't know the value of the const, as in its defined in another file but declared in a header, then for that particular module it would be faster to have a macro, but if the compiler is aware of its value and you have optimizations turned on, then they'll likely be the same. The best reason to use const instead of #define is for debugging purposes and in general you might want to avoid macros unless you're doing something that can only be done with a macro.
@fabmilan_
@fabmilan_ 2 года назад
I'm really curious if the generated assembly would actually read from that memory address without needing to mark the variable as volatile if optimizations were turned off with -O0. Off to Godbolt, I suppose.
@fabmilan_
@fabmilan_ 2 года назад
Aaaand (spoiler) It doesn't work, it still moves the literal 10 directly into the register before calling printf. /z/dqnMqaWc9 Edit: Interestingly, in C++, the volatile trick no longer works as the generated assembly fetches the value of A from a sort of "data segment" (where strings are also stored) in which a bunch of labels are defined right after all the code. Only then did -O0 come into play in altering the value of the const variable, but it's still required to be declared as `volatile`, even though the value 10 is still stored in that same data segment in the binary, effectively wasting space. /z/TEsbv46v8 Great last couple of videos, they really clarified to me what const actually means in C. Would love to see a follow-up on C++'s constexpr keyword. tl;dr: don't undefined behavior I guess ¯\_(ツ)_/¯ Edit2: Had to remove the Compiler Explorer links as they kept getting this reply scrubbed by youtube.
@finmat95
@finmat95 2 года назад
Occam's razor: just let and keep the meaning const = constant.
@donaldmickunas8552
@donaldmickunas8552 2 года назад
That would be nice. Unfortunately, complexity seems to be preferred these days.
@marwankhaledkasem9335
@marwankhaledkasem9335 2 года назад
ممتاز شكرا لك👍
@escribe6751
@escribe6751 2 года назад
ahh so usually a readonly variable is actually stored in memory, it can be assigned by values that change during run time and constant variables are assigned at compile time and are not read from memory, and because C has no standardized behaviour for const in this case it is treating it as a constant value and in other cases at readonly values? did i get that right? sorry im still kinda confused by all that :D
@Spielix
@Spielix 2 года назад
The compiler can only treat the const variable as constant expression if it knows its value at compile time. So it depends on the definition of the variable being visible at compile time (same compilation unit/.c file) and it being a literal expression.
@styleisaweapon
@styleisaweapon 2 года назад
@@Spielix and that means the level of optimization also matters as to what form you get at the end of the compile -- the old strategy of concatenating all your C files into one file is an optimizatin switch these days (what does gcc call it? whole programs optimization or something? or is that microsoft?)
@Spielix
@Spielix 2 года назад
@@styleisaweapon I know it mostly as link time optimization (LTO).
@styleisaweapon
@styleisaweapon 2 года назад
@@Spielix Whatever its called, a large amount of it is implicit in most JITed languages today, such as C#. Its just something they can do, so they do.
@MithicSpirit
@MithicSpirit 2 года назад
is there some compiler flag to disable replacing const variables with their value?
@oscareriksson9414
@oscareriksson9414 2 года назад
You can redefine anything with meta programming 😀. Macros without redefine or undef, const or what ever actually. Make dll, have program call into that, recompile dll with new name and swap. Before compiling dll, look for the macro or const and change the value in the source file, with some parsing program, a pre pre processor if you will. Bam, changed during runtime of the program and not UB. TADA! Can be done by a keyboard press. Well, after you've made the meta stuff and the hot reloading. Super good debug/development iterative magic voodoo
@MyEpitt
@MyEpitt Год назад
When you print an address using %p, the output is something like 0xabcdH. Aren't the prefix 0x and the suffix H redundant? Don't they both mean hex?
@thebigmacd
@thebigmacd Год назад
In Microsoft land, 0x00000011b is a valid way of expressing a binary value. MS uses them in error codes sometimes. 0x00000011b is a printing error I found on Google.
@raffaeledelgaudio2724
@raffaeledelgaudio2724 Год назад
"Unless the program rewrite its code in Memory"... Is that really possibile for a program to rewrite its own code while running? It would be Amazing to see a video about
@JacobSorber
@JacobSorber Год назад
As with most computing questions, the answer is "it depends". But yes, in many cases a program can rewrite itself. I'll add that to the topic list and see what I can do.
@maxaafbackname5562
@maxaafbackname5562 6 месяцев назад
If the code is not placed in (real) read only memory, like ROM or flash, yes I think so
@jorgeferreira6727
@jorgeferreira6727 2 года назад
Meaning "#define MAX_X 10" doesn't use up memory, while "const int MAX_X = 10;", does. And, as it seems in a useless way. My vote is for the "#define". I know, I know, a handful of bytes doesn't even worth the mention when programming for a desktop with RAM measured in GBs. But go use unnecessary "const variables" like that in a small micro controller with a few KB of flash and almost one KB of RAM. Yes, yes, yes. You can get a micro with 3 or 4 times the amount of memory for a few more cents....... ......a few more cents multiplied by one million units a year during a 10 year product cycle, for example. My vote still goes to "#define" for avoiding "magic numbers" in the code and easy tuning of the program features. After all I also use a lot of "#define" statement in the main header file of my projects just for selective compilation of functionalities in the code. It makes modular testing and debugging way more easy than having to deal with a monolithic beast. Nevertheless, I think the "const" keyword deserves its own dedicated video because it has a lot of use cases that are not always that clear. And can cause noobs some headaches when dealing with libraries where it is highly used.
@Spielix
@Spielix 2 года назад
For selective compilation I would do the defines in the compilation command (Makefile) instead. So you do not need to change source code every time.
@jorgeferreira6727
@jorgeferreira6727 2 года назад
@@Spielix Yep. For selective/conditional compilation and even for special debug builds, it more flexible to have specific "make" configurations and include the defines there. But when its only a handful of constants to avoid magic numbers in the code, it goes in the special header file that I usually name "appConfig.h".
@Spielix
@Spielix 2 года назад
@@jorgeferreira6727 Sure, I just wanted the second part of your comment to be set into context for beginners.
@joevaghn457
@joevaghn457 2 года назад
Moral of the story: Don't use assembly if you need some akin to a "constant" value
@styleisaweapon
@styleisaweapon 2 года назад
In assembly, constants (EQU) really are constant because they are just literals with a name. That stuff in the const data segment on the other hand.... const is just a name too.
@joevaghn457
@joevaghn457 2 года назад
@@styleisaweapon Oof. When I said that I was thinking of things like DW, DB, DD, where you could define data in memory, and they memory could me modified by an instruction. True tho, I can't think of any way EQU could be manipulated
@MECHANISMUS
@MECHANISMUS 2 года назад
Even with 0 optimization would the compiler pass over the memory trick?
@CK113th121
@CK113th121 2 года назад
my windows pc gives this output, pretty cool a=100, &a=0xec065ffcf4 *p=500, p=0xec065ffcf4 length of int arr[a]=100
@bokunochannel84207
@bokunochannel84207 2 года назад
const != constant
@bokunochannel84207
@bokunochannel84207 2 года назад
either const as real constant or const as reference to immutable value
@meni181818
@meni181818 2 года назад
try to cast its address in the printf..
@parlor3115
@parlor3115 2 года назад
What about us C/C++ nerds? Will you listen to us and stop using C-style code in C++ programs?
@styleisaweapon
@styleisaweapon 2 года назад
thats a feature not a bug
@i007c
@i007c Год назад
cool
@johnwick7175
@johnwick7175 Год назад
So const is constant in pretty much all cases. :P
@MetricCrusader
@MetricCrusader 2 года назад
Anyone ever tell you that you look like the coolest System Lord?
@alpyre
@alpyre 2 года назад
In this video we watch C doing everything to make that const int a constant... yet people still can say "there are no constants in C". Geez!
@anon_y_mousse
@anon_y_mousse 2 года назад
It's because they're overly pedantic. Sure it sometimes matters what the ultimate meaning of something is, but at some point you just have to stop nitpicking and say "yep, it's a constant", because for all but exceptional circumstances, it really is.
@HinaraT
@HinaraT 2 года назад
It's not C doing everything to make it constant, it is your compiler trying to make it constant but without breaking compliance with C. That's like if on a plane I say "The LED be bright when the button is pressed", well what forbids me to make the LED always lit, it does satisfy my condition, it's dumb but it works, well that the same thing with most compiler, trying to match all the C standard, but when not clearly specified by it, doing everything to gain performance.
@donaldmickunas8552
@donaldmickunas8552 2 года назад
@@HinaraT Both the standards and compilers are created by people with all of the foibles inherent in such creations.
@JohnDlugosz
@JohnDlugosz Год назад
6:55 You are invoking Undefined Behavior. You did say not to try this on programs we care about, but really this might lead to bizarre results if you optimize.
@ElPikacupacabra
@ElPikacupacabra 2 года назад
So, basically, nowadays const means constant in C. Good to know!
@ivankryvosheia446
@ivankryvosheia446 2 года назад
Like your videos but don't really see the sense of it. Is this only in terms of philosophy?^)
@thebigmacd
@thebigmacd Год назад
Read the other comments. There are real-world implications for some applications. In microcontrollers, if you define a register address as a const, its value could indeed be different every time you read it due to hardware interaction. Just because it's a constant doesn't mean you can always assume its value doesn't change. It just means it is read-only from your program.
Далее
Understanding the For Loop (examples in C)
18:37
Просмотров 17 тыс.
ITZY 예지한테 AI 메이크업하기💖 #shorts
00:23
Is memcpy dangerous?
14:08
Просмотров 24 тыс.
The What, How, and Why of Void Pointers in C and C++?
13:12
How do I access a single bit?
11:07
Просмотров 21 тыс.
Compilers, How They Work, And Writing Them From Scratch
23:53
Malware Development: Processes, Threads, and Handles
31:29
Pulling Back the Curtain on the Heap
21:38
Просмотров 37 тыс.
Premature Optimization
12:39
Просмотров 825 тыс.
ITZY 예지한테 AI 메이크업하기💖 #shorts
00:23