c - "cannot collect variable"?

标签 c debugging segmentation-fault gdb clion

“无法收集变量”是什么意思?我该如何解决它?它使调试器不会报告值。

enter image description here

我正在尝试调用我的函数

write_argument2(argc, * argv, * string[0]);

我将更改并重新排列 argv。我的变量 stringchar **string[100][100]; ,也许这并不理想。字符串变量将使用新参数更新 argv:

void write_argument2(int argc, char argv[], char *string[]) {
    int j = 0;
    for (j = 0; j <  argc; j++) {
        if (argv[j])
             string[j] = strdup(&argv[j]);
    }
}

但是我做错了什么,它崩溃了。该错误显示“无法收集变量”以及 strdup 的段错误。

我还尝试了以下编译但也成为段错误的方法:* string[j] = * strdup( & argv[j]);

Gdb 说:

GNU gdb (Ubuntu 7.11-0ubuntu1) 7.11
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./shell...done.
(gdb) run
Starting program: /home/dac/ClionProjects/shell2/openshell/shell 
'PATH' is set to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin.
$ ls

Program received signal SIGSEGV, Segmentation fault.
0x0000000000403b1a in write_argument2 (string=<optimized out>, argv=<optimized out>, 
    argc=<optimized out>) at main.c:147
147              string[j] = strdup( &argv[j]);
(gdb) 

我应该使用 strcpy 来代替还是更改一些声明?

最佳答案

argv 是一个字符串数组 (char *argv[]),它不是一个 char 数组。如果你的字符串定义为char string[100][100];,即它的存储定义为数组,你可以执行以下操作:

void write_argument2(int argc, char *argv[], char string[][100]) {
    int j = 0;
    for (j = 0; j <  argc; j++) {
        if (argv[j])
             strcpy(string[j], argv[j]);
    }
}

但是,如果 argv 的字符串数量多于 string 的数量(即 100)或 argv 的任何元素,则可能会崩溃> 大于 99。因此您也可以进行动态分配:

char **write_argument2(int argc, char *argv[]) {
    char **string = malloc(argc * sizeof(char*));
    int j = 0;
    for (j = 0; j <  argc; j++) {
        if (argv[j])
             string[j] = strdup(argv[j]);
    }
    return string;
}

编辑:添加一个测试程序来演示OP:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char **write_argument2(int argc, char *argv[]) {
    char **string = malloc(argc * sizeof(char*));
    int j = 0;
    for (j = 0; j <  argc; j++) {
        if (argv[j])
             string[j] = strdup(argv[j]);
    }
    return string;
}


int main(int argc, char *argv[])
{
    int i;
    char **string = write_argument2(argc, argv);
    for (i = 0; i < argc; i++) {
        printf("%d: %s\n", i, string[i]);
    }
    return 0;
}

调用这个问题:

$ ./a.out a bb ccc dddd
0: ./a.out
1: a
2: bb
3: ccc
4: dddd

关于c - "cannot collect variable"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37017487/

相关文章:

c - 为什么我可以在 C 语言的函数参数的数组声明中编写表达式?

c - ISO 8601 周数(C 语言)

android - "Waiting for debugger to attach"显示即使不在 Debug模式下运行

xcode - iOS 8 : How to properly setup extension to debug it in Xcode using simulator?

segmentation-fault - 如何在不链接libc.so的情况下访问段寄存器?

c++ - 故障排除段错误

c++ - 如何检查来自 recvfrom() 的消息大小?

c# - 隐藏消息框并给出错误

c++ - 同名结构,不同定义 : segmentation fault with -O2

并发环境中的 C++11 std::vector