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

Поделиться
HTML-код
  • Опубликовано: 26 сен 2024
  • How to resize a string stored in dynamically allocated memory using C. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!

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

  • @PortfolioCourses
    @PortfolioCourses  3 месяца назад +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 3 месяца назад

    Thank you, Kevin.

  • @dimitrioskalfakis
    @dimitrioskalfakis 3 месяца назад +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 3 месяца назад

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

    • @dimitrioskalfakis
      @dimitrioskalfakis 3 месяца назад +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  3 месяца назад +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