简体   繁体   English

Shell C中的动态分配和分段故障

[英]Dynamic allocation and Segmentation fault in shell C

I want to dynamic allocate memory for line and argv and realloc if needed but i get errors when i do it like this: 我想为行和argv动态分配内存,并在需要时重新分配,但是当我这样做时出现错误:

char* argv = malloc(sizeof(char)*BUFFER); assignment makes integer from pointer without a cast in many lines like strcmp. 赋值使指针产生整数,而无需像strcmp这样的许多行进行强制转换。 Error log : https://i.imgur.com/qsYNinq.jpg 错误日志: https : //i.imgur.com/qsYNinq.jpg

Also i want it to print nothing than $(space) when someone hits ENTER but i get Segmentation fault(Core dumped) with the code or not scanf("%s",line); if(line == "\\n") printf("$ "); 另外,当有人按ENTER键时,我也希望它不打印$(space),但是我得到分段错误(转储了内核)或没有代码scanf("%s",line); if(line == "\\n") printf("$ "); scanf("%s",line); if(line == "\\n") printf("$ ");

Thanks in advance. 提前致谢。

If this 如果这

char* argv = malloc(sizeof(char)*BUFFER);

results in an assignment makes integer from pointer without a cast warning, the actual code you are compiling probably misses an 导致赋值使指针的整数变成整数而没有强制转换警告,则您正在编译的实际代码可能会丢失

#include <stdlib.h>

directive at the start of the file. 文件开头的指令。 GCC still implements implicit function definitions (long removed from the C standard), so it automatically supplies a declaration of malloc like this: GCC仍然实现隐式函数定义(已从C标准中删除了很长时间),因此它会自动提供malloc的声明,如下所示:

int malloc();

But this is definition is useless on 64-bit architectures because it clips to upper 32 bits of the pointer value (casting the return value back to char * will not restore them). 但这定义在64位体系结构上是没有用的,因为它会裁剪指针值的高32位(将返回值广播回char *不会恢复它们)。

Current versions of GCC will warn about the missing declaration of malloc . 当前版本的GCC会警告缺少malloc声明。 You really should compile with -Wall to get this warning (it was not enabled by default with older GCC versions). 您确实应该使用-Wall进行编译以获取此警告(较早的GCC版本默认未启用该警告)。

In the current code, argv is a array of pointers, but it the proposed code char* argv = malloc(sizeof(char)*BUFFER); 在当前代码中, argv是一个指针数组,但是它建议的代码是char* argv = malloc(sizeof(char)*BUFFER); it becomes an array of characters . 它变成一个字符数组。

When you later try to do argv[i]=token; 当您稍后尝试执行argv[i]=token; , you try to assign a pointer ( token ) to a mere character, which causes the warning assignment makes integer from pointer without a cast . ,您尝试将一个指针( token )分配给一个纯字符,这会导致警告分配使指针从指针变为整数而不进行强制转换

The correct way is to let argv still be a pointer to pointers: 正确的方法是让argv仍然是指向指针的指针:

int cmd_argc = 10;
char **argv = malloc(cmd_argc * sizeof(char *));

If you later need more parameters in argv array, you will be able to realloc: 如果以后需要在argv数组中使用更多参数,则可以重新分配:

cmd_argc *= 2;
if (NULL == realloc(argv, cmd_argc * sizeof(char *))) {
    // memory allocation error
    ...
}

But by convention , argc and argv are used for the parameters of the current program, so you should use another name like for example cmd_argv . 但是按照惯例argcargv用于当前程序的参数,因此您应该使用另一个名称,例如cmd_argv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM