Dynamic memory management

View previous topic View next topic Go down

Dynamic memory management

Post by BIT0112-Rokon on Sun Aug 23, 2009 7:58 pm

Dynamic memory management:

malloc

function

void * malloc ( size_t size );


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

/*malloc example: string generator*/

Code:
     
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i,n;
  char * buffer;

  printf ("How long do you want the string? ");
  scanf ("%d", &i);

  buffer = (char*) malloc (i+1);
  if (buffer==NULL) exit (1);

  for (n=0; n<i; n++)
    buffer[n]=rand()%26+'a';
  buffer[i]='\0';

  printf ("Random string: %s\n",buffer);
  free (buffer);

  return 0;
}
 

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 );


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 */

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 );


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 */
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
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: Dynamic memory management

Post by BIT0122-Amit on Mon Aug 24, 2009 10:53 pm

Erm Rokon... apparently, we can't see the header files.
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.

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Dynamic memory management

Post by BIT0112-Rokon on Tue Aug 25, 2009 7:27 am

AMIT,
I have edited my post...
Sorry about source. I forgot the web site..
next time, of-course I'll mention the source...

cheers! geek
Rokon
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: Dynamic memory management

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum