C_135 Dynamic Memory Allocation using realloc() | C Language Tutorials

preview_player
Показать описание
GeeksforGeeks has come up with this opportunity for you to become a leader amongst your peers, with their Campus Mantris Program. Yes, you can be a mantri at your college campus and it is as prestigious as it sounds.

To register and know more, you can click on:

Connect & Contact Me:

See Complete Playlists:
Рекомендации по теме
Комментарии
Автор

According to the video, the important points about the realloc function in C are:

It is used to resize previously allocated memory blocks. [1:09]
It can be used to increase or decrease the size of the allocated memory block. [1:11]
It returns a void pointer, so the user needs to typecast it according to their needs. [1:21]
One important point is that the realloc function tries to resize the previously allocated memory block in-place, if possible. [1:23] This means that if there is enough free space available next to the allocated block, the realloc function will just expand the block in-place. [1:24]
If it is not possible to resize the block in-place, the realloc function will allocate a new block of memory of the requested size, copy the contents of the old block to the new block, and then deallocate the old block. [1:28]
In any case, the realloc function always guarantees that the data from the original block is preserved up to the minimum of the old size and the new size. [1:31] This means that if you reduce the size of the block, you may lose data from the end of the block. [1:32]
If the realloc function fails to allocate a new block of memory (if there is not enough free space available), it returns NULL, just like malloc and calloc do. [1:34]
You can use realloc to simulate malloc and free. For example, if you pass a null pointer to realloc, it will behave just like malloc. [1:40] Conversely, if you pass a pointer to a previously allocated block of memory and a new size of zero to realloc, it will behave just like free. [1:42] ░

angshulsana
Автор

I went from the introduction of memory allocation, malloc, calloc to the realloc function videos. Now i clearly understand what all this is all about. Thank you! Well explained.

kitwanashebabetsiezechiel
Автор

IMPORTANT POINTS OF REALLOC( )

1. It mean "reallocation of memory"
2. It main purpose is to "resize" the memory after already allocated with malloc or calloc
3. Resize means to "increase or decrease" memory
4. You can only use realloc if you have already allocated memory using malloc or calloc, else it is impossible to use realloc
5. it accept two arguments: (a) previous allocated pointer (b) new memory size
6. realloc returns void*
7. when it increases memory, it will not lose the previous content
8. after using realloc, you should free it.

abdulrahimjalloh
Автор

we use realloc for reallocation of memory
we can resize the memory allocated either we can increase or we can decrease
relloc also accepts two arguments one for previously allocated pointer and other is for updated size
while resizing the program according to requirements if we use NULL in place previous allocated pointer it behaves as malloc functions
if we use NULL in size_t size place it will free the preious pointer.

hariparuchuru
Автор

1. 👉mam but when you teaching us about heap & dma, you said that dma allocat mamery from opposite side in heap . what is meaning of that? in heap if memory is allocated from end side and we increase more elements then in which side blocks will be added.

2.👉 if memory is allocated from end side then which is first block thats address will be returned? first block from end side or last block from end side.

3. 👉 if we have 3 continuous blocks and we increase size of one of middle of then how the compiler or our operating system manage that??

4. 👉if realloc not able to expand the size then it will return null when we store in same pointer🤔 so pointer to that previous block will lost. then how the data is safe ??

and suppose we store new address in new pointer then what about the old pointer after deallocating .

5.👉 if using malloc ir calloc we allocate data for int and after that using realloc we we typecast pointer to char or another data type then what will happen with the previous elements of another datatype like int. (typecasted to char);

6.👉 why calloc ? what is need of multiple blocks .?
7.👉 if i want to manipulate a specific element in DMA, than how to done it
8.👉13:45 whats about the poniter after put null in realloc
please explain these

RiyaRoy-qitk
Автор

Important point
In reallocation it expands the same memory address.

errorless
Автор

realloc()-
~resize - increase or decrease the memory size that was previously allocated by calloc and malloc
~takes 2 arguments, previously allocated memory pointer and the new size
~can resize the same block, if not possible then allocate fresh memory
~on failure of reallocation it will return NULL
~behave as a calloc and malloc when you pass null pointer
~behave as a free() when you pass the new size as null

srs
Автор

Mam you doing lot of hard work for us thanks for that ❤️❤️

Bornkill-jqts
Автор

Reallocation also works in NULL pointer. If we are used realoc () function (without using the data from malloc() and calloc() function) then the realloc function works as malloc().

santhoshk
Автор

Perfect timeing,
I recieve the video notification while reading calloc function in book 🙂

mmmb
Автор

If realloc has caused previously allocated memory to be freed. Isn't ptr now a dangling pointer? Jenny ma'am, please clarify doubt.
17:00 previous address and new address are not same as shown in screen. Can please clarify anyone? Hence there is a requirement to free ptr1 and ptr2. Does realloc also do free of previous ptr?

pratikkurra
Автор

But ma'am, at 17:19, the previous address and the new address are different. So, does it mean that not enough memory space was available for reallocation? And also when you run the same program the second time, the two addresses were equal.

rickk
Автор

Hi mam.thanks for ur lecture....I got first only because of ur class...lots of luv from telangana.really ur lectures are so nyc....tq so much mam...we always support u.... keep going mam...all the best for ur future..

infinity....
Автор

Thank you mama, Now i got it completely

PRODoroNobita
Автор

Your voice, your eyes, your smile, your face expression...all are so so so so have been never seen...

surajlahine
Автор

when we reinitialized the new content the its print only the new content not the previous one

arpansingh
Автор

I have a doubt that before realloc() we already typecasted that we want to store int value in malloc and realloc is adding only block or else make another memory space so why are we again typecasting realloc ()

akankshaawasthi
Автор

17:15 ma’am realloc() should act like calloc() not malloc() when initialized with null in pointer location as when you executed the program instead of giving garbage value it gives 0 when printed

hardikverma
Автор

And I started today and target complete all vedio in one month 135 lecture PlZ provide MCQ on c. Reply mam

s.kkumar
Автор

-Realloc is for reallocation of memory or if we want to decrease number of elements or want to increase without losing the previous data defined before.
-Realloc when not sucessfully allocate memory will return NULL that's why its better to check before
-Realloc when can't add anymore elements to heap on that memory address it will copy the elements and it will reallocate on another memory address with purpose to store every element.
-We must use dynamic memory allocation like calloc and malloc first and then to expand the size using realloc we cannot use realloc before malloc and calloc because that will be undefined behaviour.

godza