C ++中的动态数组-条件跳转或移动取决于未初始化的值

时间:2018-12-21 13:12:15

标签: c++ valgrind

我有此代码:

char *removechar(char *str, int ch)
{
    char *cpos = str;

    while((cpos = strchr(cpos, ch)))
    {
        strcpy(cpos, cpos + 1);
    }
    return str;
}

我不明白valgrind的这个错误:

#include <iostream>

using namespace std;

int main()
{
    int tmp = 5;
    int * arr = new int[tmp];
    for(int i = 0; i < 7; i++)
    {
        if (i == tmp) //if count of values is equal to max size of arr then create new arr with more space
        {
            int * s = new int[tmp]; // reserve memory
            for(int i = 0; i < (tmp); i++)
            {
                s[i] = arr[i]; //fill reserve memory with values from array arr
            }
            delete [] arr; // delete arr array
            tmp *= 2; //twice more space for array
            arr = new int[tmp]; //create new arr with twice more space
            for (int i = 0; i < (tmp / 2); i++)
            {
                arr[i] = s[i]; // add values from old short arr to new arr with more space
            }
            delete [] s; // delete reserve memory
        }
        arr[i] = 1; //add 1 to position i in array
    }
    //show array
    for (int j = 0; j < tmp; j++)
        cout << arr[j] << " ";
    cout << endl;
    delete [] arr; //delete arr
    return 0;
}

我发现如果我运行此循环

==2664== 
==2664== Conditional jump or move depends on uninitialised value(s)
==2664==    at 0x4EBFCDE: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x4EC02BC: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x4ECC06D: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x400AE1: main (in /home/vojta/Dokumenty/C++/vstup do pole/a.out)
==2664==  Uninitialised value was created by a heap allocation
==2664==    at 0x4C2B800: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2664==    by 0x400A2D: main (in /home/vojta/Dokumenty/C++/vstup do pole/a.out)
==2664== 
==2664== Use of uninitialised value of size 8
==2664==    at 0x4EBFBC3: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x4EBFD05: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x4EC02BC: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x4ECC06D: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x400AE1: main (in /home/vojta/Dokumenty/C++/vstup do pole/a.out)
==2664==  Uninitialised value was created by a heap allocation
==2664==    at 0x4C2B800: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2664==    by 0x400A2D: main (in /home/vojta/Dokumenty/C++/vstup do pole/a.out)
==2664== 
==2664== Conditional jump or move depends on uninitialised value(s)
==2664==    at 0x4EBFBCF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x4EBFD05: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x4EC02BC: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x4ECC06D: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x400AE1: main (in /home/vojta/Dokumenty/C++/vstup do pole/a.out)
==2664==  Uninitialised value was created by a heap allocation
==2664==    at 0x4C2B800: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2664==    by 0x400A2D: main (in /home/vojta/Dokumenty/C++/vstup do pole/a.out)
==2664== 
==2664== Conditional jump or move depends on uninitialised value(s)
==2664==    at 0x4EBFD33: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x4EC02BC: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x4ECC06D: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==2664==    by 0x400AE1: main (in /home/vojta/Dokumenty/C++/vstup do pole/a.out)
==2664==  Uninitialised value was created by a heap allocation
==2664==    at 0x4C2B800: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2664==    by 0x400A2D: main (in /home/vojta/Dokumenty/C++/vstup do pole/a.out)
==2664== 
1 1 1 1 1 1 1 0 0 0 
==2664== 
==2664== HEAP SUMMARY:
==2664==     in use at exit: 0 bytes in 0 blocks
==2664==   total heap usage: 3 allocs, 3 frees, 80 bytes allocated
==2664== 
==2664== All heap blocks were freed -- no leaks are possible
==2664== 
==2664== For counts of detected and suppressed errors, rerun with: -v
==2664== ERROR SUMMARY: 12 errors from 4 contexts (suppressed: 0 from 0)

到10不仅到7,valgrind不显示任何错误。 我想问你,我在数组中有一些空位的想法是否错误?我不知道我的代码是否正确,取决于您要添加多少值来创建数组。是否存在其他方法,如果需要,如何创建更大的数组?

