How to use realloc in C

Поделиться
HTML-код
  • Опубликовано: 25 май 2023
  • Source code can be found here:
    code-vault.net/lesson/707793f...
    ===== Support us through our store =====
    code-vault.net/shop
    ===== Check out our website =====
    code-vault.net
    ===== Check out our Discord server =====
    discord.code-vault.net

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

  • @Bing.Chillin
    @Bing.Chillin 11 месяцев назад +11

    Man I wish I had your videos 15 years ago

  • @NOPerative
    @NOPerative 11 месяцев назад +5

    The "trouble-shooting" discussions frame out the topic (realloc) excellently; addressing segfault and possible memory leaks in easily digestible examples and terminology as well as correctly implementing interfacing preventing and managing the problems was concise. As usual, awesome job.
    Good vid.

  • @zyghom
    @zyghom 11 месяцев назад +2

    I simply LOVE your videos about C - thank you

  • @bjornnordquist1616
    @bjornnordquist1616 11 месяцев назад +1

    That explains the pesci warning when not adjusting the pointer after reallocating memory. Thanks for taking the time to explain what's going on in background and I feel a lot more comfortable using realloc now.

  • @churrosz
    @churrosz 11 месяцев назад +4

    Your videos about C are the best! I'm taking a software engineering course and you have helped me a lot! You should make videos about sockets, using select, poll, epoll e kqueue

  • @huseynadze7933
    @huseynadze7933 5 месяцев назад +2

    Looks really understandable, thanks so much for broad explanation!

  • @raul_ribeiro_bonifacio
    @raul_ribeiro_bonifacio 8 месяцев назад

    Your videos saved me man!

  • @makara2711
    @makara2711 8 месяцев назад +4

    Can you do more videos about I/O in C? Thank you ❤😊

    • @CodeVault
      @CodeVault  7 месяцев назад +1

      Yes, I certainly will

  • @SSnQ_LPCWSTR
    @SSnQ_LPCWSTR 8 месяцев назад

    Hey man! Thanks for the C content!

  • @aka.windstranger
    @aka.windstranger 9 дней назад

    fantastic explanation!

  • @reenko
    @reenko 6 месяцев назад

    Great video as always , bravo!

  • @fun-damentals6354
    @fun-damentals6354 Месяц назад +1

    you are awesome man

  • @osamaanees8406
    @osamaanees8406 5 месяцев назад

    Best Channel In Existence! I dont know why you dont have millions of views.
    Love From Pakistan!

  • @chinmaygupta8854
    @chinmaygupta8854 10 месяцев назад +2

    Great Video Sir,
    please make videos on IPC in Linux. I know you have made some but it will be great if you can add message queues.
    Also interrupt handling, bottom halves concept

  • @katiakominski432
    @katiakominski432 11 месяцев назад

    I needed this video

  • @xarax7950
    @xarax7950 8 дней назад

    really good explanation !

  • @miguelmendez241
    @miguelmendez241 Месяц назад

    Your videos are awesome and super easy to understand. Thank you. Do you think it could be possible for you to upload two big videos one for the whole Threads playlist and one for the Processes playlist?

    • @CodeVault
      @CodeVault  Месяц назад +1

      There are playlists on the channel for this. I don't think it's a great idea to upload large videos like that on RUclips

  • @thiyagarajank9666
    @thiyagarajank9666 4 месяца назад +1

    This is an awesome tutorial, want more of your C programming . I have a request on fike handling in C , so kindly accept it 😊

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

    Mulțumim!

  • @grimvian
    @grimvian 11 месяцев назад +1

    Thanks for making C videos.
    Was is an idear to make a video or more videos about idiomatic C - please?

    • @CodeVault
      @CodeVault  11 месяцев назад +1

      Yes, I will make more videos on this topic

  • @willofirony
    @willofirony 11 месяцев назад +1

    Excellent video (as we have come to expect). One point, if the the original allocation contained pointers to things within the allocation, say it contains a linked list, and arr is not NULL and doesn't equal res; the links will need to be adjusted by adding the difference between arr and res to each link. Aren't pointers wonderful?

    • @willofirony
      @willofirony 11 месяцев назад

      @@-Xtreme- That depends on how often one expects to require reallocation, especially if the reason for having the nodes in one block of memory was to avoid cache misses. The memory offset is a good idea but it introduces extra cycles to every node access.

  • @dimitrioskalfakis
    @dimitrioskalfakis 11 месяцев назад

    very good.

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

    I think beginner programmers have a tough time with malloc, realloc etc. since when we are learning we allocate statically.

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

    which keyboard are you using, and is it the same as couple of years ago?

    • @CodeVault
      @CodeVault  Месяц назад

      Yeah, I think it's the DasKeyboard Model S with Brown switches. Solid keyboard, works perfectly even after almost 10 years

  • @fusca14tube
    @fusca14tube 11 месяцев назад +2

    Hi... what's the best C programming practice? Instead of "return 1", use "exit(1)" if realloc fails OR do "arr = res;"? I must confess that I never thought about create an exclusive pointer to manage the realloc operation. Thanks in advance.

    • @fusca14tube
      @fusca14tube 11 месяцев назад

      @@-Xtreme- OK... But when your process is finished, all memory is released. So, you could call exit() whenever you want to finish the process, right?

    • @CodeVault
      @CodeVault  11 месяцев назад +1

      You can call exit(1) without issues. I simply used return 1 since it's in the main function. As @Xtreme says, you should clean up allocated resources (file descriptors, memory etc.) before exiting. It's not necessary on modern OSes since they will usually clean up for you but it's good practice

    • @fusca14tube
      @fusca14tube 11 месяцев назад

      @@CodeVault Agreed! Thanks

    • @sfa22
      @sfa22 6 месяцев назад

      ​@@CodeVault Sergiu, in general, you don't want to terminate your program by calling exit(), since the function that does the realloc() is most likely not the "main" function (let's say is called "f"). You don't know who will be calling "f", might be a server, which may have multiple threads that have socket connections. Bringing the entire server down is maybe not a good idea in that case and I guess you would like to maybe take some actions (e.g. trace the out of memory).
      Another case I can think of is having system-wide persistent resources like System V semaphores which were incremented before calling "f" and by returning, the caller has a chance to decrement it, but because you call exit() our process will not decrement them anymore. Most likely it is not the responsibility of "f" to decrement, but of the caller.
      I think "return" is the better choice, but ofc in small examples like this one it doesn't matter if you use exit().

  • @mehdierdem7
    @mehdierdem7 28 дней назад

    Really you explain very good but I dont know english so good

  • @prabhanjanakumartripathy4140
    @prabhanjanakumartripathy4140 11 месяцев назад +1

    Is that possible to get the geolocation through c program

    • @CodeVault
      @CodeVault  10 месяцев назад

      Yes, if you have a GPS locator on your device

  • @KFlorent13
    @KFlorent13 5 месяцев назад

    Hello,
    In your "learn pointers" pdf, I think there is a mistake:
    Analyzing the line:
    int p = &x;
    Should be :
    Analyzing the line:
    int * p = &x;

    • @CodeVault
      @CodeVault  4 месяца назад +1

      Oh, sorry about that. I uploaded a new version with the line corrected

    • @KFlorent13
      @KFlorent13 4 месяца назад

      @@CodeVault No problem. Keep up the good work.

  • @ShubhamKumar-id9sm
    @ShubhamKumar-id9sm 11 месяцев назад

    Please create videos on c++

    • @CodeVault
      @CodeVault  11 месяцев назад +1

      I will look into it. Still debating if I should do C++ or Rust

  • @bjornnordquist1616
    @bjornnordquist1616 11 месяцев назад +1

    Hi! I stumbled across another interesting C feature that may be of interest for your audience (and me :) ). __func__ to trace function call chains. And the uses for tracing where the program currently is.

    • @CodeVault
      @CodeVault  11 месяцев назад

      Oh this is very interesting. Thanks for the info! I might make a video on this feature

  • @animeshkumarsinha5954
    @animeshkumarsinha5954 11 месяцев назад

    If the initial array had 10 element and in realloc we reduce it to 8.
    Then does the last 2 elements gets memory leaked?

    • @CodeVault
      @CodeVault  11 месяцев назад

      What @Xtreme said is correct. In C++ I wouldn't even recommend using malloc/calloc/realloc with classes

    • @animeshkumarsinha5954
      @animeshkumarsinha5954 11 месяцев назад +1

      @@CodeVault if i did malloc of 10 elements then when I do a free then all the 10 elements gets deallocated.
      Now if we do a realloc of 8 elements then what happens to the last 2 elements?
      Do they get deallocated just after realloc since we have realloc for only 8 elements?
      Or they get deallocated when we call free()?

    • @andrewvasylchuk
      @andrewvasylchuk 11 месяцев назад

      I share the same interest in this matter.

    • @CodeVault
      @CodeVault  11 месяцев назад +1

      malloc and free don't have the concept of "elements". What we allocated in the video was only 1 block of memory (which can hold many elements of an array). So, reallocating from 40 bytes (10 * sizeof(int)) to 32 bytes (8 * sizeof(int)) simply makes the block of memory that we have dynamically allocated a bit smaller (or in some cases gets moved like in the second case I explained). All you have to do is free that one block of memory that is 32 bytes long now

    • @andrewvasylchuk
      @andrewvasylchuk 11 месяцев назад

      @@CodeVault Thank you