Hello, i got problem with function which simply creates struct and using simple pointers copies for making a whole sturcture (Linked list). In first use of this function is nothing wrong, but in second use, values seems getting wrong. Better look at example and example of output.
Code:
row *create_row(int values[], char tab_name[]) {
printf("2.1\n");
if (find_tab(tab_name)->first_row != NULL) {
print_row(find_tab(tab_name),find_tab(tab_name)->first_row);
}
tab *tab_temp = find_tab(tab_name);
printf("2.2\n");
row *actual = malloc(sizeof(row));
item *first_item = calloc(tab_temp->columns_count,sizeof(item));
printf("2.3\n");
item *iter = malloc(sizeof(item));
int i;
if (tab_temp->first_row != NULL) print_row(tab_temp,tab_temp->first_row);
printf("2.4\n");
for (i = 0; i < tab_temp->columns_count; i++) {
iter = (first_item + i*sizeof(item));
if (i == ((tab_temp->columns_count) - 1)) {
iter->next = NULL;
}
else {
iter->next = first_item + ((i+1)*(sizeof(item)));
}
iter->value = values[i];
printf("Value %d\n",iter->value);
}
if (tab_temp->first_row != NULL) print_row(tab_temp,tab_temp->first_row);
printf("2.5\n");
actual->first_item = first_item;
actual->next = NULL;
printf("2.6\n");
if (tab_temp->first_row != NULL) print_row(tab_temp,tab_temp->first_row);
if (tab_temp->first_row == NULL) {
tab_temp->first_row = actual;
}
else {
actual->next = tab_temp->first_row;
tab_temp->first_row = actual;
}
if (tab_temp->first_row != NULL) print_row(tab_temp,tab_temp->first_row);
printf("2.7\n");
print_row(tab_temp,tab_temp->first_row);
printf("2.8\n");
return actual;
}
And Output:
Code:
2.1
2.2
2.3
2.4
Value 4
Value 5
Value 9
2.5
2.6
He Be Ce
4 5 9
2.7
He Be Ce
4 5 9
2.8
He Be Ce
4 5 9
2.1
He Be Ce
4 5 9
2.2
2.3
He Be Ce
4 0
2.4
Value 2
Value 7
Value 3
He Be Ce
4 2 7 3
2.5
2.6
He Be Ce
4 2 7 3
He Be Ce
2 7 3
2.7
He Be Ce
2 7 3
2.8
He Be Ce
2 7 3
3.
Look at this appearanc of magic number after 2.3 (at least for me magic). What comes wrong?
Code:
2.1
He Be Ce
4 5 9
2.2
2.3
He Be Ce
4 0
2.4
One more point, there appears warning when i debbuging:
warning: HEAP: Free Heap block 550fc0 modified at 551080 after it was freed
when allocing memory for row actual, but not only for this memory allocation
I feel that it is some basic fault but i cant find it.
Could you help me to find it? Or explain to me what i do wrong?
Thank you.