c - C 结构中的指针问题

标签 c pointers

我有一个关于 c 指针的问题。我目前正在构建自己的 shell。根据 valgrind 的说法,我有一个与指针有关的内存泄漏。我尝试了许多不同的方法来释放内存,但这些方法通常都会导致段错误。我的代码非常复杂,所以我不会发布它。但本质上我拥有的代码的一部分是:

void seperateCommands(char *input[]){
   process *processArr[10];
   char **currInstruction;
   currInstruction = calloc(300 , sizeof(char *));
   if(input[inputIndex] == NULL){
   currInstruction[instructionIndex] = NULL;
  //printString(currInstruction);
    process *tempP;
    tempP = (process *)calloc(1,sizeof(process));
    tempP->instruction = currInstruction;
    processArr[processIndex] = tempP;
    processIndex++;
     break;
    }
    }
    job *j;
    j = (job *) calloc(1,sizeof(job));
    j->currProcess = processArr[0];

以及免费方法:

 void free_job(job* j){
      int i = 0;
      if(j->currProcess != NULL){
        free(j->currProcess->instruction);
      }
      if(j->currProcess != NULL){
        free(j->currProcess);
      }
      if(j->twoProcess){
        if(j->secondProcess != NULL){
          free(j->secondProcess->instruction);
          free(j->secondProcess);
        }
      }
      for (i = 0; i < 300; i++){
        free(j->instruction[i]);
      } 
      free(j->instruction);
      free(j);
    }

还有两个结构:

typedef struct job{
    char **instruction;
    struct job* nextJob; //next active
    pid_t pgid; //-1 if not set
    int status; //0 if running, 1 if completed, 2 if stopped
    struct process* currProcess; //max of 2 with pipes
    struct process* secondProcess;
    char state; //'F' if forground or 'B' if background
    int twoProcess;
} job;

typedef struct process{
    char **instruction; //current instruction to be executed
    int *inputPipe;
    int *outputPipe;
    char *inputFile;
    char *outputFile;
    int status; //status
    int completed; //1 if completed
    char stopped; //1 if stopped
    pid_t pid; //pid of process
} process;

typedef struct job{
    char **instruction;
    struct job* nextJob; //next active
    pid_t pgid; //-1 if not set
    int status; //0 if running, 1 if completed, 2 if stopped
    struct process* currProcess; //max of 2 with pipes
    struct process* secondProcess;
    char state; //'F' if forground or 'B' if background
    int twoProcess;
} job;

不知何故,valgrind 表明 tempP 和 currProcess 没有释放。然而,由于我通过创建作业来引用这些指针,因此我最终使用 free_job 方法释放了这些值。如果我尝试添加 free(tempP) 或 free (currinstruction),则会出现段错误。任何帮助将非常感激。几个小时以来我一直在尝试不同的事情。

关于基于概念的旁注,如果我有:

char **input;
char **sample = malloc(sizeof(char*) * 30);
input = sample; 
free(input)

我还需要免费 sample 吗?

最佳答案

您的意图尚不清楚,例如,在您的侧面示例中,您似乎分配了一个包含 30 个字符的 char 数组。为此,您不需要执行指针的指针,这就足够了:

char *input;
char *sample = malloc(sizeof(char*) * 30);
input = sample; 
free(input)

并且您不需要释放样本,但是当您释放输入时样本将无效,因为它们引用相同的分配 block 。因此,不要尝试在“释放(输入)”之后重复使用示例

关于c - C 结构中的指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26682964/

相关文章:

c++ - 删除从函数返回的指针?

c - 来自 DIR 的统计文件 *

c - C语言中删除结构体

c++ - 带有指针数据的 OpenGL glBufferData

c++ - 有效取消引用空指针的规则是什么?

iphone - iOS 指针问题

c++ - 从链表中删除一个节点运行但不删除任何东西

c - printf中的格式化字符串攻击

c++ - Qt4中如何使用libavcodec?

c - 使用clone()和printf的段错误