Lecture Dynamic Memory Allocation

Document Sample
Lecture Dynamic Memory Allocation Powered By Docstoc
					Lecture 15: Storage

       Homework: Read
     Chapter 13 of Barclay
        Lecture 15: Outline
 Memory management
  – Dynamic memory allocation
    • Functions: malloc, calloc, realloc
  – De-allocating memory
    • Functions: free
          Dynamic Memory
 Two different ways to look at memory
  – Transparent to user
     • Done by the compiler as well as the OS.
  – User-defined
     • Memory allocated by the user.
   Dynamic Memory (contd.)
 Why allocate memory ??
  – Required by program for permanent variables.
     • Compiler arranges for memory at compiler time.
     • OS + program allocate memory at run time.
     • Memory exists for life of program.
   Dynamic Memory (contd.)
 Why allocate memory ??
  – Reserve working space for program.
     • Working space is space required for execution of
       program instructions (e.g: arithmetic, logical
     • Normally maintained using a stack or a heap.
   Dynamic Memory (contd.)
 Stacks and heaps:
  – Data structures with specific purposes.
  – A stack is an organized data structure, which
    follows the LIFO (Last In First Out)
     • A data item which enters the stack last is the first
       to be taken out.
  – A heap is just a collection in memory that is
    manipulated as the user/system chooses.
    STACK Manipulation

4      After removal     4
3                        3
2                        2
1                        1
HEAP Manipulation

  1 2 3 4 5 6

          After removal

   1 2     4 5 6
   Dynamic Memory (contd.)
 Why do we need dynamic allocation ?????
  – There are times when we don‟t know how
    much memory is required.
     • Employee data base that sorts employee records
       and adds new employees.
     • Program accepting command line input defining
       size of matrices.
     • Directory trees where files/folders can be
   Dynamic Memory (contd.)
 Programmer arranges to allocate
  appropriate amount of memory.
  – Reads command line arguments.
  – Processes input flag to add new record to
    Dynamic Memory (contd.)
 E.g:
  – char response;
  – printf (“Add a new record y/n ?”);
  – scanf (“%c”, &response);
  – if (response == „y‟ || response == „Y‟)
  – {allocate required memory}
   Dynamic Memory (contd.)
 Also used by the OS !!!
 Heap is managed dynamically through the
  use of dynamic memory.
  – Used mostly to calculate the values of
    expressions, assign memory blocks etc.
 Stacks are also managed dynamically.
  – Used mostly for function calls, scope of
    variables etc.
    Dynamic Memory (contd.)
 Functions available to allocate memory:
  – void *malloc (size_t size)
  – Returns a void pointer to a contiguous block in
    memory of size equal to size bytes.
  – ** Size is always taken to be in bytes.
    Dynamic Memory (contd.)
 E.g: x = malloc (10);
  – If successful, returns address of the block of
    10 bytes in memory.
   x points to the beginning of this block of mem.

               Size of memory block is 10 bytes.
              Notes on malloc
 On success returns pointer (address of
  beginning of block of memory just
 On error, returns NULL (keyword in C).
  – Errors:
     • Insufficient block size.
     • size = 0.
     Notes on malloc (contd.)
 Memory is allocated from the heap.
 Allocated space is not initialized, so
  contains garbage.
 No connection exists with type of data to
  be stored in the memory space just created.
 Contained in the header file: stdlib.h or
     Notes on malloc (contd.)
 Why is a void pointer returned ???
  – malloc just allocates memory which consists
    of some number of bytes.
  – Memory as such has no data type, unless the
    user or the running program chooses to
    associate a data type with the block of memory
    just created.
     Notes on malloc (contd.)
 How to assign a data type to the memory
  just created ???
  – Use type casting.
  – Once type cast to a data type, memory takes
    on the meaning of that data type.
  – Amount of data types that can be represented
    depends on the size of the data type.
     Notes on malloc (contd.)
 E.g:
  – double *x; // x is a pointer to double as malloc
    returns an address.
  – x = (double *) malloc (100); // type casting the
    memory to be of type pointer to double.
  – No. of doubles = 100/8 = 12 !!!
  – We can clearly see a pit fall of malloc out
     Notes on malloc (contd.)
 More
  – double *x;
  – x = (double *) malloc (100 * sizeof (double));
  – This is the correct declaration, gives us 800
    bytes in memory, where we have 100 doubles.
  – Always better to use sizeof instead of the
     • x = (double *)malloc (100 * 8); // assume that
       double is 8 bytes 
          Dynamic Memory
 Other memory allocation functions:
  – void *calloc (size_t count, size_t size)
  – Same as malloc, but is contained in stdlib.h
  – Allocated space is automatically initialized
    to zero.
  – Still have to cast void pointer to correct type.
             Notes on calloc
 Advantages over malloc:
  – Allocated memory is automatically initialized.
  – The semantics of size and count are separated,
    making it easier for the user during
     • Less prone to making errors while declaration for
       the correct amount of size and/or count required.
         Notes on calloc (contd.)
 E.g:
  int *x;
  x = (int *) calloc (10, sizeof (int));

  Gives us a block of 10 integers in memory.

  char *x;
  x = (char *) calloc (10, sizeof (char));
  Gives us a block of 10 characters in memory.
    Notes on malloc and calloc
 We can also assign space for structures.
   – E.g:
   struct test {
    int x;
    int y;

   struct test *x;

   x = (struct test *) calloc (10, sizeof (struct test));
          Dynamic Memory
 More functions:
  – void *realloc (void *blk, size_t newsize)
  – blk is pointer to previously allocated block of
    memory using either malloc or calloc.
  – Attempts to reallocate the block of memory.
  – Resizes the memory to newsize (can be greater
    or smaller than previously designated size).
            Notes on realloc
 There are times, when after assigning
  memory, we find that we have assigned
  more/less than what we require.
  – realloc to the rescue.
 On success returns pointer to new location.
 On success, copies data from old location
  to new location.
 On error returns NULL.
      Notes on realloc (contd.)
 Pit falls:
   – size = 0 frees memory block and returns
   – Once memory is re-allocated, the old location
     in memory is invalid.
      • Both memory locations (new and old), are the
        same now (= new).
      • Example follows.
     Notes on realloc (contd.)
 E.g:
     # include <stdio.h>
     int main ()
       int *x, *y;
       x = (int *) malloc (1 * sizeof (int));
       *x = 1; // value of *x is 1 here.
       y = (int *) realloc (x, 1);
      *y = 10; // value of *x and *y are same = 10 !!!
       printf ("%d\n", *y);
       printf ("%d\n", *x);
    Notes on realloc (contd.)


           Dynamic Memory
 Memory that is allocated can also be freed
  to the heap.
 Only dynamically allocated memory can
  be freed.
  – E.g:
     • int x; // memory for x is statically assigned here,
       hence it cannot be freed.
   Dynamic Memory (contd.)
 When finished using dynamically allocated
  memory, free it.
  – Good practice to avoid memory leaks.
     • Growing loss of memory due to unreleased
     • Could prevent program from running properly, due
       to lack of memory.
     • Can run out of memory.
   Dynamic Memory (contd.)
 Function to free memory:
  – void free (void *blk)
  – De-allocates memory block previously
    allocated with malloc, calloc or realloc.
  – blk is pointer to existing memory block
    already allocated.
              Notes on free
 E.g:
  char *x;

  x = (char *) malloc (10 * sizeof (char));

  free (x); // frees the memory just assigned.
                Notes on free
 No return value.
 Available in the header file stdlib.h
 Never free a memory block that is not
  dynamically allocated.
  – E.g:
     • int *x;
     • free (x); // error
         Notes on free (contd.)
 Once block has been freed, do not attempt
  to access it.
  – E.g:
     •   char *x;
     •   x = (char *) malloc (10 * sizeof (char));
     •   free (x);
     •   strcpy (x, “welcome”); // error as memory has
         already been freed.
       Notes on free (contd.)
 Free memory only when you are sure of
  not using it again elsewhere in the
 Cannot free memory already freed.
  – E.g:
     • char *x = (char *) malloc (10 * sizeof (char));
     • free (x);
     • free (x); // ERROR
                        Pit falls
 Memory is being manipulated directly, so
  any error will stop your program.
 Free memory at the end of the program.
 Use realloc as little as possible.
     • As once realloc is executed, new location and old
       location map to the same address in memory
     char *x = (char *) malloc (10 * sizeof (char));
     char *y = realloc (x, 15);
     free (x); // y is also freed as x  y.
                       Pit falls
 Dangling pointer:
   – E.g:
   char *x, *y;

   x = (char *) malloc (10 * sizeof (int));
   y = (char *) malloc (10 * sizeof (int));

   x = y; // cannot access what was in x anymore, so that
     block is always lying around in memory with no way
     to free it until program terminates.
               Next Lecture
 Formatted file I/O.
  – Opening a file.
  – Closing a file.
  – Random/Direct access from file.
  – Reading/Writing characters in a file.
  – Reading/Writing strings from a file.
  – Reading/Writing bytes from a file.