How to use realloc in C
HTML-код
- Опубликовано: 25 май 2023
- Source code can be found here:
code-vault.net/lesson/707793f...
===== Support us through our store =====
code-vault.net/shop
===== Check out our website =====
code-vault.net
===== Check out our Discord server =====
discord.code-vault.net
Man I wish I had your videos 15 years ago
The "trouble-shooting" discussions frame out the topic (realloc) excellently; addressing segfault and possible memory leaks in easily digestible examples and terminology as well as correctly implementing interfacing preventing and managing the problems was concise. As usual, awesome job.
Good vid.
I simply LOVE your videos about C - thank you
That explains the pesci warning when not adjusting the pointer after reallocating memory. Thanks for taking the time to explain what's going on in background and I feel a lot more comfortable using realloc now.
Your videos about C are the best! I'm taking a software engineering course and you have helped me a lot! You should make videos about sockets, using select, poll, epoll e kqueue
Looks really understandable, thanks so much for broad explanation!
Your videos saved me man!
Can you do more videos about I/O in C? Thank you ❤😊
Yes, I certainly will
Hey man! Thanks for the C content!
fantastic explanation!
Great video as always , bravo!
you are awesome man
Best Channel In Existence! I dont know why you dont have millions of views.
Love From Pakistan!
Great Video Sir,
please make videos on IPC in Linux. I know you have made some but it will be great if you can add message queues.
Also interrupt handling, bottom halves concept
I needed this video
really good explanation !
Your videos are awesome and super easy to understand. Thank you. Do you think it could be possible for you to upload two big videos one for the whole Threads playlist and one for the Processes playlist?
There are playlists on the channel for this. I don't think it's a great idea to upload large videos like that on RUclips
This is an awesome tutorial, want more of your C programming . I have a request on fike handling in C , so kindly accept it 😊
Mulțumim!
Thanks for making C videos.
Was is an idear to make a video or more videos about idiomatic C - please?
Yes, I will make more videos on this topic
Excellent video (as we have come to expect). One point, if the the original allocation contained pointers to things within the allocation, say it contains a linked list, and arr is not NULL and doesn't equal res; the links will need to be adjusted by adding the difference between arr and res to each link. Aren't pointers wonderful?
@@-Xtreme- That depends on how often one expects to require reallocation, especially if the reason for having the nodes in one block of memory was to avoid cache misses. The memory offset is a good idea but it introduces extra cycles to every node access.
very good.
I think beginner programmers have a tough time with malloc, realloc etc. since when we are learning we allocate statically.
which keyboard are you using, and is it the same as couple of years ago?
Yeah, I think it's the DasKeyboard Model S with Brown switches. Solid keyboard, works perfectly even after almost 10 years
Hi... what's the best C programming practice? Instead of "return 1", use "exit(1)" if realloc fails OR do "arr = res;"? I must confess that I never thought about create an exclusive pointer to manage the realloc operation. Thanks in advance.
@@-Xtreme- OK... But when your process is finished, all memory is released. So, you could call exit() whenever you want to finish the process, right?
You can call exit(1) without issues. I simply used return 1 since it's in the main function. As @Xtreme says, you should clean up allocated resources (file descriptors, memory etc.) before exiting. It's not necessary on modern OSes since they will usually clean up for you but it's good practice
@@CodeVault Agreed! Thanks
@@CodeVault Sergiu, in general, you don't want to terminate your program by calling exit(), since the function that does the realloc() is most likely not the "main" function (let's say is called "f"). You don't know who will be calling "f", might be a server, which may have multiple threads that have socket connections. Bringing the entire server down is maybe not a good idea in that case and I guess you would like to maybe take some actions (e.g. trace the out of memory).
Another case I can think of is having system-wide persistent resources like System V semaphores which were incremented before calling "f" and by returning, the caller has a chance to decrement it, but because you call exit() our process will not decrement them anymore. Most likely it is not the responsibility of "f" to decrement, but of the caller.
I think "return" is the better choice, but ofc in small examples like this one it doesn't matter if you use exit().
Really you explain very good but I dont know english so good
Is that possible to get the geolocation through c program
Yes, if you have a GPS locator on your device
Hello,
In your "learn pointers" pdf, I think there is a mistake:
Analyzing the line:
int p = &x;
Should be :
Analyzing the line:
int * p = &x;
Oh, sorry about that. I uploaded a new version with the line corrected
@@CodeVault No problem. Keep up the good work.
Please create videos on c++
I will look into it. Still debating if I should do C++ or Rust
Hi! I stumbled across another interesting C feature that may be of interest for your audience (and me :) ). __func__ to trace function call chains. And the uses for tracing where the program currently is.
Oh this is very interesting. Thanks for the info! I might make a video on this feature
If the initial array had 10 element and in realloc we reduce it to 8.
Then does the last 2 elements gets memory leaked?
What @Xtreme said is correct. In C++ I wouldn't even recommend using malloc/calloc/realloc with classes
@@CodeVault if i did malloc of 10 elements then when I do a free then all the 10 elements gets deallocated.
Now if we do a realloc of 8 elements then what happens to the last 2 elements?
Do they get deallocated just after realloc since we have realloc for only 8 elements?
Or they get deallocated when we call free()?
I share the same interest in this matter.
malloc and free don't have the concept of "elements". What we allocated in the video was only 1 block of memory (which can hold many elements of an array). So, reallocating from 40 bytes (10 * sizeof(int)) to 32 bytes (8 * sizeof(int)) simply makes the block of memory that we have dynamically allocated a bit smaller (or in some cases gets moved like in the second case I explained). All you have to do is free that one block of memory that is 32 bytes long now
@@CodeVault Thank you