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

Поделиться
HTML-код
  • Опубликовано: 25 ноя 2024

Комментарии • 30

  • @filipcimo998
    @filipcimo998 3 года назад +41

    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 😉

    • @LowLevelTV
      @LowLevelTV  3 года назад +27

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

    • @isuckatthisgame
      @isuckatthisgame 2 года назад +7

      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 2 года назад +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!

  • @sflux4593
    @sflux4593 2 года назад +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.

  • @BenvelMusic
    @BenvelMusic 2 года назад +2

    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 Год назад

      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.

  • @isuckatthisgame
    @isuckatthisgame 2 года назад

    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);

  • @flywittzbeats4008
    @flywittzbeats4008 Год назад

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

  • @empowercode
    @empowercode 3 года назад +4

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

    • @LowLevelTV
      @LowLevelTV  3 года назад +2

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

    • @empowercode
      @empowercode 3 года назад +2

      @@LowLevelTV Yup, thank you!

  • @navadeep025
    @navadeep025 2 года назад +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 2 года назад +2

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

    • @RingZeroLabs
      @RingZeroLabs 2 года назад +1

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

    • @isuckatthisgame
      @isuckatthisgame 2 года назад +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.

  • @pritesh_2564u
    @pritesh_2564u 2 года назад

    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

  • @skf957
    @skf957 3 года назад +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 2 года назад +1

      C and C++ are different languages.

  • @gabehcuodsuoitneterp203
    @gabehcuodsuoitneterp203 2 года назад

    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 2 года назад

      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.

    • @qwerte6948
      @qwerte6948 3 месяца назад

      ​@@Sealedaway most for loops compile to LOOP instruction that will use ECX register as refrence for number of iterations, the value of *i* either lives there or it is pushed to the stacked when ECX is needed for some instruction than poped back in... however most compilers optimise vode nowdays and likely see *i* isnt refrenced anywhere after the for loop (outside of other for loops) and would just not track it for you...

    • @Sealedaway
      @Sealedaway 3 месяца назад

      @@qwerte6948 Yeah since that comment I’ve learned a lot more about compilers and just how much they often assume and intuit. In an overwhelming number of cases the only difference in the end result is just a shorter loop header. I guess it also has the benefit of never accidentally using the wrong integer type. For example if you’re looping through potentially huge arrays multiple times, accidentally typing int instead of size_t in a loop header could be disastrous, while declaring the iterator as size_t once will make sure it always has enough capacity. So in short, the performance will be the same except maybe for a few hyper specific cases, and the benefits are mostly just being slightly more concise and communicating intent.

  • @antiresistance
    @antiresistance 2 года назад

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

  • @skepticgoat7591
    @skepticgoat7591 2 года назад

    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

  • @kale.online
    @kale.online Год назад

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

  • @purplenation3467
    @purplenation3467 Год назад

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

    • @Wutheheooooo
      @Wutheheooooo 29 дней назад +1

      So they are called "garbage", which are trace of old memory that used to store there, malloc() only tell that this memory region is in use, it doesn't initialize it to 0 or any values, that's your responsibility, you can use calloc which does but generally slower.

    • @purplenation3467
      @purplenation3467 29 дней назад

      @Wutheheooooo Ohhh I see that makes sense thank you so much