Skip to content

Change all the `sizeof('\0')` I commited over the last year into `sizeof("")`.

Quinten Cabo requested to merge fix-sizeof into develop

This mr changes all the sizeof('\0') I commited over the last year into sizeof("").

Why?

I have started using sizeof('\0') instead of +1 when I needed an extra byte of space to terminate a string somewhere. I think writing + sizeof('\0') intead of +1 is nice because it more clearly shows the intent to the reader.

However, while working on my last mr I found out in a debugger that apparently in C sizeof('\0') or the size returned by any character literal is actually not 1, but 4! This is because apparently character constant have type int. Even though sizeof char is 1! Also, what is worse is that in C++ it will actually return 1 like I would have expected.

So what to use instead? I would like to still say something better than +1.

sizeof (char)'\0' actually works but that is terrible. I thought I would simply replace the sizeof '\0' with sizeof char which is definitely less nice than sizeof '\0' but it works. But then I also thought about sizeof("") which I think is actually quite nice, it communicates a similar idea, and it does actually evaluate to 1 like you would expect if you know about null terminated strings.

Merge request reports