简体   繁体   English

将命令行字符串复制到数组中,valgrind错误

[英]Copying command line strings to an array, valgrind errors

I am expecting names from the command line and I need to hold them in an array. 我期望从命令行输入名称,并且需要将它们保存在数组中。 The multiple ways I've tried aren't working (I'm using strcpy) and I'm getting the following valgrind errors: 我尝试过的多种方法均无效(我正在使用strcpy),并且遇到以下valgrind错误:

Use of uninitialised value of size 8
==23027==    at 0x4C2E1E0: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23027==    by 0x400F90: main (pt-cruisers.c:52)
==23027==
==23027== Invalid write of size 1
==23027==    at 0x4C2E1E0: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23027==    by 0x400F90: main (pt-cruisers.c:52)
==23027==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

Here's the code snippet: 这是代码片段:

        char *racern[argc][20];
        Racer *racers[argc];
        pthread_t threads[argc];
        int rc;
        void *returnv;
        for(int i = 1; i < argc; i++){
                strcpy(racern[i-1], argv[i]); <-line 52
        }

I know that the length of the names will not exceed 20 characters. 我知道名称的长度不会超过20个字符。 I used argc to know how many names are going to be on the command line. 我使用argc知道命令行上将有多少个名称。 I'm assuming I'm not giving enough memory however even when I gave a large arbitrary amount and tested with 3 names I still got the same issues. 我假设我没有提供足够的内存,但是即使我随意分配大量内存并测试了3个名称,我仍然遇到相同的问题。 Any help is appreciated! 任何帮助表示赞赏!

I think you want to change: From: char *racern[argc][20]; 我认为您想更改:发件人: char *racern[argc][20]; To: char racern[argc][20]; 收件人: char racern[argc][20];

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

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