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

  Рет қаралды 14,362

Jacob Sorber

Jacob Sorber

Жыл бұрын

Patreon ➤ / jacobsorber
Courses ➤ jacobsorber.thinkific.com
Website ➤ www.jacobsorber.com
---
Clarifying about literals, macros, and const (still not constant?) // Last week's video about how a const int isn't a constant could have been more carefully worded, and it confused a few people (and frustrated others). This one clarifies a few things and tries to tease apart a few related concepts - literals, constants, macros, and constant integer expressions. I hope it helps.
Related Videos:
Last week's video: • A const int is not a c...
***
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...]

Пікірлер: 121
@jackkendall6420
@jackkendall6420 Жыл бұрын
I am literally constantly confused by literals and constants!
@reptilicusrex4748
@reptilicusrex4748 Жыл бұрын
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 Жыл бұрын
It's simple, page_address = address - (address % 4096) because a page is 4096 bytes.
@_Omni
@_Omni Жыл бұрын
@@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 Жыл бұрын
yeah, I'm all for it too!
@mk72v2oq
@mk72v2oq Жыл бұрын
@@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 Жыл бұрын
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.
@hansdietrich83
@hansdietrich83 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
That's already mentioned at 4:08
@SlideRSB
@SlideRSB Жыл бұрын
@@lennymclennington No it wasn't.
@lennymclennington
@lennymclennington Жыл бұрын
@@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 Жыл бұрын
@@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.
@darvil82
@darvil82 Жыл бұрын
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.
@harshitjoshi3082
@harshitjoshi3082 Жыл бұрын
Just read a few confusing comments about this somewhere and you made a video for it, great timing thanks 🌟
@suhel19690
@suhel19690 6 ай бұрын
wow...analysing const in such a depth...really good...❤
@udhayakumar8259
@udhayakumar8259 Жыл бұрын
defiantly Needed jacob, pages,offset,memory management..
@primosoma
@primosoma Жыл бұрын
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.
@portblock
@portblock Жыл бұрын
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!
@underdogrobot
@underdogrobot Жыл бұрын
Thank you for more information, I look forward to more deep dives into why bits bang the way they do ;)
@tusharsnn
@tusharsnn Жыл бұрын
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);
@konnosgar
@konnosgar 9 ай бұрын
Haha nice. Programming comedy, love it. You learn a lot while having fun, like all types of comedy.
@donaldmickunas8552
@donaldmickunas8552 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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.
@oching4
@oching4 4 ай бұрын
I love computers. Thank you for teaching me so many things, friend!
@greg4367
@greg4367 Жыл бұрын
Good job Jacob.
@sanderbos4243
@sanderbos4243 Жыл бұрын
Excellent video, thank you!
@bonehelm
@bonehelm Жыл бұрын
Wow fascinating. Thanks!
@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.
@danielwait779
@danielwait779 Жыл бұрын
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.
@redjr242
@redjr242 Жыл бұрын
Really interesting, thanks! Would the compiler still put the variable in read only memory if it's volatile const?
@samuelmartin7319
@samuelmartin7319 Жыл бұрын
I would love a video about “pages, offsets, and how that all works”!
@anon_y_mousse
@anon_y_mousse Жыл бұрын
I'm curious what you think of the additions to C coming in C23, constexpr might be interesting, and I kind of love #embed.
@embeddedbastler6406
@embeddedbastler6406 Жыл бұрын
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 Жыл бұрын
yes, that is correct.
@JacobSorber
@JacobSorber Жыл бұрын
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 Жыл бұрын
OS loader isnt that complicated. Hopefully it never is. You will get automatic write protection on your code segment. Thats about it.
@konnosgar
@konnosgar 9 ай бұрын
This reminds me of my successful efforts to bring properties to C++ from Pascal.
@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) }
@marcotulioalvesdebarros518
@marcotulioalvesdebarros518 Жыл бұрын
Hi Jacob. Could you please make a video about Red-Black Trees? I'm really struggling with it
@MithicSpirit
@MithicSpirit Жыл бұрын
is there some compiler flag to disable replacing const variables with their value?
@MECHANISMUS
@MECHANISMUS Жыл бұрын
Even with 0 optimization would the compiler pass over the memory trick?
@zildeos9858
@zildeos9858 Жыл бұрын
this video was just amazing but it left me craving for a video about masking bits
@JacobSorber
@JacobSorber Жыл бұрын
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 Жыл бұрын
@@JacobSorber you bit I'll check them out (sorry i just couldn't resist)
@Error_00101
@Error_00101 Жыл бұрын
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 Жыл бұрын
Yes, a read-only register would be a reasonable place to use a volatile const.
@maxaafbackname5562
@maxaafbackname5562 3 ай бұрын
Another case is variable that is changed in an interrupt service routine, but the code that uses the variable only reads the variable
@milasudril
@milasudril Жыл бұрын
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.
@metal571
@metal571 Жыл бұрын
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 Жыл бұрын
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....
@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.
@JuusoAlasuutari
@JuusoAlasuutari Жыл бұрын
Trivia question: how do you express the decimal integer literal of value zero in C?
@unperrier5998
@unperrier5998 Жыл бұрын
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 Жыл бұрын
😅😂 Hopefully, they listen to the parts where I tell them not to.
@unperrier5998
@unperrier5998 Жыл бұрын
@@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 Жыл бұрын
@@unperrier5998 Weird reasoning.
@unperrier5998
@unperrier5998 Жыл бұрын
@@31redorange08 to the close minded it appears weird.
@31redorange08
@31redorange08 Жыл бұрын
@@unperrier5998 To them probably as well, yes.
@benoitrousseau4137
@benoitrousseau4137 Жыл бұрын
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 Жыл бұрын
I agree.
@Spielix
@Spielix Жыл бұрын
C23 gets a less powerful (compared to C++) version of constexpr which should make this stuff way less messy.
@sababugs1125
@sababugs1125 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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.
@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.
@junbird
@junbird Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
Also it could still differ between different parts of your code.
@meni181818
@meni181818 Жыл бұрын
try to cast its address in the printf..
@fabmilan_
@fabmilan_ Жыл бұрын
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_ Жыл бұрын
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.
@maxaafbackname5562
@maxaafbackname5562 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
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. [...]
@marwankhaledkasem9335
@marwankhaledkasem9335 Жыл бұрын
ممتاز شكرا لك👍
@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).
@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
@escribe6751
@escribe6751 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
@@styleisaweapon I know it mostly as link time optimization (LTO).
@styleisaweapon
@styleisaweapon Жыл бұрын
@@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.
@vectoralphaSec
@vectoralphaSec Жыл бұрын
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 Жыл бұрын
Although, it's good to be aware of additions to C which affect C++. Watch Jason Turner's latest video.
@styleisaweapon
@styleisaweapon Жыл бұрын
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)
@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 3 ай бұрын
If the code is not placed in (real) read only memory, like ROM or flash, yes I think so
@CK113th121
@CK113th121 Жыл бұрын
my windows pc gives this output, pretty cool a=100, &a=0xec065ffcf4 *p=500, p=0xec065ffcf4 length of int arr[a]=100
@finmat95
@finmat95 Жыл бұрын
Occam's razor: just let and keep the meaning const = constant.
@donaldmickunas8552
@donaldmickunas8552 Жыл бұрын
That would be nice. Unfortunately, complexity seems to be preferred these days.
@oscareriksson9414
@oscareriksson9414 Жыл бұрын
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
@bokunochannel84207
@bokunochannel84207 Жыл бұрын
const != constant
@bokunochannel84207
@bokunochannel84207 Жыл бұрын
either const as real constant or const as reference to immutable value
@i007c
@i007c Жыл бұрын
cool
@joevaghn457
@joevaghn457 Жыл бұрын
Moral of the story: Don't use assembly if you need some akin to a "constant" value
@styleisaweapon
@styleisaweapon Жыл бұрын
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 Жыл бұрын
@@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
@parlor3115
@parlor3115 Жыл бұрын
What about us C/C++ nerds? Will you listen to us and stop using C-style code in C++ programs?
@styleisaweapon
@styleisaweapon Жыл бұрын
thats a feature not a bug
@johnwick7175
@johnwick7175 Жыл бұрын
So const is constant in pretty much all cases. :P
@jorgeferreira6727
@jorgeferreira6727 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
@@jorgeferreira6727 Sure, I just wanted the second part of your comment to be set into context for beginners.
@MetricCrusader
@MetricCrusader Жыл бұрын
Anyone ever tell you that you look like the coolest System Lord?
@alpyre
@alpyre Жыл бұрын
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 Жыл бұрын
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.
@TheRobbix1206
@TheRobbix1206 Жыл бұрын
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 Жыл бұрын
@@TheRobbix1206 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 Жыл бұрын
So, basically, nowadays const means constant in C. Good to know!
@ivankryvosheia446
@ivankryvosheia446 Жыл бұрын
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
Jacob Sorber
Рет қаралды 17 М.
Is memcpy dangerous?
14:08
Jacob Sorber
Рет қаралды 23 М.
39kgのガリガリが踊る絵文字ダンス/39kg boney emoji dance#dance #ダンス #にんげんっていいな
00:16
💀Skeleton Ninja🥷【にんげんっていいなチャンネル】
Рет қаралды 7 МЛН
ЧУТЬ НЕ УТОНУЛ #shorts
00:27
Паша Осадчий
Рет қаралды 7 МЛН
🤔Какой Орган самый длинный ? #shorts
00:42
تجربة أغرب توصيلة شحن ضد القطع تماما
00:56
صدام العزي
Рет қаралды 57 МЛН
Pulling Back the Curtain on the Heap
21:38
Jacob Sorber
Рет қаралды 36 М.
A const int is not a constant.
9:16
Jacob Sorber
Рет қаралды 66 М.
What's the Best Way to Copy a Struct in C and C++?
13:44
Jacob Sorber
Рет қаралды 33 М.
WHY IS THE STACK SO FAST?
13:46
Core Dumped
Рет қаралды 142 М.
The What, How, and Why of Void Pointers in C and C++?
13:12
Jacob Sorber
Рет қаралды 51 М.
Bit Fields in C. What are they, and how do I use them?
13:26
Jacob Sorber
Рет қаралды 80 М.
One second to compute the largest Fibonacci number I can
25:55
Sheafification of G
Рет қаралды 77 М.
The Inline Keyword in C.
16:18
Jacob Sorber
Рет қаралды 57 М.
Java in 100 Seconds
2:25
Fireship
Рет қаралды 1,2 МЛН
39kgのガリガリが踊る絵文字ダンス/39kg boney emoji dance#dance #ダンス #にんげんっていいな
00:16
💀Skeleton Ninja🥷【にんげんっていいなチャンネル】
Рет қаралды 7 МЛН