SlideShare ist ein Scribd-Unternehmen logo
1 von 4
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.
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
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
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

Weitere ähnliche Inhalte

Andere mochten auch

Andere mochten auch (9)

A Guy and gal in STL
A Guy and gal in STLA Guy and gal in STL
A Guy and gal in STL
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
Wan Important Questions
Wan Important QuestionsWan Important Questions
Wan Important Questions
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Wan notes
Wan notesWan notes
Wan notes
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
Introduction to OpenCV (with Java)
Introduction to OpenCV (with Java)Introduction to OpenCV (with Java)
Introduction to OpenCV (with Java)
 
Image Processing with OpenCV
Image Processing with OpenCVImage Processing with OpenCV
Image Processing with OpenCV
 

Ähnlich wie Malloc, calloc, realloc

13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation
웅식 전
 
C++tutorial
C++tutorialC++tutorial
C++tutorial
dips17
 
I really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdfI really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdf
wasemanivytreenrco51
 
PHPConPl 2013 - Allowed memory size of X bytes exhausted
PHPConPl 2013 - Allowed memory size of X bytes exhaustedPHPConPl 2013 - Allowed memory size of X bytes exhausted
PHPConPl 2013 - Allowed memory size of X bytes exhausted
Piotr Pasich
 
I have the first program completed (not how request, but it works) a.pdf
I have the first program completed (not how request, but it works) a.pdfI have the first program completed (not how request, but it works) a.pdf
I have the first program completed (not how request, but it works) a.pdf
footworld1
 

Ähnlich wie Malloc, calloc, realloc (20)

dynamic_v1-3.pptx
dynamic_v1-3.pptxdynamic_v1-3.pptx
dynamic_v1-3.pptx
 
DS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxDS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docx
 
13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
 
Dynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxDynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptx
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 
Linked list
Linked listLinked list
Linked list
 
C++tutorial
C++tutorialC++tutorial
C++tutorial
 
(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
dynamic-allocation.pdf
dynamic-allocation.pdfdynamic-allocation.pdf
dynamic-allocation.pdf
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
I really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdfI really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdf
 
Introduction to modern c++ principles(part 1)
Introduction to modern c++ principles(part 1)Introduction to modern c++ principles(part 1)
Introduction to modern c++ principles(part 1)
 
NativeBoost
NativeBoostNativeBoost
NativeBoost
 
PHPConPl 2013 - Allowed memory size of X bytes exhausted
PHPConPl 2013 - Allowed memory size of X bytes exhaustedPHPConPl 2013 - Allowed memory size of X bytes exhausted
PHPConPl 2013 - Allowed memory size of X bytes exhausted
 
I have the first program completed (not how request, but it works) a.pdf
I have the first program completed (not how request, but it works) a.pdfI have the first program completed (not how request, but it works) a.pdf
I have the first program completed (not how request, but it works) a.pdf
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - C
 

Mehr von Saket Pathak (7)

GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?
 
C++ friendship
C++ friendshipC++ friendship
C++ friendship
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Copy constructor
Copy constructorCopy constructor
Copy constructor
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
C++ diamond problem
C++ diamond problemC++ diamond problem
C++ diamond problem
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
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