Sample C Program for Dynamic Memory Allocation - malloc()

This section provides a sample C program using dynamic memory allocation function, malloc().

Another main feature of the C language is dynamic memory allocation using the built-in function, malloc(). Here is sample code, sort.c, that reads in a text file, stores each line as a string in dynamically allocate memory, and sort all lines (Source: "The C Book" by Mike Banahan, et al.):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSTRING    50   /* max no. of strings */
#define MAXLEN       80   /* max length. of strings */
void print_arr(const char **p_array);
void sort_arr(const char **p_array);
char *next_string(void);
main(){
   char **p_array;
   int nstrings;   /* count of strings read */
   p_array = (char **)malloc(
      sizeof(char *[MAXSTRING+1]));
   if(p_array == 0){
      printf("No memory\n");
      exit(EXIT_FAILURE);
   }
   nstrings = 0;
   while(nstrings < MAXSTRING &&
      (p_array[nstrings] = next_string()) != 0){
      nstrings++;
   }
   p_array[nstrings] = 0; /* terminate p_array */
   sort_arr(p_array);
   print_arr(p_array);
   exit(EXIT_SUCCESS);
}
void print_arr(const char **p_array){
   while(*p_array)
      printf("%s\n", *p_array++);
}
void sort_arr(const char **p_array){
   const char **lo_p, **hi_p, *tmp;
   for(lo_p = p_array; *lo_p != 0 && *(lo_p+1) != 0; lo_p++){
      for(hi_p = lo_p+1; *hi_p != 0; hi_p++){
      if(strcmp(*hi_p, *lo_p) >= 0) continue;
      tmp = *hi_p;
      *hi_p = *lo_p;
      *lo_p = tmp;
      }
   }
}
char *next_string(void){
   char *cp, *destination;
   int c;
   destination = (char *)malloc(MAXLEN);
   if(destination != 0){
      cp = destination;
      while((c = getchar()) != '\n' && c != EOF){
         if(cp-destination < MAXLEN-1) *cp++ = c;
      }
      *cp = 0;
      if(c == EOF && cp == destination) return(0);
   }
   return(destination);
}

Here is a test of sort.c on sorting the first 10 lines of sort.c source code:

\tmp>head sort.c | \local\tcc\tcc -run sort.c
   char **p_array;
#define MAXLEN       80   /* max length. of strings */
#define MAXSTRING    50   /* max no. of strings */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *next_string(void);
main(){
void print_arr(const char **p_array);
void sort_arr(const char **p_array);

Table of Contents

 About This Book

 1957 - FORTRAN Language Developed by IBM

 1970 - UNIX Operating System Developed by AT&T Bell Labs

 1971 - FTP Protocol Created by Abhay Bhushan

1972 - C Language Developed by Dennis Ritchie

 What Is C Language?

 TCC - Tiny C Compiler

 Compilation and Execution Process of C Programs

 GNU C Compiler

 Sample C Program for Arrays and Pointers

Sample C Program for Dynamic Memory Allocation - malloc()

 1976 - vi Text Editor Developed by Bill Joy

 1977 - Apple II Designed by Steve Jobs and Steve Wozniak

 1977 - The Bourne Shell Developed by Stephen Bourne

 1978 - The C Shell Developed by Bill Joy

 1978 - Bash (Bourne-Again Shell) Developed by Brian Fox

 1979 - The Tcsh (TENEX C Shell) Developed by Ken Greer

 1983 - "Sendmail" Mail Transfer Agent Developed by Eric Allman

 1984 - Macintosh Developed by Apple Inc.

 1984 - X Window System Developed a MIT Team

 1991 - WWW (World Wide Web) Developed by Tim Berners-Lee

 1991 - Gopher Protocol Created by a University of Minnesota Team

 1995 - Java Language Developed by Sun Microsystems

 1995 - PHP: Hypertext Preprocessor Created by Rasmus Lerdorf

 2002 - .NET Framework Developed by Microsoft

 References

 PDF Printing Version