marq

Dr. Charles Simonyi is the Father of Modern Microsoft Excel                                           JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, later LiveScript, and finally renamed to JavaScript.                                           The word "Biology" is firstly used by Lamarck and Treviranus                                           Hippocrates (460-370 bc) is known as father of medicine.                                           Galene, 130-200 is known as father of Experimental Physology                                           Aristotle (384-322 BC) is known as Father of Zoology because he wrote the construction and behavior of different animals in his book "Historia animalium"                                           Theophrastus(370-285 BC) is known as father of Botany because he wrote about 500 different plants in his book "Historia Plantarum".                                           John Resig is known as Father of Jquery -                                          HTML is a markup language which is use to design web pages. It was invented in 1990 by Tim Berners-Lee.                                                                The Google was founded by Larry Page and Sergey Brin.                                                                Rasmus Lerdorf was the original creator of PHP. It was first released in 1995.                                                               Facebook was founded by Mark Zuckerberg                                                               Bjarne Stroustrup, creator of C++.                                                                Dennis Ritchie creator of C                                                                                                                              James Gosling, also known as the "Father of Java"                                          At 11.44%, Bihar is India's fastest growing state                                          Father of HTML -Tim Berners Lee                                          orkut was created by Orkut Büyükkökten, a Turkish software engineer                    Photoshop: It came about after Thomas Knoll, a PhD student at the University of Michigan created a program to display grayscale images on a monochrome monitor which at the time was called 'Display'.

C Programming - Dynamic Memory allocation


Embedded and other low-memory footprint applications need to be easy on the amount of memory they use when executing.  In such scenarios, statically sized data types and data structures just are not going to solve your problem.  The best way to achieve this is by allocation of memory for variables at runtime under your watchful eye. This way your program is not using more memory than it has to at any given time. However, it is important to note that the amount of memory that can be allocated by a call to any of these functions is different on every operating system.
Dynamic Memory Allocation
malloc, calloc, or realloc are the three functions used to manipulate memory.  These commonly used functions are available through the stdlib library so you must include this library in order to use them.

Sample Code
#include stdlib.h



After including the stdlib library you can use the malloc, calloc, or realloc functions to manipulate chunks of memory for your variables.

Dynamic Memory Allocation Process
When a program executes, the operating system gives it a stack and a heap to work with. The stack is where global variables, static variables, and functions and their locally defined variables reside. The heap is a free section for the program to use for allocating memory at runtime.

Allocating a Block of Memory
Use the malloc function to allocate a block of memory for a variable. If there is not enough memory available, malloc will return NULL.

The prototype for malloc is:

Sample Code
void *malloc(size_t size);
.


Do not worry about the size of your variable, there is a nice and convenient function that will find it for you, called sizeof. Most calls to malloc will look like the following example:

Sample Code
ptr = (struct mystruct*)malloc(sizeof(struct mystruct));
.


This way you can get memory for your structure variable without having to know exacly how much to allocate for all its members as well.

Allocating Multiple Blocks of Memory
You can also ask for multiple blocks of memory with the calloc function:

Sample Code
void *calloc(size_t num, size_t size);
.


If you want to allocate a block for a 10 char array, you can do this:

Sample Code
char *ptr; 
ptr = (char *)calloc(10, sizeof(char));
.


The above code will give you a chunk of memory the size of 10 chars, and the ptr variable would be pointing to the beginning of the memory chunk. If the call fails, ptr would be NULL.

Releasing the Used Space
All calls to the memory allocating functions discussed here, need to have the memory explicitly freed when no longer in use to prevent memory leaks. Just remember that for every call to an *alloc function you must have a corresponding call to free.

The function call to explicitly free the memory is very simple and is written as shown here below:

Sample Code
free(ptr);
.


Just pass this function the pointer to the variable you want to free and you are done.

To Alter the Size of Allocated Memory

Lets get to that third memory allocation function, realloc.

Sample Code
void *realloc(void *ptr, size_t size);
.


Pass this function the pointer to the memory you want to resize and the new size you want to resize the allocated memory for the variable you want to resize.

Here is a simple and trivial example to give you a quick idea of how you might see calloc and realloc in action. You will have many chances for malloc viewing as it is the most popular of the three by far.

allocation.c:
Sample Code

#include <stdio.h>
#include <stdlib.h>
void main() {
char *ptr, *retval;
ptr = (char *)calloc(10, sizeof(char));
if (ptr == NULL)
printf("calloc failed\n");
else
printf("calloc successful\n");
retval = realloc(ptr, 5);
if (retval == NULL)
printf("realloc failed\n");
else
printf("realloc successful\n");
free(ptr);
free(retval);
}


First we declared two pointers and allocated a block of memory the size of 10 chars for ptr using the calloc function. The second pointer retval is used for getting the return value from the call to realloc. Then we reallocate the size of ptr to 5 chars instead of 10. After we check whether all went well with that call, we free up both pointers.

You can play around with the values of size passed to either of the memory allocation functions to see how big a chunk you can ask for before it fails on you. Do not worry, your operating system has the ability to keep your program in check, you will not hurt it this way.

Here is the output:

Sample Code
~/Projects/C_tutorials/dynamic memory allocation/samples $ ./all
ocation
calloc successful
realloc successful
~/Projects/C_tutorials/dynamic memory allocation/samples $
.

No comments:

Post a Comment