0

今天晚上我一直在玩下面的代码几个小时,我只是在摸不着头脑。

使用函数从标准输入填充数组时,我不断收到“大小为 8 的无效写入”和“大小为 8 的无效读取”。

任何帮助将不胜感激......我知道 Stack Overflow 上有很多这样的错误,但其中大多数是这种情况所特有的。

void RawScore(unsigned int rawScoreCount, unsigned int numStudents, student studentInfo[],
              unsigned int projectCount, double rawScores[], double scores[], double weights[])
{
    int id;

    for (int i = 0; i < rawScoreCount; i++)
    {
        std::cin >> id;

        for (int j = 0; j < numStudents; j++)
        {
            if (id == studentInfo[j].id)
            {
                for (int k = 0; k < projectCount; k++)
                {
                    std::cin >> rawScores[k];
                    studentInfo[j].score += rawScores[k]/scores[k] * weights[k];
                }
            }
        }
            std::cin.ignore(10000, '\n');
    }
}

Memcheck 的错误如下:

==5793== Memcheck, a memory error detector
==5793== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==5793== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==5793== Command: a.out.app
==5793== 
==5793== Invalid write of size 8
==5793==    at 0x40E54DB: std::__1::istreambuf_iterator<char, std::__1::char_traits<char> > std::__1::num_get<char, std::__1::istreambuf_iterator<char, std::__1::char_traits<char> > >::__do_get_floating_point<double>(std::__1::istreambuf_iterator<char, std::__1::char_traits<char> >, std::__1::istreambuf_iterator<char, std::__1::char_traits<char> >, std::__1::ios_base&, unsigned int&, double&) const (in /usr/lib/i386-linux-gnu/libc++.so.1.0)
==5793==    by 0x40E517E: std::__1::num_get<char, std::__1::istreambuf_iterator<char, std::__1::char_traits<char> > >::do_get(std::__1::istreambuf_iterator<char, std::__1::char_traits<char> >, std::__1::istreambuf_iterator<char, std::__1::char_traits<char> >, std::__1::ios_base&, unsigned int&, double&) const (in /usr/lib/i386-linux-gnu/libc++.so.1.0)
==5793==    by 0x804D0FA: std::__1::basic_istream<char, std::__1::char_traits<char> >::operator>>(double&) (locale:771)
==5793==    by 0x804CECC: RawScore(unsigned int, unsigned int, student*, unsigned int, double*, double*, double*) (input.cpp:44)
==5793==    by 0x804EE6A: main (main.cpp:35)
==5793==  Address 0x445c388 is 0 bytes after a block of size 40 alloc'd
==5793==    at 0x402A17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==5793==    by 0x40BA709: operator new(unsigned int) (in /usr/lib/i386-linux-gnu/libc++.so.1.0)
==5793==    by 0x804EE26: main (main.cpp:32)
==5793== 
==5793== Invalid read of size 8
==5793==    at 0x804CED3: RawScore(unsigned int, unsigned int, student*, unsigned int, double*, double*, double*) (input.cpp:49)
==5793==    by 0x804EE6A: main (main.cpp:35)
==5793==  Address 0x445c388 is 0 bytes after a block of size 40 alloc'd
==5793==    at 0x402A17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==5793==    by 0x40BA709: operator new(unsigned int) (in /usr/lib/i386-linux-gnu/libc++.so.1.0)
==5793==    by 0x804EE26: main (main.cpp:32)
==5793== 
....... output of program here ......
==5793== 
==5793== HEAP SUMMARY:
==5793==     in use at exit: 0 bytes in 0 blocks
==5793==   total heap usage: 9 allocs, 9 frees, 476 bytes allocated
==5793== 
==5793== All heap blocks were freed -- no leaks are possible
==5793== 
==5793== For counts of detected and suppressed errors, rerun with: -v
==5793== ERROR SUMMARY: 20 errors from 2 contexts (suppressed: 0 from 0)

我已将问题缩小到以下两行,10 个写入错误和 10 个读取错误:

std::cin >> rawScores[k];
studentInfo[j].score += rawScores[k]/scores[k] * weights[k];

任何见解将不胜感激!

4

4 回答 4

1
std::cin >> rawScores[k];
studentInfo[j].score += rawScores[k]/scores[k] * weights[k];

从您的上述程序jk取决于用户输入,因此它们的值可以超出实际数组的 studentInfo rawScores索引。

您的程序应该具有逻辑,以便您的程序不会访问数组边界。

你可以监控你的程序

$ valgrind --tool=memcheck --db-attach=yes ./a.out

有关此概念以及如何使用它的详细信息,您可以参考以下帖子:

https://stackoverflow.com/a/22658693/2724703

于 2014-11-08T05:21:22.320 回答
1

您是否正确分配 rawScores?

您还需要确保 projectCount 小于 rawScores 大小

于 2014-11-08T05:25:12.677 回答
0
  1. 在传递给此函数之前,请检查您是否为数组 [rawScores, scores, weights, studentInfo] 分配了足够的内存。
  2. 检查用户输入,应该在数组范围内。

它会很好地解决你的问题

于 2014-11-08T05:33:56.087 回答
0

数组边界溢出是阴险的。在 C++ 中,如果你不损坏你关心的东西,你可能永远不会发现你超越了一些东西......除了你的一些值可能不太正确。(这是许多病毒攻击的核心“错误”——利用编写不佳的程序来假设缓冲区/数组大小。

假设您有类似的东西:

   char buffer[50];
   char author[] = "My Name";

   cout << author;
   cin >> buffer;

如果我键入一个 20 字符的输入字符串,没有任何伤害,没有犯规。

如果我输入一个 55 字符的输入字符串,“我的名字”将被部分覆盖,没有人会注意到,除非我尝试重新打印作者。这可能要等到许多(许多)声明之后才会发生。当你看到作者时,可能它看起来像“1234ame”,你会问“这是从哪里来的?

更糟糕的是,如果我输入一个 70 个字符的输入字符串,我会丢弃作者,以及它之后的任何内容,可能是内存管理控制块、i/o 缓冲区等,也许这将是“明显的”(或不明显) .

现在,我希望您明白为什么阵列管理错误可能在提交很久之后才会显示,如果有的话。“没有失败”可能并不意味着“正确”,因此您对“输出未显示任何超出范围的内容”的评论可能不像您希望的那样令人欣慰。

正如 Rupesh 之前所说 - 仔细注意您的数组,包括分配和填充。

如果这不是您需要的答案,那么您需要显示数组的定义以及它们是如何创建的。

于 2014-11-08T06:28:06.850 回答