获得free():无效的指针错误

时间:2016-04-08 08:52:11

标签: c++

我试图制作一个程序,用最多e的字符串替换字符e和*,但是每当我运行这个程序时,我都会得到一个随机的free():无效的指针错误:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    const char char1 = 'e', char2 = '*';
    int N, mostchars = 0, its;

    cout << "N = ";
    cin >> N;
    string strings[N];

    for(int i = 1; i <= N; i++)
    {
        cin >> strings[i];
        if(mostchars < count(strings[i].begin(), strings[i].end(), char1))
        {
            mostchars = count(strings[i].begin(), strings[i].end(), char1);
            its = i;
        }
    }
    replace(strings[its].begin(), strings[its].end(), char1, char2);
    cout << strings[its] << endl;
    return 0;
}

这是gdb输出:

*** Error in `/home/hiddendirishidden/STRING/bin/Debug/STRING': free(): invalid pointer: 0x00007fffffffe6a0 ***
======= Backtrace: =========
/usr/lib/libc.so.6(+0x6f364)[0x7ffff720a364]
/usr/lib/libc.so.6(+0x74d96)[0x7ffff720fd96]
/usr/lib/libc.so.6(+0x7557e)[0x7ffff721057e]
/usr/lib/libstdc++.so.6(_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_mutateEmmPKcm+0xfd)[0x7ffff7b765dd]
/usr/lib/libstdc++.so.6(_ZStrsIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EE+0x314)[0x7ffff7afd624]
/home/hiddendirishidden/STRING/bin/Debug/STRING[0x400e13]
/usr/lib/libc.so.6(__libc_start_main+0xf0)[0x7ffff71bb710]
/home/hiddendirishidden/STRING/bin/Debug/STRING[0x400c39]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:21 1452363                            /home/hiddendirishidden/STRING/bin/Debug/STRING
00601000-00602000 rw-p 00001000 08:21 1452363                            /home/hiddendirishidden/STRING/bin/Debug/STRING
00602000-00634000 rw-p 00000000 00:00 0                                  [heap]
7ffff0000000-7ffff0021000 rw-p 00000000 00:00 0 
7ffff0021000-7ffff4000000 ---p 00000000 00:00 0 
7ffff719b000-7ffff7333000 r-xp 00000000 08:21 396432                     /usr/lib/libc-2.23.so
7ffff7333000-7ffff7532000 ---p 00198000 08:21 396432                     /usr/lib/libc-2.23.so
7ffff7532000-7ffff7536000 r--p 00197000 08:21 396432                     /usr/lib/libc-2.23.so
7ffff7536000-7ffff7538000 rw-p 0019b000 08:21 396432                     /usr/lib/libc-2.23.so
7ffff7538000-7ffff753c000 rw-p 00000000 00:00 0 
7ffff753c000-7ffff7552000 r-xp 00000000 08:21 396763                     /usr/lib/libgcc_s.so.1
7ffff7552000-7ffff7751000 ---p 00016000 08:21 396763                     /usr/lib/libgcc_s.so.1
7ffff7751000-7ffff7752000 rw-p 00015000 08:21 396763                     /usr/lib/libgcc_s.so.1
7ffff7752000-7ffff7855000 r-xp 00000000 08:21 396490                     /usr/lib/libm-2.23.so
7ffff7855000-7ffff7a55000 ---p 00103000 08:21 396490                     /usr/lib/libm-2.23.so
7ffff7a55000-7ffff7a56000 r--p 00103000 08:21 396490                     /usr/lib/libm-2.23.so
7ffff7a56000-7ffff7a57000 rw-p 00104000 08:21 396490                     /usr/lib/libm-2.23.so
7ffff7a57000-7ffff7bc9000 r-xp 00000000 08:21 396798                     /usr/lib/libstdc++.so.6.0.21
7ffff7bc9000-7ffff7dc9000 ---p 00172000 08:21 396798                     /usr/lib/libstdc++.so.6.0.21
7ffff7dc9000-7ffff7dd3000 r--p 00172000 08:21 396798                     /usr/lib/libstdc++.so.6.0.21
7ffff7dd3000-7ffff7dd5000 rw-p 0017c000 08:21 396798                     /usr/lib/libstdc++.so.6.0.21
7ffff7dd5000-7ffff7dd9000 rw-p 00000000 00:00 0 
7ffff7dd9000-7ffff7dfc000 r-xp 00000000 08:21 396431                     /usr/lib/ld-2.23.so
7ffff7fc5000-7ffff7fcb000 rw-p 00000000 00:00 0 
7ffff7ff7000-7ffff7ff8000 rw-p 00000000 00:00 0 
7ffff7ff8000-7ffff7ffa000 r--p 00000000 00:00 0                          [vvar]
7ffff7ffa000-7ffff7ffc000 r-xp 00000000 00:00 0                          [vdso]
7ffff7ffc000-7ffff7ffd000 r--p 00023000 08:21 396431                     /usr/lib/ld-2.23.so
7ffff7ffd000-7ffff7ffe000 rw-p 00024000 08:21 396431                     /usr/lib/ld-2.23.so
7ffff7ffe000-7ffff7fff000 rw-p 00000000 00:00 0 
7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0                          [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

Program received signal SIGABRT, Aborted.
0x00007ffff71ce2a8 in raise () from /usr/lib/libc.so.6

我不确定为什么会这样。也许我做错了什么,毕竟这是我第一次尝试算法库。

2 个答案:

答案 0 :(得分:6)

for(int i = 1; i <= N; i++)

您使用的是基于1的索引。 strings只有从0N-1的索引元素,strings[N]是无效的访问权限。

C ++约定是基于0的索引。做

for(int i = 0; i < N; ++i)

您还可以初始化变量并检查输入错误。

答案 1 :(得分:1)

您使用变量来确定数组的长度。你不能在C ++中安全地做到这一点。

使用std::vector<std::string>代替std::string[N]

此外,您的循环变为 1→N ,而不是正确的 0→N-1 。这意味着您在最后一次迭代中尝试访问的std::string并不存在,因此std::string内部执行的各种分配都会被破坏。