Привет всем. Есть проект, в котором потихоньку ковыряюсь. Сейчас запнулся на одном интересном моменте. Определенный кусок кода, в котором присутствует такие вот строки:
dictionary * dictionary_new(int size)
{
dictionary * d = NULL ;
/* If no size was specified, allocate space for DICTMINSZ */
if (size<DICTMINSZ) size=DICTMINSZ ;
if (!(d = (dictionary *)calloc(1, sizeof(dictionary)))) {
return NULL;
}
printf("d = %p\n", (void *)d);
d->size = size ;
d->val = (char **)calloc(size, sizeof(char*));
d->key = (char **)calloc(size, sizeof(char*));
d->hash = (unsigned int *)calloc(size, sizeof(unsigned));
return d ;
}
На строке
d->val = (char **)calloc(size, sizeof(char*));
Программа периодически вываливается с :
Program received signal SIGSEGV, Segmentation fault. malloc_consolidate (av=0xb7eaf440) at malloc.c:4246 4246 malloc.c: No such file or directory. (gdb) bt #0 malloc_consolidate (av=0xb7eaf440) at malloc.c:4246 #1 malloc_consolidate (av=0xb7eaf440) at malloc.c:4192 #2 0xb7d8318f in _int_malloc (av=0xb7eaf440, bytes=512) at malloc.c:3532 #3 0xb7d86d46 in __libc_calloc (n=128, elem_size=4) at malloc.c:3274 #4 0xb7f751c0 in dictionary_new (size=128) at src/dictionary.c:129 #5 0xb7f74889 in iniparser_load (ininame=0x804ae32 "../res/bds.ini") at src/iniparser.c:651 #6 0x0804a16b in get_key_number (ini_file=0x0, key
Не могу понять: в чем тут дело. С MALLOC_CHECK_=1 (2 или 3). Все работает. Но, как я понял — там тогда используются другие реализации malloc (более медленные). Сейчас в ступоре: не могу понять в чем именно дело. Или портится где-то сам кусок кучи, где malloc хранит указатели на свои «выделения». Запускал под valgrind, что странно — программа не вылетает. Запускал с
valgrind -v --tool=exp-sgcheck
==7061== ERROR SUMMARY: 49 errors from 3 contexts (suppressed: 9 from 7) ==7061== ==7061== 1 errors in context 1 of 3: ==7061== Invalid read of size 1 ==7061== at 0x419EB8E: vfprintf (vfprintf.c:1624) ==7061== by 0x42572D8: __vsprintf_chk (vsprintf_chk.c:86) ==7061== Address 0xbedb00d8 expected vs actual: ==7061== Expected: stack array «section» of size 1,025 in frame 3 back from here ==7061== Actual: stack array «key» of size 1,025 in frame 3 back from here ==7061== Actual: is 0 after Expected ==7061== ==7061== ==7061== 18 errors in context 2 of 3: ==7061== Invalid read of size 1 ==7061== at 0x419EB8E: vfprintf (vfprintf.c:1624) ==7061== by 0x42572D8: __vsprintf_chk (vsprintf_chk.c:86) ==7061== by 0x7320726E: ??? ==7061== Address 0xbedb00a8 expected vs actual: ==7061== Expected: stack array «section» of size 1,025 in frame 3 back from here ==7061== Actual: stack array «key» of size 1,025 in frame 3 back from here ==7061== Actual: is 0 after Expected ==7061== ==7061== ==7061== 30 errors in context 3 of 3: ==7061== Invalid read of size 1 ==7061== at 0x419EB8E: vfprintf (vfprintf.c:1624) ==7061== by 0x42572D8: __vsprintf_chk (vsprintf_chk.c:86) ==7061== by 0x62757472: ??? ==7061== Address 0xbedb00a8 expected vs actual: ==7061== Expected: stack array «section» of size 1,025 in frame 3 back from here ==7061== Actual: stack array «key» of size 1,025 in frame 3 back from here ==7061== Actual: is 0 after Expected ==7061== --7061-- --7061-- used_suppression: 9 ld-2.X possibly applying relocations ==7061== ==7061== ERROR SUMMARY: 49 errors from 3 contexts (suppressed: 9 from 7)Хотя, честно, не понял где тут «хорошо» и где «плохо».
Куда дальше копать?