Dynamic memory management
2 posters
Page 1 of 1
Dynamic memory management
Dynamic memory management:
Allocate memory block
Allocates a block of size bytes
of memory, returning a pointer to the beginning of the block.
The content of the newly allocated block of memory is not initialized,
remaining with indeterminate values.
Parameters
size ,Size of the memory block, in bytes.
Return Value
On success, a pointer to the memory block allocated by the
function.
The type of this pointer is always void*, which can be cast to the desired
type of data pointer in order to be dereferenceable.
If the function failed to allocate the requested block of memory, a null
pointer is returned.
Example
This program generates a string of the length specified by the user and fills
it with alphabetic characters. The possible length of this string is only
limited by the amount of memory available in the system that malloc can allocate.
Allocate space for array in memory
Allocates a block of memory for an
array of num elements, each of them [i]size bytes long, and
initializes all its bits to zero.
The effective result is the allocation of an zero-initialized memory block of (num* size) bytes.
Parameters
num ,Number of elements to be allocated.
size, Size of elements.
Return Value
A pointer to the memory block allocated by the function.
The type of this pointer is always void*, which can be cast to the desired
type of data pointer in order to be dereferenceable.
If the function failed to allocate the requested block of memory, a NULL pointer is returned.
Example
/*calloc example */
This program simply stores numbers and then prints them out. But the number of
items it stores can be adapted each time the program is executed because it
allocates as much dynamic memory as needed during runtime.
Reallocate memory block
The size of the memory block pointed
to by the ptr parameter is changed to the size bytes, expanding
or reducing the amount of memory available in the block.
The function may move the memory block to a new location, in which case the new location is returned. The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved. If the new size is larger, the value of the newly allocated portion is indeterminate.
In case that ptr is NULL, the function behaves exactly as malloc, assigning a new block of size
bytes and returning a pointer to the beginning of it.
In case that the size is 0, the memory previously allocated in ptr
is deallocated as if a call to free
was made, and a NULL pointer is returned.
Parameters
ptr, Pointer to a
memory block previously allocated with malloc,
calloc or realloc to be reallocated.
If this is NULL, a new block is allocated and a pointer to it is returned by
the function.
size, New size for the
memory block, in bytes.
If it is 0 and ptr points to an existing block of memory, the memory
block pointed by ptr is deallocated and a NULL pointer is returned.
Return Value
A pointer to the reallocated memory block, which may be
either the same as the ptr argument or a new location.
The type of this pointer is void*, which can be cast to the desired type of data pointer in order to be dereferenceable.
If the function failed to allocate the requested block of memory, a NULL
pointer is returned.
Example
/*realloc example: rememb-o-matic */
The program prompts the user for numbers until a zero character is entered. Each
time a new value is introduced the memory block pointed by numbers is increased
by the size of an int.
Deallocate space in memory
A block of memory previously allocated using a call to malloc, calloc or realloc is deallocated, making it available again for further allocations.
Notice that this function leaves the value of ptr unchanged, hence it
still points to the same (now invalid) location, and not to the null pointer.
Parameters
ptr, Pointer to a memory block previously allocated with malloc, calloc or realloc to be deallocated.
If a null pointer is passed as argument, no action occurs.
Return Value
(none)
Example
/*free example */
This program has no output. Just demonstrates some ways to allocate and free
dynamic memory using the cstdlib functions.
SOURCE : http://cplusplus.com/reference/clibrary/cstdlib
malloc |
|
void * malloc ( size_t size ); | |
Allocates a block of size bytes
of memory, returning a pointer to the beginning of the block.
The content of the newly allocated block of memory is not initialized,
remaining with indeterminate values.
Parameters
size ,Size of the memory block, in bytes.
Return Value
On success, a pointer to the memory block allocated by the
function.
The type of this pointer is always void*, which can be cast to the desired
type of data pointer in order to be dereferenceable.
If the function failed to allocate the requested block of memory, a null
pointer is returned.
Example
/*malloc example: string generator*/
|
This program generates a string of the length specified by the user and fills
it with alphabetic characters. The possible length of this string is only
limited by the amount of memory available in the system that malloc can allocate.
calloc | function |
void * calloc ( size_t num, size_t size ); | |
Allocates a block of memory for an
array of num elements, each of them [i]size bytes long, and
initializes all its bits to zero.
The effective result is the allocation of an zero-initialized memory block of (num* size) bytes.
Parameters
num ,Number of elements to be allocated.
size, Size of elements.
Return Value
A pointer to the memory block allocated by the function.
The type of this pointer is always void*, which can be cast to the desired
type of data pointer in order to be dereferenceable.
If the function failed to allocate the requested block of memory, a NULL pointer is returned.
Example
/*calloc example */
- Code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i,n;
int * pData;
printf ("Amount of numbers to be entered: ");
scanf ("%d",&i);
pData = (int*) calloc (i,sizeof(int));
if (pData==NULL) exit (1);
for (n=0;n<i;n++)
{
printf ("Enter number #%d: ",n);
scanf ("%d",&pData[n]);
}
printf ("You have entered: ");
for (n=0;n<i;n++) printf ("%d ",pData[n]);
free (pData);
return 0;
}
This program simply stores numbers and then prints them out. But the number of
items it stores can be adapted each time the program is executed because it
allocates as much dynamic memory as needed during runtime.
realloc | function |
void * realloc ( void * ptr, size_t size ); |
Reallocate memory block
The size of the memory block pointed
to by the ptr parameter is changed to the size bytes, expanding
or reducing the amount of memory available in the block.
The function may move the memory block to a new location, in which case the new location is returned. The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved. If the new size is larger, the value of the newly allocated portion is indeterminate.
In case that ptr is NULL, the function behaves exactly as malloc, assigning a new block of size
bytes and returning a pointer to the beginning of it.
In case that the size is 0, the memory previously allocated in ptr
is deallocated as if a call to free
was made, and a NULL pointer is returned.
Parameters
ptr, Pointer to a
memory block previously allocated with malloc,
calloc or realloc to be reallocated.
If this is NULL, a new block is allocated and a pointer to it is returned by
the function.
size, New size for the
memory block, in bytes.
If it is 0 and ptr points to an existing block of memory, the memory
block pointed by ptr is deallocated and a NULL pointer is returned.
Return Value
A pointer to the reallocated memory block, which may be
either the same as the ptr argument or a new location.
The type of this pointer is void*, which can be cast to the desired type of data pointer in order to be dereferenceable.
If the function failed to allocate the requested block of memory, a NULL
pointer is returned.
Example
/*realloc example: rememb-o-matic */
- Code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int input,n;
int count=0;
int * numbers = NULL;
do {
printf ("Enter an integer value (0 to end): ");
scanf ("%d", &input);
count++;
numbers = (int*) realloc (numbers, count * sizeof(int));
if (numbers==NULL)
{ puts ("Error (re)allocating memory"); exit (1); }
numbers[count-1]=input;
} while (input!=0);
printf ("Numbers entered: ");
for (n=0;n<count;n++) printf ("%d ",numbers[n]);
free (numbers);
return 0;
}
The program prompts the user for numbers until a zero character is entered. Each
time a new value is introduced the memory block pointed by numbers is increased
by the size of an int.
free | function |
void free ( void * ptr ); | |
A block of memory previously allocated using a call to malloc, calloc or realloc is deallocated, making it available again for further allocations.
Notice that this function leaves the value of ptr unchanged, hence it
still points to the same (now invalid) location, and not to the null pointer.
Parameters
ptr, Pointer to a memory block previously allocated with malloc, calloc or realloc to be deallocated.
If a null pointer is passed as argument, no action occurs.
Return Value
(none)
Example
/*free example */
- Code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int * buffer1, * buffer2, * buffer3;
buffer1 = (int*) malloc (100*sizeof(int));
buffer2 = (int*) calloc (100,sizeof(int));
buffer3 = (int*) realloc (buffer2,500*sizeof(int));
free (buffer1);
free (buffer3);
return 0;
}
This program has no output. Just demonstrates some ways to allocate and free
dynamic memory using the cstdlib functions.
SOURCE : http://cplusplus.com/reference/clibrary/cstdlib
Last edited by bit0112-rokon on Tue Aug 25, 2009 7:51 am; edited 5 times in total
Re: Dynamic memory management
Erm Rokon... apparently, we can't see the header files.
Can you please include the codes in between the coding tags?
Like this:
And when you get something from internet, instead of simply writing "SOURCE: INTERNET", try "SOURCE: "The website address" (from which you gathered the information)".
And yes, it is an informative post rokon, (Except the Header files :p ) Keep up the good work.
Can you please include the codes in between the coding tags?
Like this:
- Code:
the code starting tag->[code](write your code here)[/code]<-the finishing tag
And when you get something from internet, instead of simply writing "SOURCE: INTERNET", try "SOURCE: "The website address" (from which you gathered the information)".
And yes, it is an informative post rokon, (Except the Header files :p ) Keep up the good work.
Re: Dynamic memory management
AMIT,
I have edited my post...
Sorry about source. I forgot the web site..
next time, of-course I'll mention the source...
cheers!
Rokon
I have edited my post...
Sorry about source. I forgot the web site..
next time, of-course I'll mention the source...
cheers!
Rokon
Similar topics
» Dynamic routing
» Report on dynamic instruction scheduling
» ppt slides on greedy algorithm & dynamic programming
» Study Material: Dynamic programming presentations
» Intel Pentium memory translation and LRU implementation
» Report on dynamic instruction scheduling
» ppt slides on greedy algorithm & dynamic programming
» Study Material: Dynamic programming presentations
» Intel Pentium memory translation and LRU implementation
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford