c - free causing different results from malloc -
below c program have written print different combination of characters in string.
this not efficient way algorithm creats lot of strings. question not how solve problem more efficiently.
the program works(inefficiently though) , prints different combination of characters of string(correctly). when try free
strings getting created run issue. free
causing issue @ end of thr recur_printc
function (it commented).
#include<stdio.h> #include<stdlib.h> #include<string.h> #define n 3 void recur_printc(char *, int, char *); int main() { char str[] = "abc"; char *print_arr = malloc(n * sizeof(char)); //call recur_print recur_printc(print_arr, 0, str); free(print_arr); return 0; } void recur_printc( char *print_arr, int index, char *remaining) { int i, j, rem_len, index_4_next; //base case, last cahracter remaining if(strlen(remaining) == 1) { print_arr[index] = remaining[0]; //print print_arr for(i=0; i<n; i++) { printf("%c",print_arr[i]); } printf("\n"); return; } //if more 1 character remaining else { rem_len = strlen(remaining); for(i=0; i<rem_len; i++) { //add 1 character print_arr print_arr[index] = remaining[i]; //now create string remaining characters char *remaining_for_next = malloc((rem_len-1) * sizeof(char)); index_4_next = 0; for(j=0; j<rem_len; j++) { if(j != i) { remaining_for_next[index_4_next] = remaining[j]; index_4_next++; } } //call recur_print recur_printc(print_arr, index+1, remaining_for_next); //free remainin_for_next /*------this causing issues----*/ //free(remaining_for_next); remaining_for_next = null; } } }
when ran program in gdb
noticed when i=1
first instance of recur_print
, strange thing happens malloc
.
when line executed :
char *remaining_for_next = malloc((rem_len-1) * sizeof(char));
although rem_len-1
equals 2
, malloc allocates 3
bytes, , whole algorithm fails beacuse somewhere in code strlen
of string used (which 3 instead of 2). not sure happening.(this not happen when comment out free()
line.)
below gdb output :
42 char *remaining_for_next = malloc((rem_len-1) * sizeof(char)); (gdb) print remaining_for_next $3 = 0x0 (gdb) n 43 index_4_next = 0; (gdb) print remaining_for_next $4 = 0x602030 "@ `" (gdb) print rem_len-1 $5 = 2 (gdb) q
sorry long post. once again, question not how print comibnation in different(and better) way. question why above code fails when try free remaining_for_next
string (maybe why malloc getting affected).
every time creating string, not appending null terminator, causes error.
so change this:
for(j=0; j<rem_len; j++) { if(j != i) { remaining_for_next[index_4_next] = remaining[j]; index_4_next++; } }
to this:
for(j=0; j<rem_len; j++) { if(j != i) { remaining_for_next[index_4_next] = remaining[j]; index_4_next++; } } remaining_for_next[index_4_next] = '\0';
output:
gsamaras@gsamaras:~/desktop/px$ gcc -wall main.c gsamaras@gsamaras:~/desktop/px$ ./a.out abc acb bac bca cab cba
tip: it's must to null terminate strings, not forget it!
important edit:
as alk noticed, need change this:
char *remaining_for_next = malloc((rem_len - 1) * sizeof(char));
to this:
char *remaining_for_next = malloc((rem_len) * sizeof(char));
in order make space null terminator.
nice question, +1.
Comments
Post a Comment