Search code examples
c++memorycharvalgrindheap-memory

Why do i get mismatched error ? (C )


I have this code, but can't find the source of the error.I would like to store multiple names in the char array, and i have to use char type. I made this, but something is wrong with this.

#include <iostream>
#define MAX 2
using namespace std;

int main()
{

    char **names;
    names=new char* [MAX];
    names[0]=new char[3];

    for(int i=0;i<3;i++)
       names[0][i]='a';

    names[1]=new char[4];
    cout<<names[0]<<endl;
    for(int i=0;i<4;i++)
        names[1][i]='b';

    cout<<names[1]<<endl;
    ////////////////
    for(int i=0;i<MAX;i++)
        delete names[i];
    delete [] names;
    return 0;
}

Can somebody explain why, do i get errors in valgrind? There is no leak, but there is an error.

Invalid read of size 1
==4063==    at 0x4C2BFB4: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4063==    by 0x4EC62E0: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==4063==    by 0x400911: main (main.cpp:16)
==4063==  Address 0x5a03093 is 0 bytes after a block of size 3 alloc'd
==4063==    at 0x4C2AC27: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4063==    by 0x4008B4: main (main.cpp:10)
==4063== 
aaa
==4063== Invalid read of size 1
==4063==    at 0x4C2BFB4: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4063==    by 0x4EC62E0: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==4063==    by 0x400964: main (main.cpp:20)
==4063==  Address 0x5a030e4 is 0 bytes after a block of size 4 alloc'd
==4063==    at 0x4C2AC27: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4063==    by 0x4008FA: main (main.cpp:15)
==4063== 
bbbb
==4063== Mismatched free() / delete / delete []
==4063==    at 0x4C2A4BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4063==    by 0x400992: main (main.cpp:23)
==4063==  Address 0x5a03090 is 0 bytes inside a block of size 3 alloc'd
==4063==    at 0x4C2AC27: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4063==    by 0x4008B4: main (main.cpp:10)

Solution

  • You're forgetting the null terminator in your C-string

    names[0] = new char[4]; // 3 + 1 because of the null terminator
    
    for(int i = 0; i < 3; i++)
       names[0][i] = 'a';
    
    names[0][3] = '\0';
    

    So essentially, when you output the string using std::cout <<, valgrind is reporting an invalid memory access because the output routine doesn't understand where the C-string ends, and simply keeps outputting characters beyond the memory buffer allocated for the string.

    Also, I assume this is homework or something, otherwise you'd be insane not to use std::string.