'\o'

This is a discussion on '\o' within the C++ Programming forums, part of the General Programming Boards category; This is indeed a small question, which I for some unkown reason started to think about a few minuts ago. ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    160

    '\o'

    This is indeed a small question, which I for some unkown reason started to think about a few minuts ago.
    Why is it that the end of an array should be without anything at all in it? Is it so that when deleting the compiler knows were to delete to or what?
    Another thing is the '\0' in the end of an char array, though since it shouldn't be placed in the end, but in the second last, this should be like:
    char String[3];
    String[0] = 'H';
    String[1] = 'H';
    String[2] = 'i';
    String[3] = '\0';
    String[4] = NULL;

    I would really like to have this clearified. My gues is that the '\0' is there because those old functions like strcpy and strcmp use it.

    Last edited by Zahl; 10-14-2002 at 10:05 AM.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    NULL termination applies to strings (char arrays). It is so that functions, such as strcmp etc., can know where the string ends. This is because char* strings, do not otherwise, hold data on the length the string.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Additionally:

    NULL == '\0'

    Hence, your String[4] is redundant.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Its a good way to end a string...that's all.....otherwise you would always have to tell functions the size of the array.....

    In the old days, if you wanted to use a certain dos interupt to send a string to the screen, you would end the string with '$'....it did the same a '\0' but then you were stuck if you wanted to print a dollar sign

    It just makes things easier

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    But I couldn't delete a char array on the heap before i had both the second last char worth a '\0' and the last char of the array worth nothing at all.

    You know "delete [] Text;"
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    delete [] doesnt care about any '\0' chars...

    When you allocate with new, the compiler creates an area of memory and adds a tag just before that memory to say the size that was allocated....delete just uses that info to free the correct amount off the heap.......It doesnt care about the contents of the memory its learing (apart from calling destructors obviously...)

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    But still, why is it then that the very last part of an array should be empty.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Zahl
    But still, why is it then that the very last part of an array should be empty.
    It shouldn't (unless you count \0 as empty).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Zahl
    But still, why is it then that the very last part of an array should be empty.
    No reason why it should

    Code:
    int main(){
    	
    	
    	char str[6] = "Hello";//5 char + 1 '\0' = 6
    	
    	cout << str;
    
    
    }

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Here is the code I'm talking about (showed it in a new thread about two minuts ago... it's what I had trouple with you might gues )
    This code works, but if you'll read the coment you'll get my question.

    Code:
    AnsiString TFileHandle::Load() const
    {
      char *Text = new char[1];
      Text[0] = '\0';
      char *Temp;
      char Char;
    
      unsigned int Lenght;
    
      ifstream File(itsName.c_str());
    
      if (File.is_open())
      {
        while (File.get(Char))
        {
          Lenght = strlen(Text);
    
          Temp = new char[Lenght + 2];
          /* If I don't put the length of  this array to "+ 2" I can't
          delete it without an error on run-time. Sure it would work, but would allso have maid a memory leak. :(
    Thereby I've maid an array that will always have an end without
    anything in it.
    
          strcpy(Temp, Text);
    
          Temp[Lenght] = Char;
          Temp[Lenght + 1] = '\0';
    
          delete [] Text;
          Text = Temp;
        }
      }
    
      File.close();
    
      return Text;
    }
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Code:
    char *Text = new char[1];
    Text[0] = '\0';
    
    .......
    
    Lenght = strlen(Text);
    Without checking I would say that your problem is that strlen is returning 0.....I imagine thats why you have the error on delete....

    Better way - use a loop to count the amount of bytes...then create enough memory for the lot...then go and actually read the data..

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Sounds like a good idea what oyu recommend at the end, but other then that what your saying implies that you haven't read the code through (who can blame you ), because the reason it gives zero at first is because i want it to, so that I can construct my loop the way I have. If I didn't I would have to say strlen and allot of other things a totally different way. And when I finally says delete it aint giving me an error any more, and that doesn't exactly explain why it didn't give any error at all if I just maid the array one point longer.

    Another thing.... this mean that counting it all and make you own array would without doubt be much faster then using a string object since I know that thy use a method with allocating on the heap every time you add just one char or an array of them
    Last edited by Zahl; 10-14-2002 at 10:57 AM.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  13. #13
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789


    Ok..ok...I didnt read it properly.....you got me!!!!

  14. #14
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    YES! Succes... Hey... that didn't really explain anything... very clever huh
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  15. #15
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Looking at your code, the problem isn't that delete fails, but your indexing of the last element (where you set the \0 character).

    Length (or lenght as you type it, lol ) contains the length of the string excluding the NULL terminator, so a string of length n would require an array of length n+1.

    The indexes of the string ranges from 0 to Length-1, and the NULL terminator is at index Length. You try to set it at index Length+1, which is wrong, thus you need an extra byte in your array.

    Also, I don't think you need to set that NULL character manually, since it is automatically set using strcpy.

    Hope some of this helps.

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

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