Live data from Hacker News

Sizeof(char) is 1

drj11.wordpress.com

21–30 of 55 posts

Re: Sizeof(char) is 1

#21
Really, a better coding style is to use STL strings, containers and algorithms and never get down to the point where you have to care.

Coding at the bare-metal level is a very slow, tedious, error-prone and silly way to code unless you're doing, or have some external constraint that forces you to (and, no, efficiency isn't usually a reason not to use C++ or the STL, it almost always compiles to the same as using strcpy's and malloc's.)

-- Ayjay on Fedang/coding

Re: Sizeof(char) is 1

#22
post #15

While the author is ridiculing those "silly" Mozilla devs I would like to ridicule the author of the blog. He seems like the person who would try to write "clever" code. Code that not only works correctly but teaches everyone _all_ the intricate operator precedence rules and all the possibly usages of bit operators, perhaps also code where they "cleverly" manipulate the call stack with asm inline instructions to demo…

Absolutely.

I once had to deal with code that did something like this:

  return a ? b : c ? d : e ? f : g;
I'm not kidding.

Re: Sizeof(char) is 1

#23

I actually disagree - while sizeof(char) might always be 1, code isn't just to communicate to a compiler, it's to communicate to another developer . When I see "sizeof(char) * strlen(foo)", I know that they want to allocate 'n' characters and that this is a string buffer: even though as a dev I certainly could've figured out, making code read like you think makes it far faster for others to interpret the intent of th…

The semicolon in your #define is evil and should be removed.

Re: Sizeof(char) is 1

#24
post #21

Really, a better coding style is to use STL strings, containers and algorithms and never get down to the point where you have to care. Coding at the bare-metal level is a very slow, tedious, error-prone and silly way to code unless you're doing, or have some external constraint that forces you to (and, no, efficiency isn't usually a reason not to use C++ or the STL, it almost always compiles to the same as using strc…

The man is writing about C, not C++. C remains important. I agree that C++ and the STL are good toolsI don't agree that STL style string handling code will compile down to the same thing as C style string handling code.

Re: Sizeof(char) is 1

#25

Everyone is commenting on his point about sizeof(char). Does anyone have any comments on his recommendation to use expressions with sizeof rather than types?

Personally, I like the idiom

  T* t = malloc( sizeof *t );
It's no big deal, but it's just one less thing to change if you need to change the type. (I once had to go through some code changing longs to ints to make it run on a 64-bit machine. Were the mallocs written this way it might have been a bit easier.)

And although I agree that his sizeof(char) rage is overblown, is the author's point about the zlib code not reasonable?

Re: Sizeof(char) is 1

#26
post #19

I actually disagree - while sizeof(char) might always be 1, code isn't just to communicate to a compiler, it's to communicate to another developer . When I see "sizeof(char) * strlen(foo)", I know that they want to allocate 'n' characters and that this is a string buffer: even though as a dev I certainly could've figured out, making code read like you think makes it far faster for others to interpret the intent of th…

It's also the main reason to use constants, even when magic numbers would do (e.g., use HOURS_IN_DAY instead of 24. It's not likely you'll ever have to change the number, but HOURS_IN_DAY is clearer for humans to parse). This also applies to the bit in the article about coercing return values, which I disagree with. Funnily enough, he mentions that K&R also recommend coercion, even though they have a small note that…

As with all rules like that they can be taken to DailyWTF levels of silliness.

I remember working on a Java codebase years ago where every string literal was defined in a Constants class - fair enough I guess. However, it had entries that looked like this:

   public static String HTTP = "http";

   public static String COLON = ":";

   public static String SLASH = "/";
leading to code that looked like:

   url = Constants.HTTP + Constants.COLON + Constants.SLASH + Constants.SLASH +

Re: Sizeof(char) is 1

#27
post #18

If a char is to be defined as number from 0 to 255 that and its storage space is exactly 1 byte - that is correct. But it is rather a legacy nowadays. No one should use it.

You missed the explanation: a char is always 1 byte, because in C the definition of byte is char. The wrong assumption is 1 byte == 8 bits.

Re: Sizeof(char) is 1

#28

Everyone is commenting on his point about sizeof(char). Does anyone have any comments on his recommendation to use expressions with sizeof rather than types?

I think it depends. In general, I prefer using expression, but there are cases where using sizeof is better (where better may be clearer, more maintainable, etc...).

I think casting malloc is useless in C (not only semantically, but from a readability POV), and is generally correlated with poor C programming. As other mentioned, mozilla is in C++, so that does not apply.

Re: Sizeof(char) is 1

#29
post #16
post #4

OK, I'm not exactly a professional C programmer, but among those who are: is the use of sizeof(char) here more important than the copy operation that is going to write the terminating null past the area actually malloc'ed? Because that sounds suspiciously like "potentially exploitable" to me.

No, you are right. The copying is a bad bug. Using sizeof(char) is mostly a coding style question. The casting of the malloc() return value is something reasonable people can disagree about (by which I mean nerds can endlessly flame each other about). But here are some points: * In C++, which Mozilla is written in, it is a compile time error to not cast, so most likely this blog post is just wrong. Conceivably, the f…

In the blog's comment, someone pointed out the same thing but the author then dismissed it as, "I’m a C programmer, not a C++ programmer, so I wouldn’t know anything about that." But considering Mozilla is a C++ project, it's unfair to critique it out of context.

Re: Sizeof(char) is 1

#30
post #29
post #16

Earlier quoted context omitted.

No, you are right. The copying is a bad bug. Using sizeof(char) is mostly a coding style question. The casting of the malloc() return value is something reasonable people can disagree about (by which I mean nerds can endlessly flame each other about). But here are some points: * In C++, which Mozilla is written in, it is a compile time error to not cast, so most likely this blog post is just wrong. Conceivably, the f…

In the blog's comment, someone pointed out the same thing but the author then dismissed it as, "I’m a C programmer, not a C++ programmer, so I wouldn’t know anything about that." But considering Mozilla is a C++ project, it's unfair to critique it out of context.

Well, the file he's quoting is C (note the lang:c filter). OTOH he's quoting Mozilla 1.7.7, which is positively ancient (Firefox 1.0.x I think).
Post reply on HN