/** * @Comment: WSU - Fall 06 - CS 3830-0 * @Title: p3p4.c - Project 3 prog 4 * @Author: Vincenzo Maggio */ #include #include #include #include int main( void) { int i, ic; signed short index; char dest[10], *myarray, str[6] = "Hello\0"; size_t len; system("color f0"); MENU: system("cls"); gotoxy( 29, 2); printf("CS 3830 - Prj. 3 Prg. 4"); gotoxy( 1, 4); printf(" 1. Array index (this crashes the program !)\n"); printf(" 2. malloc()\n"); printf(" 3. memcpy()\t\t0. Exit\n"); printf(" 4. strncpy()\n"); printf("\n Choice: "); while ( !kbhit()); ic = getchar(); printf("\n"); switch ( ic) { case '1': myarray = (char *) malloc( 32800); if ( myarray) { memset( myarray, 65, 32800); for ( i = index = 32765; i < 32800; i++, index++) /* THIS CODE CRASHES THE PROGRAM !!! */ printf("%c ", myarray[index]); free( myarray); } break; case '2': index = 32766; index += 10; myarray = (char *) malloc( index); printf( "\n Value of pointer returned by malloc: %p\n\n", myarray ); break; case '3': index = 32767 + 10; /* <-- signed short */ if ( index > 0 ) memcpy( dest, str, index); /* index is always less than zero - so memcpy crashes */ printf(" memcpy lenght value: %d\n\n", index); break; case '4': index = 32767 + 10; /* <-- signed short */ strncpy( dest, str, index); /* index is always less than zero so strncpy just exits */ printf(" strncpy lenght value: %d\n\n", index); break; case '0': goto EXIT; } system("pause"); goto MENU; EXIT: printf("\n Compiler:\tBorland BuilderX Personal 1.0.0.1786\n"); printf(" Platform:\tXP Pro + SP2 (+ circa 72 patches)\n\t\tThanks for using prj 3 prog 4 !\n\n\t"); system("pause"); system("color 0f"); return 0; }