- ここに良い答えがあります.C ++プログラムのSegfault; 理解不能なvalgrind の出力ですが、コードで何が起こっているのか説明されていません
エラーの概要:
==6520== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
==6520==
==6520== 1 errors in context 1 of 1:
==6520== Invalid read of size 4
==6520== at 0x401882: test_START_EMPTY_TREE_TREEBASE_PRINT_FREETREE_TEST (check_test.c:173)
==6520== by 0x4046B2: srunner_run_all (in /home/travis/build/batousik/Practical-C2/bin/.libs/lt-check_test)
==6520== by 0x4019EF: main (check_test.c:341)
==6520== Address 0x0 is not stack'd, malloc'd or (recently) free'd
設定:
int_arr_ptr = malloc(arr_size * (sizeof(int)));
ptr_tree_base_int_1 = new_base(comp_ints, clean_ints, print_ints);
if(!ptr_tree_base_int_1 || !int_arr_ptr) {
printf("Error allocating memory in test Setup\n");
fflush(stdout);
assert(NULL);
}
for (int i = 0; i < arr_size; ++i) {
int r = rand() % 20000;
*(int_arr_ptr+i) = r;
printf("setup::%d\n",*(int_arr_ptr + i));
fflush(stdout);
}
機能部分:
int *ptr_helper;
for (int i = 0; i < arr_size; ++i) {
ptr_helper = malloc(sizeof(int));
if (!ptr_helper)
ck_abort_msg("FAILED memory allocation\n");
printf("%d\n",*(int_arr_ptr + i));
fflush(stdout);
//*ptr_helper =
isValid = insert(ptr_tree_base_int_1, ptr_helper);
ck_assert_int_eq(isValid, true);
}
取り壊す:
if (ptr_tree_base_int_1){
isValid = freeTree(ptr_tree_base_int_1);
ck_assert_int_eq(isValid, true);
free(ptr_tree_base_int_1);
ptr_tree_base_int_1 = NULL;
}
if(int_arr_ptr){
free(int_arr_ptr);
int_arr_ptr = NULL;
}
173行目は次のとおりです。
printf("%d\n",*(int_arr_ptr + i));