Resize A String Stored In Dynamically Allocated Memory | C Programming Tutorial

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

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

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

    Note: realloc() could fail, this video explains how to handle it: ruclips.net/video/skqCnhhA0ZY/видео.html. 🙂 Also note that when using dynamically allocated memory we should use free() after we are done using it, this won't matter in a small teach-a-concept demonstration like this, but in real large program it can cause a memory leak!

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

    Thank you, Kevin.

  • @dimitrioskalfakis
    @dimitrioskalfakis 5 месяцев назад +4

    sure, but if it returns NULL would it not be better to store the return address to a different pointer and then determine if string will point to it? this happens more often than one would think in small embedded chips.

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

      in that case you don't want to use realloc? just manually malloc new space and free old space?

    • @dimitrioskalfakis
      @dimitrioskalfakis 5 месяцев назад +1

      @@bugurme1 realloc with a NULL argument amounts to the same thing. i am saying that checking for NULL is important. you can not avoid fragmentation as long as you deal with heap instead of stack.

    • @PortfolioCourses
      @PortfolioCourses  5 месяцев назад +3

      Yeah this video is intended for beginners, if anyone wants to learn more about handling realloc failures check out this video covering the topic: ruclips.net/video/skqCnhhA0ZY/видео.html