在C中使用free()后的分段错误

时间:2017-10-31 23:40:50

标签: c segmentation-fault malloc free

我在inputCommandString方法中使用malloc()动态地将内存分配给变量getInputCommand(),但是当我在main方法中使用free()函数释放内存时,我就是得到分段错误。我尝试了几乎所有可用于stackOverflow的解决方案,但它仍然没有用。我甚至检查过我传递给free()的变量的地址与malloc()返回的地址相同。任何人都可以看看我的下面的代码,并解释我这里有什么问题?

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "unistd.h"
#include "errno.h"
#define COMMAND_SIZE 200;

struct Command{
 char* name; //name of caommand
 char* option; //options passed tocommand
 char* arguements[2]; //array to store arguements passed to command
 char* helper; //helper words used to write command like "to" "from" 
 etc
 int no_of_arguements; //
};

struct Command structuredInputCommand;
char* inputCommandString; //command inputted by user
char* commandTokensArray[10]; //maximum number of tokens is 10
char* commandDelimiter = " "; //delimiter used to tokenize input 
command
int no_of_command_tokens;

void displayCurrentDirectory(){

 char* currentDirectory;
 currentDirectory = (char*)malloc(200*sizeof(char));
 getcwd(currentDirectory, (size_t)200);
 fprintf(stdout, "%s>\n", currentDirectory);
 printf("%d\n", errno);
 //printf("%s>\t\n", currentDirectory);
}

char* getCommandInput(){

 char* commandToken; //token from command
 int i = 0;
 char* tempInputString;
 inputCommandString = NULL;
 no_of_command_tokens = 0;
 inputCommandString = (char*)malloc(200*sizeof(char));
 scanf("%[^\n]s\n", inputCommandString);
 //printf("%p", inputCommandString);
 strcpy(tempInputString, inputCommandString);
 //fgets(command, sizeof(command), stdin);
 printf("%s\n", inputCommandString);
 commandToken = strtok(inputCommandString, commandDelimiter);

 while(commandToken != NULL){
  no_of_command_tokens++;
  commandTokensArray[i] = commandToken;
  commandToken = strtok(NULL, commandDelimiter);
  i++;
 }
 return tempInputString;
}

void parseInputCommand(){

 int i = 0, j = 0;
 int optionFlag = 0;
 if(no_of_command_tokens == 2){
  structuredInputCommand.name = commandTokensArray[0];
  structuredInputCommand.arguements[0] = commandTokensArray[1];
 }
 else if (no_of_command_tokens == 3) {
  structuredInputCommand.name = commandTokensArray[0];
  structuredInputCommand.option = commandTokensArray[1];
  structuredInputCommand.arguements[0] = commandTokensArray[2];
 }
 else if (no_of_command_tokens == 4) {
  if(commandTokensArray[0] == "go"){
   structuredInputCommand.name = commandTokensArray[0];
   structuredInputCommand.helper = commandTokensArray[1];
   structuredInputCommand.option = commandTokensArray[2];
   structuredInputCommand.arguements[0] = commandTokensArray[3];
  }
  else{
   structuredInputCommand.name = commandTokensArray[0];
   structuredInputCommand.helper = commandTokensArray[2];
   structuredInputCommand.arguements[0] = commandTokensArray[1];
   structuredInputCommand.arguements[1] = commandTokensArray[3];
  }
 }
 else if(no_of_command_tokens == 5){
  structuredInputCommand.name = commandTokensArray[0];
  structuredInputCommand.helper = commandTokensArray[1];
  structuredInputCommand.option = commandTokensArray[2];
  structuredInputCommand.arguements[0] = commandTokensArray[3];
  structuredInputCommand.arguements[1] = commandTokensArray[4];
 }

 printf("command name: %s\ncommand option: %s\narguememnt: 
 %s\narguement: %s\nhelper: %s", structuredInputCommand.name, 
 structuredInputCommand.option, structuredInputCommand.arguements[0], 
 structuredInputCommand.arguements[1], structuredInputCommand.helper);
}

int main(int argc, char const *argv[]) {

 char* tempInputString;
 displayCurrentDirectory();
 tempInputString = getCommandInput();

 while(strcmp(tempInputString, "exit") != 0){
  if(no_of_command_tokens < 2 || no_of_command_tokens > 5){

   perror("INVALID COMMAND");

   // for(int i=0; i<no_of_command_tokens; i++){
   //   free(commandTokensArray[i]);
   // }
  }

  else{
   parseInputCommand();
  }

  displayCurrentDirectory();
  //printf("%p\n", inputCommandString);
  free(inputCommandString);
  //inputCommandString = NULL;
  tempInputString = getCommandInput();
 }


 //printf("%d\n", errno);
 return 0;
}

1 个答案:

答案 0 :(得分:0)

  1. 从getCommandInput()返回的char *是tempInputString,space 没有使用malloc为它分配,另外你试图将一个字符串复制到tempInputString指向的内存中,同样不会释放所需的空间。
  2. 您没有释放为函数内部的inputCommandString 分配的内存,这将导致重大内存泄漏。 char * getCommandInput(){

    char* getCommandInput(){
    
      char* commandToken; //token from command
      int i = 0;
      char* tempInputString = (char*)malloc(200*sizeof(char));
      no_of_command_tokens = 0;
      inputCommandString = (char*)malloc(200*sizeof(char));
      scanf("%[^\n]s\n", inputCommandString);
      //printf("%p", inputCommandString);
      strcpy(tempInputString, inputCommandString);
      //fgets(command, sizeof(command), stdin);
      printf("%s\n", inputCommandString);
                            //better we work on the copy
      commandToken = strtok(tempCommandString, commandDelimiter);
    
      while(commandToken != NULL){
      no_of_command_tokens++;
      commandTokensArray[i] = commandToken;
      commandToken = strtok(NULL, commandDelimiter);
      i++;
     }
       free(tempInputString);
       return inputCommadString;
     }