// Programa com erros de acesso a memória (exemplo)
// Carlos Maziero, DInf/UFPR, Abril 2017

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

#define MAXNAMES 100
#define NAMESIZE 64

int main ()
{
  // vetor com até MAXNAME nomes
  char *name[MAXNAMES] ;

  int i,j ;

  // aloca MAXNAME strings de NAMESIZE caracteres cada
  for (i=0; i < MAXNAMES; i++)
    name[i] = malloc (NAMESIZE) ;

  // preenche os nomes alocados com "### ...###"
  for (i=0; i <= MAXNAMES; i++)		// erro
    for (j=0; j <= NAMESIZE; j++)	// erro
    {
      name[i][j] = '#' ;
      name[i][NAMESIZE] = 0 ;
    }

  // imprime os nomes
  for (i=0; i <= MAXNAMES; i++)		// erro
    printf ("%3d: %s\n", i, name[i]) ;

  // libera os nomes
  for (i=0; i <= MAXNAMES; i++)		// erro
  {
    free (name[i]) ;
    name[i] = NULL ;
  }

  return (0) ;
}

