- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写自己的 shell,但有些东西无法与分配和空闲一起使用。我一遍又一遍地检查我的代码,我无法理解为什么我的自由函数给我带来了问题......当我不使用它时,一切正常,但是当我使用它时,代码在第二次迭代后停止工作。 ..我非常感谢您的帮助...
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h> //for PATH_MAX - longest path name permited in linux
#include <sys/wait.h>
#include <fcntl.h>
typedef struct{
char **parametersArray; //this array contains the command and the parameters
int size_of_array; //the number of strings in the array
int toFileFlag; //if we wnat to write to file
char *toFile; //name of file to write to
int fromFileFlag;//if we wnat to read from file
char *fromFile; //name of file to read to
}UserInput;
int runInBackground = 0; //is command running in background? if so, runInBackground=1;
//********************************************************************************************************************
//functions list:
UserInput* inputTokenization(char *a); //recieve string of the user input, and returns pointer to struct of UserInput.
void execCommand(UserInput *user_input); //exec the requestd command with the parameters given
void free_All_Allocations(UserInput *userinput);
//*******************************************************************************************************************
int main(int argc, char *argv[])
{
char userInputTxt[LINE_MAX]; //the line the user enters (hopefully command+parameters)
UserInput *u_i;
int i = 0;
while(1)
{
i = 0;
printf("\033[1;35m"); //print in with color (purple)
printf("### "); //### is the prompt I chose
fflush(stdout);
memset(userInputTxt, 0, LINE_MAX); //cleaning array from previous iteration
read(0, userInputTxt, LINE_MAX);
if(strcmp(userInputTxt, "exit\n") == 0) //exit the program if the user enters "exit"
exit(EXIT_SUCCESS);
u_i = inputTokenization(userInputTxt); //parsing the char array userInputTxt
execCommand(u_i);
free_All_Allocations(u_i);
}
}
UserInput* inputTokenization(char *a)
{
int i=0, size;
size = strlen(a);
UserInput *user_input = (UserInput*)malloc(sizeof(UserInput)*1);
if(user_input == NULL)
{
perror("failed to allocate memory");
exit(EXIT_FAILURE);
}
user_input->fromFileFlag = 0;
user_input->toFileFlag = 0;
user_input->size_of_array = 2;
//counting how many token we have
while(i<size)
{
if(a[i] == ' ')
(user_input->size_of_array)++;
if (a[i] != '<' || a[i] != '>' )
break;
i++;
}
printf("%d\n", user_input->size_of_array);
//we don't want to change original array(a), so we'll copy a to tmp and use tmp
char *tmp = (char*)malloc(size+1);
if(tmp == NULL)
{
perror("failed to allocate memory");
exit(EXIT_FAILURE);
}
strncpy(tmp, a, size-1);
//we'll allocate array of arrays. It's size: number of tokens in the original array, even though we might not use all of it-
//some tokens might be name of file to read or write to
user_input->parametersArray = (char**)malloc(user_input->size_of_array);
if(user_input->parametersArray == NULL)
{
perror("failed to allocate memory");
exit(EXIT_FAILURE);
}
i=0;
char* token = strtok(tmp, " ");
user_input->parametersArray[i] = (char*)malloc(strlen(token)+1);
if(user_input->parametersArray[i] == NULL)
{
perror("failed to allocate memory");
exit(EXIT_FAILURE);
}
strcpy(user_input->parametersArray[i], token);
i++;
while(token != NULL)
{
token = strtok(NULL, " ");
if(token !=NULL)
{
if(strcmp(token, "<") != 0 && strcmp(token, ">") !=0 && strcmp(token, "&") != 0)
{
user_input->parametersArray[i] = (char*)malloc(strlen(token)+1);
if(user_input->parametersArray[i] == NULL)
{
perror("failed to allocate memory");
exit(EXIT_FAILURE);
}
strcpy(user_input->parametersArray[i], token);
i++;
continue;
}
if(strcmp(token, "<") == 0)
{
user_input->fromFileFlag = 1;
token = strtok(NULL, " ");
if(token !=NULL)
{
user_input->fromFile = (char*)malloc(strlen(token)+1);
if(user_input->fromFile == NULL)
{
perror("failed to allocate memory");
exit(EXIT_FAILURE);
}
strcpy(user_input->fromFile, token);
}
}
if(strcmp(token, ">") == 0)
{
user_input->toFileFlag = 1;
token = strtok(NULL, " ");
if(token != NULL)
{
user_input->toFile = (char*)malloc(strlen(token)+1);
if(user_input->toFile == NULL)
{
perror("failed to allocate memory");
exit(EXIT_FAILURE);
}
strcpy(user_input->toFile, token);
}
}
if(strcmp(token, "&") == 0)
{
runInBackground = 1;
break;
}
}
}
user_input->parametersArray[i] = NULL;
free(tmp);
return user_input;
}
void execCommand(UserInput *user_input)
{
pid_t pid;
int status;
pid = fork();
if(pid == -1) //fork failed
{
perror("fork() failed");
exit(EXIT_FAILURE);
}
if(pid == 0) //child process
{
if(user_input->fromFileFlag == 1) //if we have file to read from
{
close(0);
if(open(user_input->fromFile, O_RDONLY) == -1)
{
perror("open file to read failed");
exit(EXIT_FAILURE);
}
}
if(user_input->toFileFlag == 1) //if we have file to write to
{
close(1);
if(open(user_input->toFile, O_WRONLY | O_CREAT, 0766) == -1)
{
perror("open file to write failed");
exit(EXIT_FAILURE);
}
}
if(execvp(user_input->parametersArray[0], user_input->parametersArray) == -1)
{
perror("execvp() failed");
exit(EXIT_FAILURE);
}
}
if(runInBackground == 0) //as long as this is the only command to execute,
waitpid(pid, &status, 0); //wait until chile process (execvp) finish. Otherwise, father process go again, and chile process run in background
}
void free_All_Allocations(UserInput *userinput)
{
int i=0;
while(userinput->parametersArray[i] != NULL)
{
free(userinput->parametersArray[i]);
i++;
}
free(userinput->parametersArray);
if(userinput->fromFileFlag == 1)
free(userinput->fromFile);
if(userinput->toFileFlag == 1)
free(userinput->toFile);
free(userinput);
}
最佳答案
我建议使用valgrind
。
使用标志 -ggdb3
编译您的代码,然后使用您的程序执行 valgrind
。它会显示程序执行期间所有无效的读取和写入。不仅如此,它还会准确地告诉您它们发生在哪一行以及相应的函数调用跟踪。
This question如果您是 valgrind
的初学者,这是一个很好的起点。
关于c - 用 c 编写我自己的 shell - free() 导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59382565/
这个问题在这里已经有了答案: How do free and malloc work in C? (8 个答案) 关闭 8 年前。 如果你使用malloc()为4个整数分配内存,它不应该返回第一个整
首先,介绍一下背景知识,这样您就不会认为我在尝试做一些疯狂的事情: 我正在尝试调试由其他人编写的 C 库中的崩溃。崩溃看起来像这样: TheProgram(44365,0x7fff75996310)
我正在 cstdlib malloc() 和 free() 函数之上创建自定义内存分配器(出于性能原因)。分配器位于一个简单的类中,该类存储一些内存缓冲区和其他参数。我想将释放内存的方法命名为 fre
我一直在解决这个练习,我不知道从哪里开始: 语言 B 是上下文无关的;语言 C 是 B 的子集:C 是否是上下文无关的?证明或反驳。 我试过使用闭包属性: C = B - ( (A* - C) ∩ B
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
如果我想获得在 C 中进行 malloc 的指针的所有权,则 docs for the Python cffi package和 this answer假设使用 ffi.gc 和 lib.free 作
#include #include struct node { int value; struct node* next; }; typedef struct node node_
众所周知,Oracle 在 Java 11 中更改了 Java 许可证,要求 JDK 的商业用途需要付费许可证。然而,使用 OpenJDK 仍然是免费的。 我的 PC 上有一个 JDK 11 文件夹,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我是 C 的新手,在 Linux 中使用带有开关 gcc -g -std=c89 -Wall ... 的 gcc4.4.6 进行编程,我在许多函数深处遇到了这个错误我的程序名为 compute: **
在多线程编程中,我们可以找到两个或多个线程/任务之间的数据传输同步的不同术语。 什么时候我们可以说某个算法是: 1)Lock-Free 2)Wait-Free 3)Wait-Freedom 我明白无锁
我正在尝试使用我通过 malloc() 手动分配的数组来运行程序。我在程序末尾释放()这个数组,但我不断收到错误消息 *** glibc detector *** ./test: double fre
我将 libxml2 与 libxslt 一起用于 C++ 程序的 XML 处理。为了使用 XSL 转换 XML 文档,我使用了以下函数(删除了错误处理): xmlDocPtr transformXm
new/delete 关键字使用免费商店 malloc/free 关键字是使用堆 我看到某处写着new 使用malloc。怎么会这样?它们不在内存段中使用? 其次,我看到某处写道我们不能在new 之后
我有这个简单的代码,我想在 tutorialspoint.com 上运行 #include using namespace std; class Vehicle { string vehic
我需要创建一个函数来删除 c 中链表的前 n 个节点,并返回删除的节点数。如果列表小于 n,它应该变为空。 另外,我不能使用递归。 使用现在的代码,它可以工作,但我没有释放“已删除”节点的内存。如果我
我需要调试这段代码的帮助。我知道问题出在 malloc 和 free 中,但找不到确切的位置、原因和解决方法。请不要回答:“使用 gdb”,仅此而已。我会使用 gdb 来调试它,但我仍然不太了解它并且
这个问题在这里已经有了答案: Unable to free const pointers in C (12 个答案) 关闭 8 年前。 将 C++11 代码连接到某些 C 回调,我必须传递 cons
这是出于好奇,我试图找到我对之前问题的疑问的答案,但他们似乎没有答案。所以在这里问,我只是写了一个代码,我试图将内存分配给一个 int 指针(以填充一个数组)并将 int 值扫描到它。完成数组后,我想
我有两个免费的单子(monad),用于不同上下文中的不同操作。但是,如果特定操作位于上下文中,则一个(主要)DSL 需要包含另一个(action)DSL: import Control.Monad.F
我是一名优秀的程序员,十分优秀!