Infinite Input Buffer | C Programming Example

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

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

  • @Rai_Te
    @Rai_Te 11 месяцев назад +10

    2 minor notes on the video:
    #1 the 'else' in the program (when something else than -1 was entered) is not needed at all.
    I would omit it.
    #2 buffer=realloc(buffer,newsize); is a timebomb.
    if realloc() fails (for whatever reason), you do not only have no new buffer,
    you also lost your old buffer ... technically the old buffer is still there, but you
    overwrote the pointer-variable holding the location.
    So, use
    newbuffer=realloc(oldbuffer,newsize)
    followed by
    if (newbuffer==NULL) { some errorhandling that might include oldbuffer }
    else { oldbuffer=newbuffer; newbuffer=NULL; }

  • @punchedchunk3483
    @punchedchunk3483 Год назад +5

    Just came to C from higher level languages as a web developer. I was struggling with the idea of malloc and realloc and this just cleared everything up for me! Thanks!

    • @PortfolioCourses
      @PortfolioCourses  Год назад +1

      You’re welcome, I’m really glad to hear the video helped to clear things up! :-)

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

    it's my second time watching this tutorial, and I just realized that it's a genius algorithm to increase the use of memory to instore data, to be processed. thank you

  • @aknsagdic9388
    @aknsagdic9388 Год назад +1

    Thank you Mr. for making the "Dynamic Memory Allocation" topic more understandable.

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

    most underrated programming youtuber!!!!!

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

    Great tutorial. A missing point, it’s required to free the allocated memory at the end, when it’s not being used anymore.

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

    as great as usual, the content is absolutely rich and fulfilling. Thank you

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

    Nice, like C++ vector

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

    if i'm correct, this behaves similarly to a vector list in c++ right?

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

    Hey I need help, am trying to understand ADT but it's like am getting the idea what can I do

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

      I don't have many videos on ADT or data structures unfortunately. :-( I have this video on Set data structures: ruclips.net/video/RVqdK6WAjUI/видео.html. And this video on Stack data structures: ruclips.net/video/Flk5yrlx5Qo/видео.html. And I have a Udemy course on Linked Lists in C: portfoliocourses.com/. With data structures, I always found the key was to draw pictures on pencil and paper at the same time as writing the code... I always needed to visualize what was going on in order to really understand what the code was doing. Hopefully this helps! :-)

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

    This was very intersting. I understood that realloc is essentially reallocating memory in ram for the code.
    What does malloc do? Does it initialise memory for the code?
    Can we just create the code without allocating memory and then reallocate if our code output is big?☺️

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

      Yes, malloc allocates memory initially for our program to use. :-) This video explains dynamic memory allocation in C in more detail: ruclips.net/video/R0qIYWo8igs/видео.html.

  • @war-c0mmander
    @war-c0mmander 2 года назад +1

    What will happen when the hardware can't store values anymore?

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

      Great question! Eventually realloc() would fail and return NULL instead of a memory address. We could add a condition to handle this case if we wanted to as well: www.tutorialspoint.com/c_standard_library/c_function_realloc.htm. 🙂

    • @war-c0mmander
      @war-c0mmander 2 года назад

      @@PortfolioCourses Thank you for your fast reply 😀

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

      @@war-c0mmander You're welcome! 🙂

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

      So without handling the case of realloc() returning null, the program will segfault due to trying to dereference a null pointer.

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

    I might be missing something here, but it appears that malloc offers the structure of an array? Else, where does buffer inherit its structure [of an array] where it has elements?
    EDIT: Disregard. You explain this in the first 5-10min of the malloc video. VERY COOL!

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

    Nice, We can use the same idea to get infinite string from the user like this maybe :
    char *infinite_line(void){
    int size = 5;
    char *line = (char*)malloc(sizeof(char) * size);
    int c ;
    int char_count = 0;
    while((c = getchar()) != '
    '){
    if(char_count == size){
    size+=5;
    line = realloc(line,sizeof(char) * size);
    }
    line[char_count] = c;
    ++char_count;
    }
    line[char_count] = '\0';
    return line;
    }

    • @htoct
      @htoct 10 месяцев назад +1

      i think if the user writes a newline and char_count == size, the buffer wont be reallocated and the ‚\0‘ will be written outside of the buffer

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

      @@htoct How can the user write a new line character ? and yes I agree with u there is a potential of overflow, we can fix it by adding an if statement that checks if the size == char_count before adding the null terminator.

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

    Hi! Thank u for the video.
    I have a question:
    Is there any reason to learn c, to better umderstand c#/c++ or any other language that based on pure C?

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

      C is still used a lot in the embedded world, and as far as I know, the Linux kernel still uses it. C++ has a C-like subset but they are now substantially different languages, and C++ has a lot of gotchas that will introduce bugs in your code if you're not very well acquainted with how this works (in particular, copy constructors in C++ are liable to cause your classes to behave incorrectly in certain situations if you don't define them).
      C# is only superficially similar to C in that it uses braces and some of the keywords are inherited originally from it, but being a garbage collected, object oriented, VM-interpreted language, is completely different from C. You might be able to run (a subset of) C++ in an embedded environment, but you're not likely to be able to do so in C#; the runtime is too large.

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

    in real programming what can I do with bitwise knowledge?

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

      Bitwise operations tend to come up in really low level programming like embedded systems, drivers, compilers, operating systems. For example some device may dump out bytes of information where each bit in the byte "means something". And bitwise operators might be used to check if these bits are set or not. :-)

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

    make a video on how to use gtk+ , to make user interface

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

      That topic has been requested a few times, and it's on my list of video ideas. 🙂

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

    make a video on GUI using c

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

      One day I may do a video on that topic, I agree it's a good idea. :-) I've thought about covering GTK+: www.geeksforgeeks.org/how-to-create-gui-in-c-programming-using-gtk-toolkit/.

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

      @@PortfolioCourses i hope in the near future you showcase using gtk. i've liked the concept of doing gui programs since we are taught about visual basic 😅

  • @sandy-be9kz
    @sandy-be9kz 3 года назад

    Is buffer a datatype?

    • @PortfolioCourses
      @PortfolioCourses  3 года назад

      No, it's just a term used in computer programming to refer to a place where data is temporarily stored: en.wikipedia.org/wiki/Data_buffer

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

      Maybe also in addition:
      buffer is the name of the pointer variable and it is pointing to an array of integer numbers.
      We can change the name of the variable to what ever we want but the name should allways describe the sense as good as possible.
      Therefore he choosed the buffer as the name of the pointer variable.

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

    is c used to develop application that require a databases😁😁

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

      Yes sometimes C programs will access a database, it depends on the purpose of the program. :-)