pointers - Problems with free() function in C -
i have while loop inside for loop handle strings. here structure of code: char mystring[1000]; //initialize , maybe change mystring for(/*conditions*/){ while(/*conditions*/){ if(strchr(mystring,' ') == null){ break; } char *temp = malloc(sizeof(char) * strlen(mystring)); strcpy(temp,mystring); *strchr(temp,' ') = '\0'; strcat(mystring," "); strcat(mystring,temp); free(temp); } } sometimes, code works fine, process ends , returns 3 means there error (3 return value when try use null shouldn't example mypointer->example mypointer null). after tests, found out line causing problem free(temp); . tried replace if(temp != null){free(temp);} didn't change anything. tried declare temp char temp[1000] instead of malloc , take away free(temp); line still same thing. if take away free(temp); line , still use malloc problem solved instead there huge m