Like Tree7Likes

Assigning a null byte to a char *

This is a discussion on Assigning a null byte to a char * within the C Programming forums, part of the General Programming Boards category; Ok... Im about to go crazy. So i have a struct that has a char * within it.I malloc, say ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    11

    Assigning a null byte to a char *

    Ok... Im about to go crazy. So i have a struct that has a char * within it.I malloc, say 100*sizeof(char), to allow 100 chars in there. Now, the user wants to add a null byte to struct->string[50]. So I did it like this,

    Code:
    struct->string[50] = '\0'
    and it doesnt work...

    Because later on, I am calling a function that returns the struct->string as a string and (im assuming) the null byte hasnt been written because its reading right past the null byte at struct->string[50]... Is there something special I have to do?

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Show us your code, particularly the struct definition.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    Struct
    Code:
    //Taken down so I dont get in trouble :-/

    Code:
    //Yup...
    Last edited by Sonny Trujillo; 09-29-2011 at 12:30 AM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How does it "not work"? That doesn't mean anything to me. You shouldn't assign the return value of realloc to the same thing you are resizing, at least not immediately, in case it fails. If it fails, you will lose the data you have if you do it that way.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    It fails because when I do strlen(x->string) it returns 6 (if there I have added foo\0bar). When it should only return 3 because strlen should stop at the null character.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    and when I try to print x->string its printing everything, and just passing over the null byte, when it should stop at the null byte

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    The code that you show does not demonstrate the bug that you experienced.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    This is the only other spot where something with the null byte could be going wrong.
    Code:
    //boo
    Last edited by Sonny Trujillo; 09-29-2011 at 12:29 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    I suggest that you post the smallest and simplest compilable program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    1234
    Last edited by Sonny Trujillo; 09-29-2011 at 01:28 PM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    What is Blah? I am guessing that you missed out the typedef that made Blah an alias of pointer to xString.

    Anyway, this is wrong:
    Code:
    Blah x = (Blah *) malloc(initialCapacity * sizeof(Blah));
    It should be:
    Code:
    Blah x = malloc(initialCapacity * sizeof(*x));
    EDIT:
    In BlahAddChar, this line is unnecessary and incorrect:
    Code:
    char * charPt = x->string[x->size];
    In BlahAddZstring, this line makes an assumption that might not hold:
    Code:
    int futureSize = x->size + strlen(z);
    z may still be a null pointer.
    Last edited by laserlight; 09-28-2011 at 10:54 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    Yeah I have the type def in the header... But other than that I have been staring at this code and have NO IDEA why it would just pass over the null byte!?!? Im seriously gonna start crying lol

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Quote Originally Posted by Sonny Trujillo
    I have been staring at this code and have NO IDEA why it would just pass over the null byte!
    Refer to my edit.

    Actually, thinking a little more, I think the whole BlahAddZstring calculation in BlahAddZstring is wrong. You do not need to compute with strlen since the size already gives the size. At most you add 1 for the null character (and 1 more for the "future").
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    AHAH!!! Strcat was overwriting the null byte that I placed in! I can't believe I had overlooked that. But now that problem is, how can I append these without overwriting the null byte... Any help would be appreciated, and I think this problem is alot easier than the original one :-)

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I suppose the first question is... WHY?

    Once you stuff a null into the middle of a string, the back end of it is all but useless unless you set a pointer one past the null... giving you effectively have 2 strings... One that you can free() and the other that's going to get taken out with it... and that's some seriously broken code.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-08-2009, 01:47 AM
  2. Assigning a non null-terminated string
    By BattlePanic in forum C Programming
    Replies: 7
    Last Post: 05-04-2008, 10:02 PM
  3. Can't save file! [NULL Byte termination]
    By q6z4k in forum Networking/Device Communication
    Replies: 8
    Last Post: 07-13-2005, 04:45 PM
  4. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM
  5. bcc32 compiler warning when assigning null to pointer
    By finnepower in forum C++ Programming
    Replies: 4
    Last Post: 06-25-2002, 11:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21