#1
  1. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13

    malloc, string problems


    I'm having a little problem:

    Code:
    // * subject is acquired here some where  
    
    j=32;
    i= 48;
    
    char *str;
    str = (char*) malloc(sizeof(char)* j);
    
    strncpy(str,subject + (i), j);
    str[(strlen(str) - 1)] = '\0'; // strllen(str) = 35 ???
    I'm telling C to only copy j(32) chars into str ,yet, strlen(str) is giving me 35. Also, when I print it it's showing those funny looking characters passed the first 32 chars. I know that the funny looking characters are shown because printf is trying to access memory that has garbage in it but why is the string 35 chars long when I only malloced 32 chars and only copied 32 chars to it?
    Some day I'll create a smart quote to put here.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    10
    Rep Power
    0
    Maybe you forgot to copy the null terminator.
  4. #3
  5. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    it shouldn't matter, cause I only told it to copy 30 chars. I added the null terminator myself:
    str[(strlen(str) - 1)] = '\0';
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    10
    Rep Power
    0
    strlen is dependant on the null terminator in the first place.
  8. #5
  9. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    Oh, so as I do:
    strncpy(str,subject + (i), j);

    it is in fact only coping 32 bytes.
    all I have to do myself is put the null terminator at the end.
    str[(j-1)] = '\0';
    At this pointer, I have putted the null terminator at the end of the malloced memory.

    thanks person
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    India
    Posts
    5
    Rep Power
    0

    Lightbulb


    Hi,
    I have seen the complete discussion. Let me add some points.

    Assume that we have 50 length string and We wanted to copy just 32 characters, then following code will work correctly.

    char mystr[50];
    char *str;
    ...
    ... /* Assume mystr is having string of length 50 */
    ...
    j=32
    str = (char*) malloc(sizeof(char)* (j + 1)); /* To hold '\0' + 32 characters */

    strncpy(str,mystr, j); /* Copy first 32 characters from 'mystr' */
    str[j] = '\0'; /* Append '\0' at the end */

    Does this give you more clarification ?

    Regards
    Karthik
  12. #7
  13. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Originally posted by kubicon
    it shouldn't matter, cause I only told it to copy 30 chars. I added the null terminator myself:
    str[(strlen(str) - 1)] = '\0';
    You use strlen before you've added the terminator.
  14. #8
  15. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    Thanks for the clarification guys.

IMN logo majestic logo threadwatch logo seochat tools logo