Arrays in C++

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ธ.ค. 2024

ความคิดเห็น • 351

  • @TheSim00n
    @TheSim00n 7 ปีที่แล้ว +259

    15:15 sizeof(int) is 4 bytes so you'd end up with 1? Were you reading your own comment while saying this or am I missing something?

    • @TheCherno
      @TheCherno  7 ปีที่แล้ว +288

      Lol yeah imma blame that on the jet lag... it would be 4 / 4 which is 1. Still not the size of the array though so the point still stands. :)

    • @alibaspinarele
      @alibaspinarele 6 ปีที่แล้ว +27

      @@TheCherno dont need to be smart to understand that, do not worry keep going :)

    • @andruha1067
      @andruha1067 4 ปีที่แล้ว +9

      Hey, I just watched this video after std::array and you contradicted yourself. Maybe because you didn't know it when you created this video but in this one you say there's an overhead due to size() but in std:: array video you pointed out that it's a template and does not add any overhead as it simply returns the value. Just wanted to make sure people watching this know that you actually recommended using std::array over c-style array due to many advantages.

    • @TheRealFFS
      @TheRealFFS 4 ปีที่แล้ว +5

      @@andruha1067 The overhead is the whole bounds checking and whatnot.

    • @TheMR-777
      @TheMR-777 4 ปีที่แล้ว +1

      @@andruha1067 I agree, but as @FFS said, yes, there are many bound checking, and other "CHECKS" which a bit slow down the performance of our code

  • @NoName-tn8rq
    @NoName-tn8rq 5 ปีที่แล้ว +342

    cherno: "We're gonna wrap this up pretty soon"
    me (moving mouse):"no, you're not"
    XD

    • @omri1324
      @omri1324 5 ปีที่แล้ว +14

      lol this literally just happened to me aswell

    • @martiananomaly
      @martiananomaly 3 ปีที่แล้ว +1

      same lol

  • @theturtlepwn
    @theturtlepwn 5 ปีที่แล้ว +210

    Part of what makes you a great programmer/teacher is that you have a deep technical understanding of how all of this works, and you're not afraid to share that information even though it may seem intimidating.

    • @Calm_Energy
      @Calm_Energy 5 ปีที่แล้ว +27

      Agreed: Sometimes it's tough to know what the audience already knows, I guess it's always good to error on the side of over explaining.

    • @sesetio338
      @sesetio338 2 ปีที่แล้ว +1

      That's how u know he's about that LIFE.

  • @julien-scholz
    @julien-scholz 7 ปีที่แล้ว +330

    "I like to live dangerously" Lmao

    • @jesusisc0mings00n3
      @jesusisc0mings00n3 3 ปีที่แล้ว +1

      @@thecashewtrader3328 I'm pretty sure Austin Powers said this before GTA SA

    • @Brahvim
      @Brahvim 3 ปีที่แล้ว

      _"Hey, Pikaju!"_

  • @roshiron1816
    @roshiron1816 4 ปีที่แล้ว +72

    TH-cam C++ videos are like looking up a word in a dictionary and the definition is full of words you don't understand so you look up those words and the chain continues. AAAAARRRGGHH!!!!!!! *slams head on desk*

    • @mwenge6355
      @mwenge6355 ปีที่แล้ว +4

      Bruuuuh. I know this was 2 years ago but I'm going through the same thing. I'm always checking the comments to see if people understand and surprisingly, they seem to. But maaaan!!!

    • @Byynx
      @Byynx ปีที่แล้ว

      ​@@mwenge6355That's normal for everyone that is starting, don't worry.

    • @TheNorthRemember
      @TheNorthRemember 9 หลายเดือนก่อน +1

      @@mwenge6355 I understand almost all of his videos, the reason is that I've been programming for almost 10 years now, so my advise is to practice video by video and look up some written docs of the same concept that you didn't understand from the video. good luck

    • @videogamesarecool9280
      @videogamesarecool9280 หลายเดือนก่อน

      this is actually part 30 of a series.
      theres a 106 episode playlist. the people who are getting it have been watching from the begining.
      you see a lot of people be confused in the virtual functions video because he introduces new concepts without eplaining them (virtual destructors, -> operator, new keyword, heap vs stack) but he has videos on all 4 of them so when you get to that video, you branch out and watch those 4 videos and then come back and you get everything

  • @femloh
    @femloh 6 ปีที่แล้ว +20

    You have made me finally UNDERSTAND pointers PROPERLY. I see their potential now. Its just you accessing memory locations and playing around with either those locations or the values in those locations. That is all pointers are !! They are not doing anything weird. Smh. Thanks man. You have no idea how clear this has made things for me. Makes we actually want to learn Machine/Assembly Language

  • @nitinnair1682
    @nitinnair1682 5 ปีที่แล้ว +72

    17:56 "however....i like to live dangerously"...XD

  • @g00dvibes47
    @g00dvibes47 6 ปีที่แล้ว +27

    Dude, phenomenal video.
    Love the technicality (pointer arithmetic (assembly OFFSET), heap alloc/dealloc, little endian, constexpr, c++11 arrays, etc)... great stuff

  • @Squash101
    @Squash101 5 หลายเดือนก่อน +2

    I'm absolutely loving this series, you should extend this series with a playlist on DSA, Arrays, LinkedList Stacks Queues BST Heaps, and then perhaps different types of algs, sorting and graphing algs

  • @axelandersson657
    @axelandersson657 7 ปีที่แล้ว +67

    5:16 Actually, less than or equal to is not performed separately, as (less than OR equal to) is the opposite of greater than, and vice versa. However, the code is more readable and more understandable if you write for (int i = 0; i < SIZE; i++) instead of for (int i = 0; i

    • @treyquattro
      @treyquattro 6 ปีที่แล้ว +22

      Correct. In this case the compiler will emit a JL (jump if less) or JLE (jump if less or equal) operation (on an x86 processor) for < or

    • @treyquattro
      @treyquattro 4 ปีที่แล้ว +4

      @Ebola Virus It may depend on how the emitted code is structured. In a very typical for loop you're counting up to some limiting factor so at the bottom of the loop you do the test and jump back to the top if the counting variable is less than the compared value. However, this is very dependent on a) the code that the programmer wrote (obviously) and b) how the compiler, optimizer and code emitter organize the object code. In the x86 world JNL is an alias for JGE (jump if greater or equal). These conditional jump instructions test flags in the flags register to determine whether to jump or not. They can test multiple flags simultaneously, such as the O(verflow) and S(ign) flags in the case of JL/JNL
      If you wrote *for (int i = 0; i < 5; i++)* then I'd expect the compiler to emit something like:
      for_loop:
      .
      .
      mov rax, [esp-8] ; i
      add rax,1
      mov [esp-8],rax ; changes no flags
      cmp rax, 5 ; sets CF,OF,SF,ZF,PF,AF flags
      jl for_loop ; jump if SF != OF
      nop
      In the case of *i < 5* the Sign Flag will be set but the Overflow Flag won't be and the jump will be taken. In the case of *i == 5* (the terminating condition) both the Sign Flag and Overflow Flag will be reset and the jump won't be taken and the CPU will continue at the NOP in this case.
      There's nothing stopping you writing code like this:
      loop_top:
      mov rax,[esp-8] ; i
      inc rax ; adds 1
      mov [esp-8],rax
      cmp rax,5
      jl loop_body
      jmp somewhere_else
      loop_body:
      ; do something
      jmp loop_top
      This is slightly less efficient, but the compiler (or hand-written assembler) might produce that kind of structure depending on size of code blocks, processor mode, etc. (in the "old days"(8/16-bit code), a conditional jump could only move the instruction pointer by -126/+129 so you'd have to reverse the sense of the conditional jump and use an immediate 16-bit jump e.g. if the loop code was longer than the distance allowed in the relative conditional jump.
      The only issue in the video was that Yan incorrectly said that writing e.g. *i

    • @canonpi
      @canonpi 4 ปีที่แล้ว

      but the computer does not understand that. it is why there exists a greater than or equal to operator

    • @Finkelfunk
      @Finkelfunk ปีที่แล้ว

      @@treyquattro Modern compilers won't emit a JL or JLE instruction, it will convert it into a JE instruction and compare it to 5 in this case to terminate the loop. Which makes sense considering it's just an XOR that has to be 0 as a result. Whereas if you use a JL or god forbid a JLE instruction it's a bitflip + 2s complement of the second value followed by an addition and subsequent interpretation of the result as a negative or positive number. If you compare sizes and whatnot it actually needs a lot more cycles to do so a modern compiler will omit that instruction. Check Godbolt if you doubt me.

    • @treyquattro
      @treyquattro ปีที่แล้ว +1

      @@Finkelfunk it's up to the compiler and optimizer how it organizes code - what tests and jumps it makes. There may well be optimizations to be made in the arrangements of what conditions to check, but etiher way a jump is going to be made either when the final condition is reached, or when an intermediate condition happens, depending on how the compiler has decided to lay out the code. The issue is that whether the compiler decides to emit JE, JL, JLE or something else (you may see aliases such as JC or JNAE for JB e.g.), the instruction timing is going to be identical - it's a test of a flag and a jump or not on result. Different flags get tested by different instructions but all Jcc (jump on condition code) operations (x86_64) have the same timing for the same sized operands. Saying check it on Godbolt is not very helpful because there are tens of different compilers on Godbolt, each with a variety of optimization settings. What gets generated differs from compiler to compiler, and version to version, and depending on the optimization levels chosen.
      The original issue was that it was mistakenly asserted that a

  • @Luingus
    @Luingus 2 ปีที่แล้ว +15

    So to summarize:
    Arrays store values of one type together
    The Array Name by itself is a memory address
    Arrays can be indexed through both for loops and pointer arithmetic
    Arrays can be made dynamically or statically (on the stack or the heap)

  • @informativecontent4778
    @informativecontent4778 6 ปีที่แล้ว +12

    9:02 this is some wild shit

  • @aPoCoTuToDac
    @aPoCoTuToDac 7 ปีที่แล้ว +48

    11:26 a pretty damn good example of *pointer* :D

    • @MrDe0
      @MrDe0 4 ปีที่แล้ว +1

      🤣🤣🤣

    • @17reason
      @17reason 3 ปีที่แล้ว

      😂😂🤫

  • @chimpionboy
    @chimpionboy 6 ปีที่แล้ว +4

    You're more than a teacher you're a perfect explainer. I wish if there's more like you or as good as you. I've been watching your series from last night and I'm just stuck on your channel. That's awesome.

  • @NaveenKumar-vj9sc
    @NaveenKumar-vj9sc 2 ปีที่แล้ว +2

    you are the best so far in my experience of learning online. In my perspective, you don't share knowledge, but rather EXPERIENCE.
    I am gonna now watch all the c++ experiences taught by you.
    Thank you soooo much.

  • @fulopboti
    @fulopboti 2 ปีที่แล้ว +3

    6:14 just a small correction for the great video, usually the C/C++ runtime knows the size of the arrays (malloc/realloc... other functions) and of course the operating system as well see VirtualAlloc, HeapAlloc, ... on Windows and even on windows the way it's kept differs between the type of allocators which are used (small/medium/large pool allocation)

  •  4 ปีที่แล้ว +3

    Please consider making C# videos..! The way you teach, express things is just amazing. I feel like I don't understand other people's teachings.

  • @pacan7380
    @pacan7380 4 ปีที่แล้ว +8

    The Cherno videos have become my morning routine :D

  • @richtourist
    @richtourist 7 ปีที่แล้ว +28

    At 16:13 you say "you have to mark it as STATIC". Why doesn't the CONST meen it is known at compile time (if it is constant), why the STATIC as well?
    You are easily the best C++ teacher I have seen, and your videos are perfect in style and content.

    • @vighnesh153
      @vighnesh153 4 ปีที่แล้ว +2

      @Peterolen Yea. because there is no 'readonly' in C++ like it is in C#. There const means compile time const and
      'readonly' means that it can be modified in the constructor.

    • @jukit3906
      @jukit3906 2 ปีที่แล้ว

      @@vighnesh153 I think the C++ spec recommends to use constexpr instead of const as const is more used as "readonly" now (or I might be mistaken maybe it was someone else lol)

    • @vika3750
      @vika3750 2 ปีที่แล้ว +4

      the const keyword is not necessarily known at compile time. It just means it can't be changed once it's initialized. For instance you can have the following:
      int i;
      std::cin >> i;
      const int x = i;
      and it is perfectly valid though the value isn't known at compile time. Therefore the compiler would have no way to know how much memory to set aside if you did int MyArray[x]
      Whereas constexpr or const static both must have constant values that are known at compile time.

    • @richtourist
      @richtourist 2 ปีที่แล้ว

      @@vika3750 Thanks for that, Vik A. 🙂
      Think I was just getting const and static confused for some reason.

  • @MegaFirst123
    @MegaFirst123 7 ปีที่แล้ว +9

    Yes Cherno, we want more long videos please!!

  • @mpwsh
    @mpwsh 7 ปีที่แล้ว +10

    I just finished the series in less than 1 week, thank you so much for this great information.

    • @9696Punk
      @9696Punk 7 ปีที่แล้ว +5

      2 days ;D

    • @suntzu1409
      @suntzu1409 3 ปีที่แล้ว +3

      @@9696Punk yall are too powerful to be kept alive

    • @scrumpus8938
      @scrumpus8938 3 ปีที่แล้ว

      @@9696Punk 8 hours a day???????

  • @roberthoople
    @roberthoople 3 ปีที่แล้ว +5

    These are some of the most useful programming videos on the internet because of your teaching style and structure.
    First thing that makes you a cut above the rest, is because you cut straight through the bullshit. ie: A variable type is actually nothing special. ie: an array is actually just a small stack of variables, and is sort of actually nothing special.
    Second thing: The memory viewer. Mind blown! For as long as I've been trying to learn these concepts (and bowing before variable types with a cult like religious reverence), I haven't found any instructor that actually does this. Instead every tutorial or course (I've taken) leaves this sort of thick fog between memory and what we're trying to achieve. As if memory is this magical thing we must never look upon or speak the name of, except in anything but convoluted riddles. Even in my Computer Science 104 course in college they taught us the hardware concepts of memory on the whiteboard, but never even attempted to connect it's fundamental truths to the code side of things. Makes me feel like Memory viewing should really be a part of the introduction, instead of being left only until advanced concepts.

  • @1Naif
    @1Naif 7 ปีที่แล้ว +14

    I've Learned new things today... Thanks.

  • @1st_ProCactus
    @1st_ProCactus 7 ปีที่แล้ว +10

    Playing with addressable LED's with an arduino, Ive seen some weird things going on when I accidentally misuse an array. I had no idea what was going on, LED's doing strange things. I did work out that arrays still try to work, I assumed it just used the memory as if it was bigger. Got some cool effects from it.
    I look forward to the spin offs as you say :)

  • @marcusrigonati7784
    @marcusrigonati7784 4 ปีที่แล้ว +4

    I started playing with arrays, just to remember how it works and found out that you can change a private atribute of a class (even out side of its scope) using pointers, nice >

  • @RogerTannous
    @RogerTannous 4 ปีที่แล้ว +6

    16:14 As the size variable should be known at compile time, you've stated that it should be static const (or constexpr). It can also be declared in a #define statement, and easily remembered, like in
    #define ARRAY_SIZE 5
    int example[ARRAY_SIZE];

    • @yusinwu
      @yusinwu 2 ปีที่แล้ว

      Yes, that's exactly what I'm thinking about. Is it okay to write macros in-line with your code? Or is it better to just put all the macros on top of the soruce code?

    • @mastershooter64
      @mastershooter64 ปีที่แล้ว +1

      oh no bro macros are evil (because they literally just replace text in your code) avoid them at all possible costs, use them only if it's absolutely necessary

  • @nimazahed6380
    @nimazahed6380 23 วันที่ผ่านมา

    Let just say I just wanted to learn pointers here and I watched 30 videos. And I learned 10 different things and now I'm excited I learned something new and I'm sure I'll watch all the videos.

  • @ashishbab887
    @ashishbab887 3 ปีที่แล้ว +1

    In a video of more than 15 min, I usually overwhelmingly check the remaining time. But with Cherno, time flies.

  • @austinfritzke9305
    @austinfritzke9305 4 ปีที่แล้ว +1

    You bring up some basic yet meaningful concepts that other introductory programming texts/videos don't. like why < is better than

  • @TheSmoothPower
    @TheSmoothPower 4 ปีที่แล้ว +1

    Hey man, you are amazing. YOU ARE THE C++ REFERENCE

  • @jackle3002
    @jackle3002 3 ปีที่แล้ว +8

    Hey The Cherno, every so often i see you doing some fancy keyboard combo magic which resembles VIM operations like selecting chunks of code quickly, moving line up or down, duplicating etc. I'd be pretty interested in learning how you've set that up and knowing more about your input "workflow". If you've already done a video on this please link me :)

  • @nickknight5373
    @nickknight5373 4 ปีที่แล้ว +1

    Very thoughtful introduction - many thanks.

  • @user-cx7te6pl2h
    @user-cx7te6pl2h 4 ปีที่แล้ว +1

    God Bless you. Put more adds on this baby, you deserve money

  • @fulopboti
    @fulopboti 2 ปีที่แล้ว

    15:15 the sizeof(pointer) for Visual C/C++ will allways be equal to sizeof(int), because these should be equal to the size of the registers are used in order to not create bottlenecks in CPU operations => the sizeof(pointer)/sizeof(int) will always be 1 for VCRT

  • @dopetag
    @dopetag 7 หลายเดือนก่อน

    You went quite deep in this one. Thanks.

  • @kurt7020
    @kurt7020 2 ปีที่แล้ว +1

    Me: Struggling to get the element count of a heap allocated array and failing miserably.
    The Cherno: It's not really possible in C++*.
    Me: Suddenly feeling less incapable.

  • @colinterry7261
    @colinterry7261 6 ปีที่แล้ว +1

    I like these longer videos where you really go in depth!

  • @mohamedguebli2522
    @mohamedguebli2522 4 ปีที่แล้ว +2

    15:10 i think the size of an integer pointer is 8 bytes and not 4 bytes !

  • @lucasjoseserena2473
    @lucasjoseserena2473 3 ปีที่แล้ว

    Cherno sos lo mas grande que hay.

  • @humbledcomposer
    @humbledcomposer 9 หลายเดือนก่อน

    Thanks for this!
    By the way, you REALLY talk with your hands!!

  • @autumnleaf8483
    @autumnleaf8483 2 ปีที่แล้ว +1

    Can somebody tell what NDNS is(on 12:30)?

  • @mishimihendrix
    @mishimihendrix 4 ปีที่แล้ว +1

    8:40: That is a proper "pro" move. Nice

    • @valizeth4073
      @valizeth4073 3 ปีที่แล้ว

      That's a proper "I invoke UB because I don't care about my code". Nobody sane in the industry would ever deliberately invoke undefined behavior, that assignment might as well make your PC order pizza for you.

  • @zerospeed6412
    @zerospeed6412 4 ปีที่แล้ว +7

    Me watching this video hoping to clear all confusion just to be greeted by more confusion... Guess I'm on my own now.

  • @ultimatedragon4281
    @ultimatedragon4281 2 ปีที่แล้ว +1

    15:55 Okay, that is interesting. That actually worked for me (no errors, compiled and executed fine) XD. Weird...

  • @alexshepler403
    @alexshepler403 7 ปีที่แล้ว +1

    Love these videos. This is helping me out in my cs131 class at evcc

  • @GenericPhantom1
    @GenericPhantom1 2 ปีที่แล้ว

    I watched neatly a third of this series. Insane stuff. I'm gonna start trying to mod Open Roller Coaster Tycoon 2 to actually put my skills to use.

  • @cap-advaith
    @cap-advaith 2 ปีที่แล้ว +1

    where the hell was this series ? such a golden playlist

  • @somerandomprogrammer6886
    @somerandomprogrammer6886 4 ปีที่แล้ว

    for using the value of std::array::size() in for loops, you've got to change the data type to signed integer or either declare the variable for the initial value as an unsigned integer

    • @betareleasemusic
      @betareleasemusic 3 ปีที่แล้ว

      the "perfect" solution would probably be to use size_t for the type of the iterating variable, because by definition size_t "can store the maximum size of a theoretically possible object of any type (including array)"
      also, you can just do a ranged for over the array if it is only the elements that you need

  • @harshuldesai8901
    @harshuldesai8901 3 ปีที่แล้ว +1

    16:14, why do you mark it as static?

  • @marco_gallone
    @marco_gallone 3 ปีที่แล้ว

    9:35 “I’m going to wrap this up pretty soon”
    But I do like the lengthier video though

  • @mickyyang6652
    @mickyyang6652 2 ปีที่แล้ว

    3:23 Hey! It's not a pointer.
    An array will be implicitly converted into a pointer to its first element, but it's not a pointer.
    Pointers store address, and arrays store their elements, so they're definitely different types. If you get the address of a pointer (`&ptr`), the result is a pointer to the pointer; in the other hand, if you get the address of an array, the result is a pointer to the array.
    As what you saw, in the memory, there are five integers instead of a "base address".

  • @mohnish.deshpande
    @mohnish.deshpande 4 ปีที่แล้ว +1

    Mate you are just great!!!
    Especially because you teach everything clearly and quickly 😎

  • @davidcfrogley
    @davidcfrogley หลายเดือนก่อน

    Another reason to allocate on the heap is when you don't know at compile time how big you need your array to be (e.g., an array whose size is determined by user input).

  • @matusjakuboc9623
    @matusjakuboc9623 6 ปีที่แล้ว +1

    Thanks! By watching this series i improved my programming skills in c++ A LOT. I hope you will keep doing such a videos

  • @veeranjineyuluvaggaiahgari6889
    @veeranjineyuluvaggaiahgari6889 4 หลายเดือนก่อน

    for loop and arrays are best friends forever

  • @damienkubik1380
    @damienkubik1380 4 ปีที่แล้ว +1

    Another great video, way easier to understand then my university teaches.
    Thanks

  • @Finkelfunk
    @Finkelfunk ปีที่แล้ว

    5:23 I checked the compiler explorer and it is in fact not correct that it will do a less than and equals comparison. Basically, compiler magic and all that optimizes it away, so no matter how you write it, it always uses a "je" instruction. But it is definitely more readable to anybody working with your code.

  • @krec348
    @krec348 7 ปีที่แล้ว +6

    Yes!! We're finally getting to more interesting topics, WOOOOOOOOOO

  • @kevtheuseless
    @kevtheuseless 4 ปีที่แล้ว +2

    3:40 Long live negative indices of Python!

    • @suntzu1409
      @suntzu1409 3 ปีที่แล้ว

      Python has negative indices?

  • @serkanozturk4217
    @serkanozturk4217 2 ปีที่แล้ว

    Personal Notes:
    - there is no array index check in c++
    - Pointer arithmetics is done automatically according to the type the pointer points to ( ex: int arr[5]; *(arr+2) accesses the 2nd element)
    - Stack: int arr[5]; heap: int* arr = new int[5]
    - Why would i need dynamic allocation? One simple example is to be able to return an array defined inside a function
    - Since C++17, std::array has been introduced which does bounds checking and knows its size
    - Didn’t get why array size must be declared as static const at 16:15

    • @bhavneeth
      @bhavneeth ปีที่แล้ว +1

      I'm leaving this here in case anyone still has any questions.
      - Using a const variable to specify the size of an array inside a class is not allowed because arrays must have a size that is known at compile-time.
      - The size of an array must be a constant expression, meaning it must be able to be evaluated at compile-time, so that the compiler knows how much memory to allocate for the array.
      - On the other hand, const variables inside a class can only be initialized with a value that can be evaluated at runtime.
      - This is because the value of a const variable inside a class is not known until the object is constructed, which is too late to specify the size of an array.
      - In order to use a const variable to specify the size of an array inside a class, you should use a static const variable.
      - This is because static const variables are shared by all objects of a class, so their value is known at compile-time.
      - Another benefit from doing this is that you use a non-static const int to set the size of the array, each object will have its own copy of the size, which is wasteful and unnecessary.
      -By using a static const int instead, the size of the array will be shared among all objects of the class and will have a single storage location in memory
      If you have any confusion regarding what static and non-static is please refer to Cherno's video on the topic

  • @thepanable5004
    @thepanable5004 7 ปีที่แล้ว +1

    Daddy Cherno feeding us more videos

  • @mvanditha07
    @mvanditha07 4 ปีที่แล้ว +2

    "However, I like to live dangerously" xD xD

  • @sleepyNovember_project
    @sleepyNovember_project 9 หลายเดือนก่อน

    11:26 nice pointer

  • @zhenzhang3451
    @zhenzhang3451 ปีที่แล้ว +1

    best online c++

  • @philippjungkamp3760
    @philippjungkamp3760 4 ปีที่แล้ว +1

    5:18 you said a "

  • @ILoveSoImAlive
    @ILoveSoImAlive 3 ปีที่แล้ว

    Most funny things do happen, if your index variable lies nearby your array in the memory.
    Suddenly you start to overwrite the index with some value, which belongs into the array. If that happens in a loop, you will, perhaps, never be able to leave this array-hell.
    That happend to my team-leader in a real-time, safety regarding, embedded project.

  • @suchetapal713
    @suchetapal713 3 ปีที่แล้ว

    At 16:30, why is static const used ? Why only const won't work?

  • @foxinio4225
    @foxinio4225 4 ปีที่แล้ว +1

    If for example I have dynamic array of class instances that all need to call destructor, because class contains a pointer, when I call delete[]array will it call destructor for each one instance or do I need to do it separately before calling delete[]?

  • @sebnemkalkan3312
    @sebnemkalkan3312 5 ปีที่แล้ว +2

    15:19
    int count = sizeof(example) / sizeof(int) ------This does not give us zero; the result of this is : 2
    int count = sizeof(*example) / sizeof(int[5]);-----This gives zero

    • @侯汶政
      @侯汶政 5 ปีที่แล้ว +1

      wow it is 2.

    • @felipevillamil8454
      @felipevillamil8454 4 ปีที่แล้ว +2

      maybe it is because you have a 64 bits application so the size of the pointer is actually 8 bytes.

    • @sebnemkalkan3312
      @sebnemkalkan3312 4 ปีที่แล้ว

      gave up coding!

  • @koungmeng
    @koungmeng 5 ปีที่แล้ว +2

    how about :
    std::vectorarr(5)
    std::cout

    • @Reinaldulin
      @Reinaldulin 5 ปีที่แล้ว

      In that case you're initializing the vector class, and the parameter 5 is its size
      so std::array myArray is equal to std::vector myArray(5)

    • @anonymoussloth6687
      @anonymoussloth6687 3 ปีที่แล้ว

      Vectors are made on the heap

  • @mohammedobad2174
    @mohammedobad2174 7 ปีที่แล้ว +1

    great stuff cherno

  • @JonathanKila
    @JonathanKila หลายเดือนก่อน

    @7:28 I assume then that ptr is a double pointer in this context?

  • @SrNavi155
    @SrNavi155 6 ปีที่แล้ว +2

    Is there a video from this channel that explains associative arrays/hashmaps/dictionaries ?

  • @mahekshah9337
    @mahekshah9337 5 ปีที่แล้ว

    concept is tricky in example code but you made it simple . THANKYOU

  • @luisamaria172
    @luisamaria172 4 ปีที่แล้ว +3

    Chorando rios vendo o vídeo todo em x0,5 pq ele é o melhor professor que eu encontrei mas fala inglês🤡

  • @diego_mellizo
    @diego_mellizo ปีที่แล้ว

    There are also std::size() and std::ssize() to retrieve the size (or count of elements) of a raw array (although they work for standard arrays as well), I don't know if these methods were there by the time this video was made but if I'm not wrong they seem to be the safe way to do it now.

  • @codastudiode
    @codastudiode 4 ปีที่แล้ว +2

    Cherno, thanks for your awesome videos! could you please make a tutorial series about game programming patterns.

  • @redaagourram2514
    @redaagourram2514 4 ปีที่แล้ว

    Bro , all what i can say is thank you
    keep up buddy

  • @mkn.567
    @mkn.567 3 ปีที่แล้ว

    Way to flip us all off at 11:27.

  • @arbaazawan
    @arbaazawan 6 ปีที่แล้ว +1

    Dude! you are superfast most of the times i have to stop the video and play it in slowmo

  • @hamzamameche3893
    @hamzamameche3893 3 ปีที่แล้ว

    16:14 you added static because it could've been changed at the constructor otherwise?

  • @jianhe
    @jianhe 2 ปีที่แล้ว

    9:12 is really wild.

  • @remak6342
    @remak6342 4 ปีที่แล้ว

    another EXCELLENT video

  • @mohamedtalaatharb2441
    @mohamedtalaatharb2441 7 ปีที่แล้ว +2

    @10:53 does this imply that a language like Java will have this overheads built into the language and there is no way around them?

    • @Puffadderr
      @Puffadderr 7 ปีที่แล้ว +1

      It's called garbage collector. It takes care of deleting objects for you. There might be way to take care of deleting your objects yourself. Not sure, i'm not java expert, but there is no reason really to do it in Java. If you really need the performance gain from dynamic memory allocation, Java isn't right tool to use, C++ is.

    • @TheCherno
      @TheCherno  7 ปีที่แล้ว +3

      Basically yes.

    • @owiley2002
      @owiley2002 7 ปีที่แล้ว +1

      Yes but depending on what you are doing you can try different tricks. Also not all languages are created equal and some are better at different task.

  • @qwertygamer8386
    @qwertygamer8386 7 ปีที่แล้ว +1

    It helped me a lot for my exam

  • @imereque
    @imereque 3 ปีที่แล้ว

    Hi Cherno,
    Correct me if I'm mistaken, but are '

  • @TheNorthRemember
    @TheNorthRemember 9 หลายเดือนก่อน

    8:15 what exacty are we adding to the ptr (ptr+2) , does it mean move forward two the size of the TYPE int which is 8 bytes ? ... i know what example[2] means, but ptr +2 !!

    • @TheNorthRemember
      @TheNorthRemember 9 หลายเดือนก่อน

      haha just 5 more seconds and you explained it exactly the way I understood it, I love myself at such moments. thanks cherno

  • @skdjypoebuddy7199
    @skdjypoebuddy7199 9 หลายเดือนก่อน

    the sad thing is i cant tell if youre joking when you say "i dont wanna make this too complicated" XD
    thank you for these free tutorials

  • @anonymousbelgique7331
    @anonymousbelgique7331 2 ปีที่แล้ว

    thank you very much for this information you teached me

  • @fanzhou7193
    @fanzhou7193 ปีที่แล้ว

    Great explanation

  • @aquadap219
    @aquadap219 ปีที่แล้ว

    you are really great teacher cherno thank you a lot!

  • @reidpattis9478
    @reidpattis9478 6 ปีที่แล้ว +1

    Great video. Still, I think this video would make more sense to be the 7th video on the playlist.

  • @francoocampo5286
    @francoocampo5286 3 ปีที่แล้ว

    just incredible. thank you

  • @DiamondShocked
    @DiamondShocked 4 ปีที่แล้ว

    5:22 This will probably not be a performance hit. Almost all common ISAs have a single instruction for "jump if less than or equals" that takes the same number of cycles as "jump if less than". (In hardware, the addition of the "equals" check likely has almost no impact on the critical path, since both the less than and equals checks can be done in parallel and the combination is a simple OR)

  • @gihanna
    @gihanna 3 ปีที่แล้ว

    Thank you very much!

  • @AgentM124
    @AgentM124 7 ปีที่แล้ว +62

    I am element 0 on this array

    • @joshs2475
      @joshs2475 7 ปีที่แล้ว +7

      I can't lie that was a good one...

    • @AgentM124
      @AgentM124 7 ปีที่แล้ว +21

      Josh Stephenson had to pointer that out.

    • @romanemul1
      @romanemul1 7 ปีที่แล้ว +5

      thisarray[0] = myname;

    • @Jjauuza
      @Jjauuza 5 ปีที่แล้ว +1

      Josh should have said "first" lol

  • @Lillu700
    @Lillu700 5 ปีที่แล้ว +1

    You could use pointer arithmetic to loop through your 50 individual integers.

  • @WayneRiesterer
    @WayneRiesterer 5 ปีที่แล้ว +1

    Are raw arrays typically used in gaming where performance is key?

  • @HappyMatt12345
    @HappyMatt12345 ปีที่แล้ว +4

    Couldn't you also specify a set of values when you create the array rather than use a for loop to define their values?
    For example int myArray[5] = {1, 2, 3, 4, 5};
    It would be more efficient to use a for loop if you have a large array, but if there are only a few elements it's quicker to write it this way in one line than to write an entire for loop imo.

  • @iwolfman37
    @iwolfman37 2 ปีที่แล้ว

    So I googled how to find the size of an array because I needed it for a code I was working (note, I'm very much still a beginner when it comes to coding), and while one of the methods was that method you showed of doing arrSize = sizeof(arr) / sizeof(value), another way which was mentioned was this way that I prefer but don't fully understand which uses pointers. The code for this is arrSize = *(&arr + 1) - arr. I didn't use this inside of a function, but my guess is it would still work inside of a function considering that it uses pointers and addresses of. Like I said, I don't fully understand the code, only that it works for finding the size of the array, so if anyone could explain it in detail, I know how pointers work, and I know about addresses of, I just don't understand this specific way of finding the size of an array.