Dynamic Memory and Pointers in C

One of the blessings (some would say curse) of C is the degree to which we are able to micromanage dynamic memory in C.  By allocating and deallocating memory dynamically our programs can execute with greater flexibility and efficiency. Liked lists and queues, for instance, would be impossible to implement without dynamic memory allocation. The use of pointers is integral to tracking dynamically allocated memory in C.

Dynamic memory allocation in C begins with the use of malloc() or a similar function to allocate memory. This memory is then used by the program via a pointer. Finally, we deallocate the memory use the free() function.

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

int main(void){
    
    double *d = (double*)malloc(sizeof(double));
    int *i = (int*)malloc(sizeof(int));
    
    *i = 2600;
    *d = 5.11;

    printf("%d\n", *i);
    printf("%f\n", *d);

    return 0;
    
}

The malloc() function accepts an argument that specifies the number of bytes to allocate. The malloc() function then returns a pointer to the memory allocated on the heap, unless it fails, in which case it returns a null pointer.

It’s important to be mindful of when we should and shouldn’t use the dereference operator with pointers to dynamic memory.

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

int main(void){
    
    long long *longlong;
    char *c;
    
    
    longlong = (long long*) malloc(sizeof(long long));
    c = (char*) malloc(sizeof(char));
    
    *longlong = 56007800;
    *c = 'd';
    
    printf("%lld\n", *longlong);
    printf("%c\n", *c);
    
    return 0;
   

Finally, we must always remember to use the free() function to deallocate the memory when it is no longer needed. Unlike memory allcoated on the stack, we must explicitly free up the memory when we are done with it. Every time the malloc() function is called, we must at some point issue a corresponding call to the free() function.

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

int main(void){
    
    int *a;
    double *b;
    char *c = (char *) malloc(sizeof(char));
    
    b = (double *) malloc(sizeof(double));
    a = (int *) malloc(sizeof (int));
    
    
    *a = 845;
    *b = 846.847;
    *c = 'd';
    
    printf("%d\t%f\t%c", *a, *b, *c);
    
    //important!!!
    free(a);
    free(b);
    free(c);
    
    return 0;
    
}

If we do not remember to use the free() function, we can have memory leaks. A memory leak happens with allocated memory is never used again but is not free.  This represents, at the very least, an inefficient use of system memory, and at worst can cause the program to crash. Memory leaks can also occur if we assign the pointer to a new memory location without freeing the memory it was previously pointing to.

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


int main(void){
    
    int *i = (int*) malloc(sizeof(int));
    
    *i = 42;
    
    printf("%d", *i);
    printf("\t(%p)\n", i);
    
    //whoops!
    i = (int*) malloc(sizeof(int));
    
    printf("%d", *i);
    printf("\t(%p)\n", i);
    
    free(i);
    
    return 0;
    
}

In our final program, we will allocate memory to store a string. We will use then use the strcpy() function to store a string of characters in the allocated memory space. We can then output the string to the console using printf() with the %s conversion specifier.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
    
    char *s = (char *) malloc(sizeof(char)*15);
    
    //note strcpy() function
    //is in string.h header file
    strcpy(s, "Saluton Mundo!");
    
    printf("%s\n", s);
    
    free(s);
    
    return 0;
    
}

I have a couple Kindle books for sale at Amazon; they’re readable both on a Kindle as well as by using the Kindle app on tablet or computer: http://www.amazon.com/Al-Jensen/e/B008MN382O/

 

 

 

 

 

 

 

Advertisements

One thought on “Dynamic Memory and Pointers in C

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s