DATA STRUCTURES IN CUNIT-1 LECTURE NO:1

BASIC CONCEPTS

Pointers

Declaring a pointer variable is quite similar to declaring an normal variable all you have to do is to insert a star '*' operator before it.

General form of pointer declaration is -

type* name;

where type represent the type to which pointer thinks it is pointing to.

Pointers to machine defined as well as user-defined types can be made

Pointer Intialization: variable_type *pointer_name = 0;

or

variable_type *pointer_name = NULL;

char *pointer_name = "string value here";

Dynamic Memory Allocation

The C programming language manages memory statically, automatically, or dynamically. Static-duration variables are allocated in main memory, usually along with the executable code of the program, and persist for the lifetime of the program; automatic-duration variables are allocated on the stack and come and go as functions are called and return. For static-duration and automatic-duration variables, the size of the allocation must be compile-time constant (except in C99, which allowed variable-length automatic arrays[5]). If the required size is not known until run-time (for example, if data of arbitrary size is being read from the user or from a disk file), then using fixed-size data objects is inadequate.

The lifetime of allocated memory can also cause concern. Neither static- nor automatic-duration memory is adequate for all situations. Automatic-allocated data cannot persist across multiple function calls, while static data persists for the life of the program whether it is needed or not. In many situations the programmer requires greater flexibility in managing the lifetime of allocated memory.

These limitations are avoided by using dynamic memory allocation in which memory is more explicitly (but more flexibly) managed, typically, by allocating it from the free store (informally called the "heap"), an area of memory structured for this purpose. In C, the library function malloc is used to allocate a block of memory on the heap. The program accesses this block of memory via a pointer that malloc returns. When the memory is no longer needed, the pointer is passed to free which deallocates the memory so that it can be used for other purposes.

malloc() can also avoid system calls by utilisingfast bins.[6]

Some platforms provide library calls which allow run-time dynamic allocation from the C stack rather than the heap (e.g. alloca()[7]). This memory is automatically freed when the calling function ends.

Differences between malloc() and calloc()

There are two differences between these functions. First, malloc() takes a single argument (the amount of memory to allocate in bytes), while calloc() needs two arguments (the number of variables to allocate in memory, and the size in bytes of a single variable). Secondly, malloc() does not initialize the memory allocated, while calloc() guarantees that all bytes of the allocated memory block have been initialized to zero.

Usage example

Creating an array of ten integers with automatic scope is straightforward in C:

int array[10];

However, the size of the array is fixed at compile time. If one wishes to allocate a similar array dynamically, the following code can be used:

int * array = malloc(10 * sizeof(int));

This computes the number of bytes that ten integers occupy in memory, then requests that many bytes from malloc and assigns the result to a pointer named array (due to C syntax, pointers and arrays can be used interchangeably in some situations).

Because malloc might not be able to service the request, it might return a null pointer and it is good programming practice to check for this:

int * array = malloc(10 * sizeof(int)); if (NULL == array) { fprintf(stderr, "malloc failed\n"); return(-1); }

When the program no longer needs the dynamic array, it should call free to return the memory it occupies to the free store:

free(array);

The memory set aside by malloc is not initialezed and may contain cruft: the remnants of previously used and discarded data. After allocation with malloc, elements of the array are uninitialized variables. The command calloc will allocate and clear the memory in one step:

int * array = calloc(10, sizeof (int));

allocates a region of memory large enough to hold 10 integers, and sets to zero all the bytes within that memory space.

DEPT. OF CSE/ISENAVODAYA INSTITUTE OF TECHNOLOGY RAICHUR