gpt4 book ai didi

c++ - 大小 8 的无效读取,大小 8 的无效写入 (Valgrind)

转载 作者:行者123 更新时间:2023-11-28 02:38:14 28 4
gpt4 key购买 nike

今晚我已经研究了以下代码几个小时,但我只是摸不着头脑。

当使用函数从标准输入填充数组时,我不断收到“大小 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];

如有任何见解,我们将不胜感激!

最佳答案

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

关于c++ - 大小 8 的无效读取,大小 8 的无效写入 (Valgrind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26813988/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com