【问题标题】:"cannot collect variable"?“无法收集变量”?
【发布时间】:2016-08-29 06:53:12
【问题描述】:

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

我正在尝试调用我的函数

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( &amp; 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 还是更改一些声明?

【问题讨论】:

    标签: c debugging segmentation-fault gdb clion


    【解决方案1】:

    argv 是一个字符串数组 (char *argv[]),它不是一个字符数组。如果你的字符串定义为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
    

    【讨论】:

    • @Montao表示分配错误,strlen(strdup(argv[j]))是字符串argv[j]的长度,不是数组中字符串的个数。为什么不贴出调用函数的代码呢?
    • string= write_argument2(argc, argv); 是调用函数的代码。
    • 是的,它有效。但我还需要更新变量。我必须使用 realloc 吗?我可以就新功能提出一个新问题。这看起来很奇怪,但我想我可能需要做类似char *** write_command2(int w, int argc, const char *argv[], const char **string[]) 的操作来更新矩阵中的内容并将变量char ** string[] 用作可以增长的动态矩阵。
    • 我发布了另一个问题,如何使用 realloc 进行更新。 stackoverflow.com/questions/37020409/…
    • @Montao 你在做什么更新?喜欢附加另一个字符串?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多