HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
Malloc, calloc, realloc
1. 2011
Saket Kr. Pathak
Software Developer
3D Graphics
Fundamentals Of Programming C
A quick review of fundamentals of C with an prospective view of implementation in daily day to
day life.
2. Fundamentals Of Programming C
Hiii all, here just a few days back we have a festival Diwali.
As you, I also thought to buy something and went to stores/showrooms to by shirt. I
selected one in blue color (my favorite) and just buy it, I also bought a hanky (obviously
white with S mark) from there. Unfortunately the packet of shirt was so compact that it's
really a taugh task to adjust hanky's packet within the shirt's.
All of sudden, malloc strike my mind ... yaaa :) ... please dn't laugh and I know you
people are not going to stop laughing.
Ok, now think the packet of shirt is just same as the memory chunk given
by malloc(size_t) i.e. for exact data, that's it. As;
//Returns the base address/memory location of a memory-chunk
//having size specified within parameter.
char* what_is_malloc(void)
{
char *chptr_packet = malloc(sizeof(char)*13);
chptr_packet = "Get a shirtn";
return chptr_packet;
}
Somehow I made some adjustment within the shirt's packet and made some space for
hanky's packet.
Then finally I returned back to home. I had taken it lightly and move forward, then came
Dhanteras (part of Diwali that comes a day before it). Since I am the lonely bird so there
was scarcity of spoons in my place. I decided to buy a spoon-set (spoon-stand) in this
Dhanteras occasion. I bought it.
I was arranging the spoon-set among my kitchen stuff then, calloc(size_t,
size_t) triggered my mind. Now how, calloc returns the exact memory chunk for the
number of items passed to it having particular size same as spoon-set with fixed number
of the place holders for spoons.
i.e.
//Returns the base address/memory-location of a memory-chunk
//for n number of objects specified as 1st parameter each
//having size specified within 2nd parameter.
Saket Kr. Pathak Page 2
3. Fundamentals Of Programming C
char** what_is_calloc(void)
{
char **chptr_stand = calloc(6, sizeof(char)*12);
chptr_stand [0] = "Tea Spoon";
chptr_stand [1] = "Tea Spoon";
chptr_stand [2] = "Fork";
chptr_stand [3] = "Fork";
chptr_stand [4] = "Butter Knife";
chptr_stand [5] = "Butter Knife";
return chptr_stand ;
}
After that, I was searching for the example of realloc(void*, size_t) in the room and I
got my luggage bag. Yes that's the one, where i can adjust about half of extra luggage
after full bag using chains in it, same as realloc in C, that only changes the size of
object and returns the base address to the caller. As;
//Returns the base-address/memory-location of a memory-chunk
//passed within the 1st parameter with the new memory-space
// defined as 2nd parameter.
char* what_is_realloc(char* bag)
{
bag = (char*)realloc(bag, sizeof(char) * 30);
bag = "Shirt, Paint, Tie, Coat, Jacket, Socks, Shoes";
return bag;
}
Now lets have main function as;
//Global Declaration with the File
char* what_is_malloc(void);
char** what_is_calloc(void);
char* what_is_realloc(char*);
Saket Kr. Pathak Page 3
4. Fundamentals Of Programming C
int main()
{
printf("Let's %s",what_is_malloc());
char** my_place = what_is_calloc();
int number_of_items = 6;
int loopCount;
for (loopCount = 0;
loopCount < number_of_items;
++loopCount)
printf("Please give me : %sn",my_place[loopCount]);
printf("Please take %s in my bag.",
what_is_realloc(what_is_malloc()));
getch();
return 0;
}
Hmmm ... got a practical implementation of these memory allocation functions given by
Dennis M. Ritchie, in daily life. Just a view :) although malloc is not so simple to create.
It's basically, a union having a structure and a long type variable as;
//Reference: The C Programming langugae by Kernighan/Ritchie -
//Chapter-8(8.7), Page-186. --- must read
union head
{
struct
{
union head *ptr;
unsigned size;
} strct;
long lval;
}
Hmmm :) ... it's the instance i had, malloc/calloc/realloc in my daily life ... haaa haaa
haaa :). Yup, a way to be close with C programming ... that's it. Take care ... in someone's
style ... not mine ... i used to say ... ctch u again ... :)
Saket Kr. Pathak Page 4