I am very confused why this is happening though I've used such statements well before. I am copying one string into other simply and my original string is getting corrupted, even before strtok gets called. This is the relevant code snippet, after this I am using strtok. But before that I'm seeing reference got corrupted.
int j, sizeref;
char *str1, *token;
printf("%s :", reference);
sizeref = strlen(reference);
printf("%d\n", sizeref);
track = malloc(sizeref);
printf("%s :", reference);
strcpy(track, reference);
printf("%d\n", strlen(track));
This is the output below. The first line prints whole reference and correct size as 234. After that I see only 106 characters got copied and original string reference is also truncated to 106 characters. track is declared as char *
+918956549122,9527529747,09847778399,08596774833,9867859469,+919999866778,6985888696,5968939898,6959869856,9898495895,6986596865,09847765399,88596774833,9967859469,+917899866778,6985889696,9527567747,09848778399,08596756733,9867999469 :234
+918956549122,9527529747,09847778399,08596774833,9867859469,+919999866778,6985888696,5968939898,69598698 :106
MORE INFO: reference is getting build up incrementally in chunks of 50 bytes. Once completely built , then only being used as mentioned above. char * reference is global , and getting built up inside main. Above mentioned code is inside a separate function
realloc(reference,strlen(reference) + CHUNK);
track = malloc(sizeref + 1)... you don't allocate space for the NUL terminator. Apart from that, your program has another UB.strlen()returnssize_tand notint, so you must print that using%zuand not%d.