2

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);
3
  • 2
    track = malloc(sizeref + 1)... you don't allocate space for the NUL terminator. Apart from that, your program has another UB. strlen() returns size_t and not int, so you must print that using %zu and not %d. Commented Nov 1, 2013 at 6:47
  • 1
    @H2CO3 you save me so much damn typing sometimes. I need to repay that favor someday =P Commented Nov 1, 2013 at 6:48
  • @WhozCraig :D fair enough. Commented Nov 1, 2013 at 6:49

3 Answers 3

3

According to manual on strlen():

DESCRIPTION
       The  strlen()  function calculates the length of the string s,
       excluding the terminating null byte ('\0').

So, as mentioned above, you should allocate buffer of size strlen() + 1 for correct null termination.

Sign up to request clarification or add additional context in comments.

3 Comments

Yea that is true , but how will that solve this problem. I tried strlen()+1 , still same results. If I'm not using +1 then it should at least allocate 234 bytes , but it's stopping at 106 .. that is weird.
@DiwakarSharma I just checked your small part of the code from question with strlen(.)+1 and it works just fine. I got output like that: %all the input numbers% : 236\n%all the input numbers% : 235. And this is right (second strlen gives us 236-1). Guess, if you have some error than it in the other parts of the code. So, if you still working on this issue - post some test code (wrapped with standalone main() {}), which is as small as possible and which contains error. Then be sure, people will help you.
Well, I have changed my approach and logic to a better one I feel and have posted a different question for problem there. stackoverflow.com/questions/19761453/…
1

C strings are null terminated. Change the code to track=malloc(sizeref + 1);, otherwise strcpy will attempt to write the null termination outside valid memory.

Comments

0

You are probably trying to get the size of the array by using sizeof in a function where the array decays to a pointer and sizeof returns only the size of pointer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.