我尝试使用--track-origins = yes运行valgrind,但找不到答案。

我是C ++的新手,所以我为每一个新的想法或提示感到高兴。 谢谢您的宝贵时间。

2 个答案:

答案 0 :(得分:4)

此数组增长到五个int之后,所示代码将其增长到十个元素,然后继续向该数组中添加两个额外的int#5和#6(我正在使用从0开始的索引,以引用数组中的各个int

最后的for循环将尝试打印数组的所有10个值。包括未初始化的值#7至#9。那就是valgrind告诉你的。

valgrind的诊断几乎无法理解,因为直到您深入I / O库的肠子后,才会真正发生未初始化的内存使用情况。该错误位于main()函数中,但是它所做的只是将对未初始化值的引用传递给I / O库。直到未初始化的值实际上被抓取到I / O库中进行格式化之前,这不会触发未定义的行为。

P.S .:首先通过将数组的内容复制到新的s数组来增长数组。原来的arr被删除,并由新的10个元素arr取代,然后从s复制回五个值,而s被删除。

这是很多不必要的复制。分配新的10个元素的数组,将现有内容复制到其中,然后删除原始数组,然后用新数组替换就足够了。

答案 1 :(得分:2)

您的问题已得到解答,但是下次,请知道,如果您使用-g3中的g++标志编译代码,valgrind还将向您显示发生问题的行号。例如,使用g++ teste.cpp -g3valgrind --track-origins=yes ./a.out进行编译将为您提供:

==20609== Memcheck, a memory error detector
==20609== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==20609== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==20609== Command: ./a.out
==20609== 
==20609== Conditional jump or move depends on uninitialised value(s)
==20609==    at 0x4F3CCAE: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x4F3CEDC: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x4F493F9: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x400AE0: main (teste.cpp:31)
==20609==  Uninitialised value was created by a heap allocation
==20609==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20609==    by 0x400A2B: main (teste.cpp:20)
==20609== 
==20609== Use of uninitialised value of size 8
==20609==    at 0x4F3BB13: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x4F3CCD9: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x4F3CEDC: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x4F493F9: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x400AE0: main (teste.cpp:31)
==20609==  Uninitialised value was created by a heap allocation
==20609==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20609==    by 0x400A2B: main (teste.cpp:20)
==20609== 
==20609== Conditional jump or move depends on uninitialised value(s)
==20609==    at 0x4F3BB1F: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x4F3CCD9: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x4F3CEDC: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x4F493F9: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x400AE0: main (teste.cpp:31)
==20609==  Uninitialised value was created by a heap allocation
==20609==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20609==    by 0x400A2B: main (teste.cpp:20)
==20609== 
==20609== Conditional jump or move depends on uninitialised value(s)
==20609==    at 0x4F3CD0C: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x4F3CEDC: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x4F493F9: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20609==    by 0x400AE0: main (teste.cpp:31)
==20609==  Uninitialised value was created by a heap allocation
==20609==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20609==    by 0x400A2B: main (teste.cpp:20)
==20609== 
1 1 1 1 1 1 1 0 0 0 
==20609== 
==20609== HEAP SUMMARY:
==20609==     in use at exit: 72,704 bytes in 1 blocks
==20609==   total heap usage: 5 allocs, 4 frees, 73,808 bytes allocated
==20609== 
==20609== LEAK SUMMARY:
==20609==    definitely lost: 0 bytes in 0 blocks
==20609==    indirectly lost: 0 bytes in 0 blocks
==20609==      possibly lost: 0 bytes in 0 blocks
==20609==    still reachable: 72,704 bytes in 1 blocks
==20609==         suppressed: 0 bytes in 0 blocks
==20609== Rerun with --leak-check=full to see details of leaked memory
==20609== 
==20609== For counts of detected and suppressed errors, rerun with: -v
==20609== ERROR SUMMARY: 12 errors from 4 contexts (suppressed: 0 from 0)

这告诉您未初始化的值在第31行中使用,它们是由第20行中的新值创建的。

这可能会很有帮助。