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; }
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!
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
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! :-)
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?☺️
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.
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. 🙂
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!
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 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.
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.
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. :-)
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/.
@@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 😅
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.
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; }
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!
You’re welcome, I’m really glad to hear the video helped to clear things up! :-)
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
Thank you Mr. for making the "Dynamic Memory Allocation" topic more understandable.
You're very welcome Akin! :-)
most underrated programming youtuber!!!!!
Great tutorial. A missing point, it’s required to free the allocated memory at the end, when it’s not being used anymore.
as great as usual, the content is absolutely rich and fulfilling. Thank you
Nice, like C++ vector
if i'm correct, this behaves similarly to a vector list in c++ right?
Hey I need help, am trying to understand ADT but it's like am getting the idea what can I do
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! :-)
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?☺️
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.
What will happen when the hardware can't store values anymore?
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. 🙂
@@PortfolioCourses Thank you for your fast reply 😀
@@war-c0mmander You're welcome! 🙂
So without handling the case of realloc() returning null, the program will segfault due to trying to dereference a null pointer.
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!
I’m glad you found the answer Brian! :-)
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;
}
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
@@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.
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?
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.
in real programming what can I do with bitwise knowledge?
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. :-)
make a video on how to use gtk+ , to make user interface
That topic has been requested a few times, and it's on my list of video ideas. 🙂
make a video on GUI using c
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/.
@@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 😅
Is buffer a datatype?
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
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.
is c used to develop application that require a databases😁😁
Yes sometimes C programs will access a database, it depends on the purpose of the program. :-)