Arrays in C++

Share
Embed
  • Published on Aug 19, 2017
  • Patreon ► patreon.com/thecherno
    Twitter ► thecherno
    Instagram ► thecherno
    Slack ► slack.thecherno.com
    Pointers ► thexvid.com/video/DTxHyVn0ODg/video.html
    Debugging ► thexvid.com/video/0ebzPwixrJA/video.html
    Series Playlist ► thexvid.com/p/PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb
    Gear I use:
    -----------------
    BEST laptop for programming! ► geni.us/pakTES
    My FAVOURITE keyboard for programming! ► geni.us/zNhB
    FAVOURITE monitors for programming! ► geni.us/Ig6KBq
    MAIN Camera ► geni.us/t6xyDRO
    MAIN Lens ► geni.us/xGoDWT
    Second Camera ► geni.us/CYUQ
    Microphone ► geni.us/wqO6g7K

Comments • 153

  • TheSim00n
    TheSim00n 2 years ago +55

    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?

    • Ali Başpınar
      Ali Başpınar 10 months ago +2

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

    • The Cherno
      The Cherno  2 years ago +60

      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. :)

  • hugopaq2
    hugopaq2 9 days ago

    there is another !

  • Mo Kassama
    Mo Kassama 20 days ago

    im sorry to say this but do i really need to see your face talking?

  • George Steven
    George Steven 24 days ago

    The memory option is not under my own debug. I'm using VS 2017

  • Nadnerb217
    Nadnerb217 2 months ago

    why not just use a #define to keep track of the size?
    #define EXAMPLE_SIZE 5
    int example[EXAMPLE_SIZE];

    • Peterolen
      Peterolen 2 months ago +1

      const int example_size = 5;
      int example[example_size];
      In C++17 you can use std::size to get the size of an array.

  • Gill Kabir
    Gill Kabir 2 months ago

    In declaration like this:
    int arr[5];
    arr is a pointer that points to the first element of the array arr. Therefore, arr must have its own memory address. However, value of arr is equal to address of arr i:e arr = &arr.
    Why?

    • ArthurGarreau
      ArthurGarreau 2 months ago +1

      Gill Kabir arrays are not pointers, when we write something like « int *ptr = arr », arr is implicitly converted to a pointer containing the address of the first element: &arr[0].

  • Talha Vlogs
    Talha Vlogs 4 months ago

    I like to live dangerously! 😎

  • Ondřej Martínek
    Ondřej Martínek 4 months ago

    Pure gold.

  • Fadope
    Fadope 5 months ago

    Why arent you using using namespace std; instead of std:: every time?

  • Gábor Králik
    Gábor Králik 5 months ago

    I used negative indexing once but it worked. Btw why are you writing it like std::cout

    • Peterolen
      Peterolen 5 months ago +1

      Accessing array elements out of bounds results in _undefined behaviour._ There is no guarantees what will happen. Some compilers add additional checks when compiling in "debug mode" to show an error if you go out of bounds. In "release mode" you normally don't want such checks because it is extra overhead that makes your program bigger and slower.

  • Mahek Shah
    Mahek Shah 6 months ago

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

  • mavihs26
    mavihs26 6 months ago

    How does anyone understand this i just have no clue on anything fml

    • Shpendulum
      Shpendulum 3 months ago

      Time and practice. These series are not meant for beginners though so if you are one then you may want to pick something different... to begin with :-)

  • nitin nair
    nitin nair 7 months ago +1

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

  • Flash KMG
    Flash KMG 7 months ago +2

    9:47 did you mean object?

  • Flash KMG
    Flash KMG 7 months ago +3

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

    • Detallado
      Detallado 3 months ago

      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)

  • Usama Hafeez
    Usama Hafeez 7 months ago

    if we write an array of 5 variables and then we access the 6th element and in the place of the 6th element there is some static const value. so will the const value be over written or would it prompt an error??

    • Peterolen
      Peterolen 7 months ago

      The C++ standard doesn't give any guarantees what will happen (this is what's called Undefined Behaviour). In debug mode (or when using a sanitizer like ubsan) it might end the program with an error message but in release mode there are usually no such error checks. The constant will probably just be overwritten but it might not behave consistently because the compiler could have made optimizations under the assumption that the constant does not change. This can for instance lead to getting the original value when using the constant directly while getting the modified value when reading the constant through a pointer.

  • Junior Jay
    Junior Jay 8 months ago

    why don't you use standard namespace??

    • Peterolen
      Peterolen 7 months ago +1

      @Luka Trdina The problem is name collisions which can lead to ambiguous function calls or accidentally calling the wrong function if you define your own function with the same name. It doesn't make your program bigger. The code still has to be compiled either way.

    • Luka Trdina
      Luka Trdina 8 months ago

      he's got a whole video on that, haven't watched it, but it's prob bc namespace std takes up a bunch of storage. It adds something like 130 thousand lines of code to your program when you build it

  • Janet Whiteman
    Janet Whiteman 8 months ago

    i love this the most! so many tips in here

  • Latiffesa Esa
    Latiffesa Esa 8 months ago

    can u cut down the background music

  • Zion
    Zion 8 months ago +1

    Me: *uses this for my final* muahaha

    • Calm Energy
      Calm Energy 4 months ago

      Whatever gets the job done! 🙂 Education is about learning not who the teacher is.

  • theturtlepwn
    theturtlepwn 9 months ago +2

    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 4 months ago

      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.

  • No Name
    No Name 9 months ago +51

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

    • omri1324
      omri1324 2 months ago +1

      lol this literally just happened to me aswell

  • Lillu
    Lillu 9 months ago

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

  • John Rhaenys
    John Rhaenys 9 months ago +1

    16:20 - What if I want to create an array (or a 2d array) that has the size given by the user? I keep getting an error ...

  • Chen Bai
    Chen Bai 9 months ago

    what is Entity?

  • Wayne Riesterer
    Wayne Riesterer 9 months ago

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

  • Matus Jakuboc
    Matus Jakuboc 9 months ago

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

  • SrNavi155
    SrNavi155 10 months ago

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

  • SrNavi155
    SrNavi155 10 months ago

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

  • Karmaindustrie
    Karmaindustrie 10 months ago

    did you make the outtro music by yourself?

  • xalspaero
    xalspaero 11 months ago

    it took us 31 videos to get to arrays!??

  • John Adams
    John Adams 11 months ago

    Since accessing an array is just an arithmetic operation (adding an offset to a base address) you can do something silly like saying 0[example] = 1; to set the 0th index of an array name example to 1, it would be stupid to do this but like you said you can do just about anything with C++, love the videos :)

  • g00d ViBeS
    g00d ViBeS Year ago +1

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

  • Adrians Netlis
    Adrians Netlis Year ago +1

    Hm... Isn't and then ! operations? Makes more sense to me. Not that it matters.

    • Peterolen
      Peterolen Year ago

      It depends on your hardware. I expect most architectures to have a special instruction for

  • femloh
    femloh Year ago

    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

  • PythonPlusPlus
    PythonPlusPlus Year ago +1

    Could you create a process to do garbage collection in c++?

  • PythonPlusPlus
    PythonPlusPlus Year ago +1

    Why don’t you declare everything in the stack. Why use the heap?

    • APaleDot
      APaleDot Year ago +2

      He gave the reason in the video. If you need to create an array within a function, then a stack-allocated array will be destroyed at the end of the function. So, if you need to use that array outside of the function, it must be heap-allocated.

  • Reid Pattis
    Reid Pattis Year ago

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

  • Clins Muleya
    Clins Muleya Year ago

    which editor are u using?

  • OneMeanDragon
    OneMeanDragon Year ago +1

    lua and vb6 starts with 1 lol

  • Victor Eijkhout
    Victor Eijkhout Year ago

    I disagree with teaching old-style arrays before std::vector. There is very little reason not to use vectors, and they make life so much easier. And dynamically allocated old-style arrays are memory leaks waiting to happen.

  • Sven D
    Sven D Year ago

    int example[20];
    int* p = example; //example is a pointer
    std::cout

  • Y S
    Y S Year ago

    Why no talk about vector

  • krisitak
    krisitak Year ago

    In practice you'd almost always use std::vector and no raw arrays. Yan didn't mention this, because the topic is about arrays and the vector is an array on steroids.

  • Black Ghost
    Black Ghost Year ago

    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.

  • hyk
    hyk Year ago

    Actually, standard arrays don't have an overhead. Only time they do bound checking is when you use the at() function. Only exra they have is the stored size variable.

    • Peterolen
      Peterolen Year ago

      The size doesn't need to be stored inside the objects because it's part of the type.

  • Attila Gyén
    Attila Gyén Year ago

    count should be done this way too int count = sizeof(arrayname) / sizeof(arrayname[0]); and if the element type has changed you don't have to change the sizeof(int) for example to double...

  • Mohit Kaintura
    Mohit Kaintura Year ago

    Thanks

  • Arbaaz Awan
    Arbaaz Awan Year ago +1

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

  • Akshay C.A
    Akshay C.A Year ago +1

    At 16:12, how making it a static fixed the problem? Thank you :)

    • Undac
      Undac Year ago +1

      He later used it to declare a static array
      int example[size]
      for which you have to know the exact size before declaring them. A static const variable is set during startup initialization and remains unchanged for the rest of the program. If he didn't do that and wanted to use raw pointers, he should have used int *example = new int[size];

  • Carl Jalal
    Carl Jalal Year ago

    Would that array created on the heap after the object instantiation get destroyed when the object is destroyed, or do we have to manually destroy it (for example in the destructor function) or else it would be a memory leak?

    • Peterolen
      Peterolen Year ago +2

      If you create sometihng with _new_ you always need to destroy it using _delete._
      ~Entity()
      {
      delete[] example;
      }

  • Bongo Cat
    Bongo Cat Year ago +2

    9:02 this is some wild shit

  • Bindhu raj
    Bindhu raj Year ago

    Thecherno please make a video about 2D array

  • Satyaki Ghosh
    Satyaki Ghosh Year ago +2

    talk slow please :3 (earnest request)

  • Colin Terry
    Colin Terry Year ago

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

  • Namah Shrestha
    Namah Shrestha Year ago

    Is it just me or is that a Stratocaster behind you

  • Anubhav Chauhan
    Anubhav Chauhan Year ago

    those blue eyes 😍😍

  • yaputra jordi
    yaputra jordi Year ago

    Owhh... So my life time problem in creating C++ array is all because I need to use "static" keyword !

    • yaputra jordi
      yaputra jordi Year ago

      Peterolen actually, I was wrong....

    • Peterolen
      Peterolen Year ago

      What does static got to do with arrays?

  • Polds Racka
    Polds Racka Year ago

    Well explained my friend. I will subscribe to be a Patron ;)

  • Anoop Suresh
    Anoop Suresh Year ago

    Can you comment the link containing your entire c++ series...I want the whole video's ...I am a beginner...