Infinite Input Buffer | C Programming Example

Поділитися
Вставка
  • Опубліковано 15 жов 2024
  • An example of an "infinite" input buffer using C. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!

КОМЕНТАРІ • 42

  • @Rai_Te
    @Rai_Te 9 місяців тому +9

    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 Рік тому +4

    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 2 місяці тому

    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.

  • @pontusschönhult
    @pontusschönhult 4 місяці тому

    most underrated programming youtuber!!!!!

  • @ErginSoysal
    @ErginSoysal 9 місяців тому +1

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

  • @zarifishmam809
    @zarifishmam809 Місяць тому

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

  • @SketchupGuru
    @SketchupGuru Рік тому

    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  Рік тому

      Yes, malloc allocates memory initially for our program to use. :-) This video explains dynamic memory allocation in C in more detail: ua-cam.com/video/R0qIYWo8igs/v-deo.html.

  • @naboulsikhalid7763
    @naboulsikhalid7763 10 місяців тому

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

  • @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!

  • @brockdaniel8845
    @brockdaniel8845 9 місяців тому

    Nice, like C++ vector

  • @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 9 місяців тому

      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 Рік тому

    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: ua-cam.com/video/RVqdK6WAjUI/v-deo.html. And this video on Stack data structures: ua-cam.com/video/Flk5yrlx5Qo/v-deo.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! :-)

  • @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 Рік тому

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

  • @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. :-)

  • @justcurious1940
    @justcurious1940 8 місяців тому

    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 8 місяців тому +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 8 місяців тому

      @@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.

  • @vinmo8326
    @vinmo8326 Рік тому

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

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

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

  • @sandy-be9kz
    @sandy-be9kz 2 роки тому

    Is buffer a datatype?

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому

      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 Рік тому

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

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

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

  • @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 😅

  • @lolcheapkill3000
    @lolcheapkill3000 2 роки тому

    Does this work the same way for a C string?
    Id like to get user input using scanf_s and the input im requesting would also be used as the parameter for the buffersize.
    char userInput[10];
    int someNum;
    scanf_s("%d %[^
    ]s", someNum, userInput, sizeof(userInput));

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +3

      I believe you could do something pretty similar for a string, but it would depend on some other string related functions (e.g. getchar perhaps). Maybe I'll make a video on an 'infinite string buffer'. :-)

    • @lolcheapkill3000
      @lolcheapkill3000 2 роки тому

      @@PortfolioCourses I look forward to it.