C Programming Tutorial 67, Memory Functions pt.5 realloc

preview_player
Показать описание

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

thank you for the videos on memory, i haven't seen others do a good job covering them

k.alipardhan
Автор

Yeah, the compiler doesn't care if you put information at that address or not.

iTzAdamX
Автор

realloc(characters, sizeof(char)*5) should the (sizeof(char) * 5) the sizeof always be used instead of plain 5? I know you discussed thit searlier to ensure there is enogh memory in case the computer is different. But is it always advised or there is an exception why i wouldnt use sizeof inside malloc or realloc?

thewhisperinyourears
Автор

So you could call realloc without assigning it to characters pointer? Ex: realloc(characters, sizeof(char)*5)

bondservantHim
Автор

Question:
malloc returns the adress of the array so you use

*characters = malloc()

At the end of the video you say that realloc returns the new adress of the array but you use

characters = realloc()

Why do you use no asterisk this time?

SaschaFroelich
Автор

Because he created and initialized the "characters" pointer on the same line:

char* characters = malloc(etc...)

But when he called realloc(), the "characters" pointer was already created--so you can simply reassign it the correct address, which is returned by realloc().

Think of it this way:

char* characters;
characters = malloc(sizeof(char) * 10));

// Do something here with the malloc'd memory

// Now call realloc()
characters = realloc(characters, (sizeof(char) * 5));
free(characters);

tcbetka