我在哪里以及如何正确释放 malloc 的指针?
where and how do I correctly free malloc'd pointers?
我现在正在学习 C 编程课程,并且正在制作一个类似于 bash 的 shell。现在,我正在努力实施管道。出于这个原因,我需要 strndup
管道命令在解析它时不修改原始管道命令。最后,我尝试 free
malloc
' 指针,但我的程序无法正常工作。有人可以告诉我我做错了什么吗?下面是我的解析器的 while 循环代码:
while (pipeSeparatedCommand != NULL) {
char *duplicateCmdForPiping = strndup(pipeSeparatedCommand, strlen(pipeSeparatedCommand)); // duplicates command
pipeSeparatedCommand = strtok(NULL, "|"); // starts at the NULL Character that was tokenized
char *token = strtok(duplicateCmdForPiping, " "); // finds the first space in the command, tokenizes the arguments by this space
int index = 0; // counts the number of commands and arguments
while (token != NULL) {
if ((input != NULL) || (output != NULL)) { // advances to the next argument if input or output is found (does not include them in args)
token = strtok(NULL, " ");
continue;
}
commandArgsCount[index]++;
commandArray[numberOfPipes][index] = token; // sets the argument index equal to the address of token (containing the token)
token = strtok(NULL, " "); // token will search for the next delimiter from the last delimiter end spot
index++;
}
commandArray[numberOfPipes + 1][index] = NULL; // prevents the args array from collecting garbage in memory.
if (pipeSeparatedCommand != NULL) // catches the zero case
numberOfPipes++; // this begins at zero, increments if more pipes need to be added
free(duplicateCmdForPiping);
duplicateCmdForPiping = NULL;
}
查看 strtok()
的手册页。它说该功能将 return "a pointer to the beginning of each subsequent token in the string." 我建议您更改行
commandArray[numberOfPipes][index] = token;
至
// make sure you `#include <string.h>`
strcpy(commandArray[numberOfPipes][index], token);
// OR
commandArray[numberOfPipes][index] = strdup(token);
顺便说一下,请避免在 C/C++ 中使用驼峰式变量命名约定。它使您的代码极难阅读。
我现在正在学习 C 编程课程,并且正在制作一个类似于 bash 的 shell。现在,我正在努力实施管道。出于这个原因,我需要 strndup
管道命令在解析它时不修改原始管道命令。最后,我尝试 free
malloc
' 指针,但我的程序无法正常工作。有人可以告诉我我做错了什么吗?下面是我的解析器的 while 循环代码:
while (pipeSeparatedCommand != NULL) {
char *duplicateCmdForPiping = strndup(pipeSeparatedCommand, strlen(pipeSeparatedCommand)); // duplicates command
pipeSeparatedCommand = strtok(NULL, "|"); // starts at the NULL Character that was tokenized
char *token = strtok(duplicateCmdForPiping, " "); // finds the first space in the command, tokenizes the arguments by this space
int index = 0; // counts the number of commands and arguments
while (token != NULL) {
if ((input != NULL) || (output != NULL)) { // advances to the next argument if input or output is found (does not include them in args)
token = strtok(NULL, " ");
continue;
}
commandArgsCount[index]++;
commandArray[numberOfPipes][index] = token; // sets the argument index equal to the address of token (containing the token)
token = strtok(NULL, " "); // token will search for the next delimiter from the last delimiter end spot
index++;
}
commandArray[numberOfPipes + 1][index] = NULL; // prevents the args array from collecting garbage in memory.
if (pipeSeparatedCommand != NULL) // catches the zero case
numberOfPipes++; // this begins at zero, increments if more pipes need to be added
free(duplicateCmdForPiping);
duplicateCmdForPiping = NULL;
}
查看 strtok()
的手册页。它说该功能将 return "a pointer to the beginning of each subsequent token in the string." 我建议您更改行
commandArray[numberOfPipes][index] = token;
至
// make sure you `#include <string.h>`
strcpy(commandArray[numberOfPipes][index], token);
// OR
commandArray[numberOfPipes][index] = strdup(token);
顺便说一下,请避免在 C/C++ 中使用驼峰式变量命名约定。它使您的代码极难阅读。