You Can Learn How to Use Arrays in C in 10 Minutes

Sdílet
Vložit
  • čas přidán 30. 12. 2020
  • In this video, I teach you how to write better C code that makes use of arrays. Arrays aren't scary! Learn dynamic and static arrays in C !
    Check out my other videos for Low Level Programming content on the C Programming language and others! Join us on Discord! / discord

Komentáře • 27

  • @filipcimo998
    @filipcimo998 Před 3 lety +39

    You forgot to use a free() function for deallocation your dynamically allocated memory, as You said, the size of allocated memory does not have to be known at compile time, that means the compiler during the compile time does not know how much dynamically allocated memory have to been deallocated at the end of process/application, then you have to do it explicitly. But really nice explanation of that how it works, keep doing this videos 😉

    • @LowLevelLearning
      @LowLevelLearning  Před 3 lety +26

      Yup! You're right, I'm already show casing bad coding practices xD thanks for letting me know.

    • @isuckatthisgame
      @isuckatthisgame Před 2 lety +5

      You can also do a check for dynamically allocated memory using function exit():
      if (pointer == NULL)
      exit(EXIT_FAILURE);
      so if your memory isn't allocated correctly (== NULL), you give exit function a constant "EXIT_FAILURE" to indicate failure
      and if you allocate memory in main() function, you can just write "return 1;" instead of using exit()
      I always do these checks, as well as freeing memory at the end of my program. Good habits :)

  • @thomashvnmusic
    @thomashvnmusic Před 2 lety +3

    I watched noumerous of your videos and i love the thorough explaining that you do. Please don't change your swag. Thanks for the great videos!

  • @flywittzbeats4008
    @flywittzbeats4008 Před rokem

    You bring up so many amazing points beyond your great explanation of arrays. Big love ❤

  • @sflux4593
    @sflux4593 Před 2 lety +7

    Great tutorial, although I think beginners may have a hard time identifying the difference. Perhaps you should have made the size a user input for the dynamic array.

  • @empowercode
    @empowercode Před 3 lety +3

    Hey! I just found your channel and subscribed, love what you're doing! Particularly, I like how clear and detailed your explanations are as well as the depth of knowledge you have surrounding the topic! Since I run a tech education channel as well, I love to see fellow Content Creators sharing, educating, and inspiring a large global audience. I wish you the best of luck on your CZcams Journey, can't wait to see you succeed!
    Your content really stands out and you've obviously put in so much thought into your videos! I applaud you for that and really wish you the best for the future!
    Cheers, happy holidays, and keep up the great work :)

    • @LowLevelLearning
      @LowLevelLearning  Před 3 lety +1

      Thanks for your kind comment!
      Good luck to you as well, and Happy Holidays!

    • @empowercode
      @empowercode Před 3 lety +2

      @@LowLevelLearning Yup, thank you!

  • @isuckatthisgame
    @isuckatthisgame Před 2 lety

    I learned to always cast return value of malloc to data type I'm allocating memory for. Malloc returns pointer of void type (void*) and conversion from void to any type works as well as vice versa, but that's just mine usual practice because in C++ it's important to specify and make this conversion.
    In this case I'd just put --> int *my_array = (int*)malloc(sizeof(int) * 5);

  • @BenvelMusic
    @BenvelMusic Před rokem +1

    I love your explanation it expands on what I learn in CS50X but honestly if I wasn’t familiar with arrays from David’s lecture I wouldn’t understand you.

    • @ToManyEndersGameFans
      @ToManyEndersGameFans Před rokem

      Tbf, he said you needed to have watched the first video or had familiarity with the syntax of C. So, this video is not for absolute beginners.

  • @antiresistance
    @antiresistance Před rokem

    when i did this on code blocks it returned the opposite static vs dynamic...any clue as to what could have happened

  • @purplenation3467
    @purplenation3467 Před rokem

    Can someone explain why when i print out the dynamic array it seems to have random values in it?

  • @pritesh_2564u
    @pritesh_2564u Před 2 lety

    In c++
    If you write my_array[1] or 1[my_array] both will give you the same output no error message going to be displayed
    But I not sure about c language

  • @navadeep.ganesh
    @navadeep.ganesh Před 2 lety +2

    Great tutorial!
    Will the dynamically allocated array be initialized to 0 always? In my case, its taking random values unless assigned. So how do we initialize malloc( ) spaces to 0?

    • @jimmycharlebois2340
      @jimmycharlebois2340 Před 2 lety +2

      you can memset(my_array, 0, sizeof(int) * 5)

    • @RingZeroLabs
      @RingZeroLabs Před 2 lety +1

      You can also use "calloc" instead of "malloc". Calloc will allocate the memory and zero the space at the same time.

    • @isuckatthisgame
      @isuckatthisgame Před 2 lety +1

      You can use calloc() which effectively does the same thing and only difference is that calloc() sets all bits of memory at 0, while malloc() doesn't do any sort of initialization.

  • @user-hd3pz2ow1b
    @user-hd3pz2ow1b Před 3 měsíci

    thanks

  • @gabehcuodsuoitneterp203

    I see everyone declare the incremented variable **i** right before the loop instead of doing it inside the loop itself. Is there some performance benefit to this?
    Great video 👍

    • @Sealedaway
      @Sealedaway Před rokem

      I’m a beginner so I don’t know for sure, but I’m gonna guess what the difference is. If you declare i only once, then the program doesn’t need to allocate memory for it everytime you want to use a for loop, giving it a *slight* speed boost. However, that means that the variable i lives throughout the entire scope of the main function and there needs to be room for it in memory constantly at run time. Declaring i inside the loop header means that i only lives within the for loop and the memory for it is freed upon exiting the loop. This means that the program needs to allocate memory for i everytime a loop is used, but on the flipside, i doesn’t occupy any memory except when it’s needed.
      So in summary, my guess is that declaring i once is slightly faster, while declaring i inside each loop is slightly slower but is slightly more efficient memory-wise. Although with the speed and size of RAM in devices nowadays, there probably isn’t any noticeable difference. Doing the former might be more convenient because there’s one less word you need to write in your for loop headers.

  • @skepticgoat7591
    @skepticgoat7591 Před rokem

    you set the pointer the pointer my_array as malloc, i mean... you set something as the action of allocating memory? how that works?
    "what is your value?"
    "memory allocation"
    wth

  • @skf957
    @skf957 Před 3 lety +1

    Thnaks for these tutorials, they are very well explained. I have two questions: You refer to it as "C". Are you using this as interchangable with the term C++? And secondly, re the array, from other high level languages I know this as a 2 dimensional array. Is there a 3 dimensional array available within C (or C++)?

    • @urbex9797
      @urbex9797 Před 2 lety +1

      C and C++ are different languages.

  • @kale.online
    @kale.online Před rokem

    That's a bracket, not parentheses. The difference between braces, brackets and parens was taught day 1