Malloc, calloc, realloc

3,892 views

Published on

A quick review of the fundamental concepts of malloc, calloc and realloc in programming language C/C++ in prospective view of daily day to day life.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,892
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
55
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Malloc, calloc, realloc

  1. 1. 2011Saket Kr. PathakSoftware Developer3D GraphicsFundamentals Of Programming CA quick review of fundamentals of C with an prospective view of implementation in daily day today life.
  2. 2. Fundamentals Of Programming CHiii 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. Iselected one in blue color (my favorite) and just buy it, I also bought a hanky (obviouslywhite with S mark) from there. Unfortunately the packet of shirt was so compact that itsreally a taugh task to adjust hankys packet within the shirts.All of sudden, malloc strike my mind ... yaaa :) ... please dnt laugh and I know youpeople are not going to stop laughing.Ok, now think the packet of shirt is just same as the memory chunk givenby malloc(size_t) i.e. for exact data, thats 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 shirts packet and made some space forhankys packet.Then finally I returned back to home. I had taken it lightly and move forward, then cameDhanteras (part of Diwali that comes a day before it). Since I am the lonely bird so therewas scarcity of spoons in my place. I decided to buy a spoon-set (spoon-stand) in thisDhanteras 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 thenumber of items passed to it having particular size same as spoon-set with fixed numberof 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. 3. Fundamentals Of Programming Cchar** 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 Igot my luggage bag. Yes thats the one, where i can adjust about half of extra luggageafter full bag using chains in it, same as realloc in C, that only changes the size ofobject 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 Filechar* what_is_malloc(void);char** what_is_calloc(void);char* what_is_realloc(char*);Saket Kr. Pathak Page 3
  4. 4. Fundamentals Of Programming Cint main(){ printf("Lets %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 byDennis M. Ritchie, in daily life. Just a view :) although malloc is not so simple to create.Its 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 readunion head{ struct { union head *ptr; unsigned size; } strct; long lval;}Hmmm :) ... its the instance i had, malloc/calloc/realloc in my daily life ... haaa haaahaaa :). Yup, a way to be close with C programming ... thats it. Take care ... in someonesstyle ... not mine ... i used to say ... ctch u again ... :)Saket Kr. Pathak Page 4

×