I've started messing around trying to code a mud just for the heck of it, but I'm having a problem with malloc. It exits the program with a 'Segmentation fault' error.
I'm compiling with gcc version 3 and running on RedHat 9.0
I've trimmed the code to illustrate the part that's causing the problem:
Any help would be very much appreciated.Code:char cmd[MAX_CMD_LEN]; char cmd_cpy[MAX_CMD_LEN]; char *cmd_val; while("true") { /* Allocate the exact amount of memory we need */ cmd_val = (char *) malloc(MAX_CMD_LEN); /* get the command from the user */ fgets( cmd, MAX_CMD_LEN, stdin ); if (cmd_val == NULL) { printf("Couldn't allocate any memory"); return; } /* make a copy for tokenising */ strcpy(cmd_cpy, cmd); cmd_val = strtok(cmd_cpy, " "); //first token of cmd_cpy /* free the memory otherwise this loop will eventually cause the program to crash */ free(cmd_val); cmd_val = NULL; } }
--
powerboy