4681l2

Listing 2. mytest00.c example program

#include <stdlib.h>
#include <stdio.h>
 
int
main (int argc, char **argv)
{
        char    *msg = malloc (4);         // Allocate 4 bytes
 
        strcpy (msg, "hello Linux users"); // Overflow the allocated memory
        printf ("%s\n", msg);
        free (msg);                        // Free the allocated memory
        strcpy (msg, "hello again");       // Write to free'd memory
        printf ("%s\n", msg);
        free (msg);                        // Free the free'd memory
        realloc (msg, 2);                  // Reallocate free'd memory
        strcpy (msg, "hello there");       // Writing to erroneous memory
        printf ("%s\n", msg);
 
        return 0;
} 

