nik123
nik123

Reputation: 113

Getting a free(): invalid pointer error

I was trying to make a program that replaces characters e with * on a string that has the most e's,but I'm getting a random free(): invalid pointer error whenever I run this program:

#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;
}

And here's the gdb output:

*** 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

I'm not sure why that happens. Maybe I did something wrong, after all this is the first time I'm trying out the algorithm library.

Upvotes: 1

Views: 14041

Answers (2)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136208

In

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

You are using 1-based index. strings only has elements indexed from 0 to N-1, strings[N] is an invalid access.

C++ convention is 0-based indexes. Do

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

You also may like to initialize variables and check for errors on input.

Upvotes: 7

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385108

You used a variable to determine the length of an array. You cannot do this safely in C++.

Use std::vector<std::string> instead of std::string[N].

Furthermore, your loop goes 1 → N, instead of the correct 0 → N-1. That means the std::string you're trying to access on the final iteration doesn't exist, and the various allocations that the std::string performs internally are therefore broken.

Upvotes: 2

Related Questions