Live data from Hacker News

Sizeof(char) is 1

drj11.wordpress.com

11–20 of 55 posts

Re: Sizeof(char) is 1

#11
post #8

Ridiculous! This is like saying that we shouldn't use parentheses because all C++ programmers should know order of operations! Not only that, but it's probably optimized out by the compiler (and if not, it is a quite insignificant speedup). Better to be safe using sizeof(x) than sorry ...

Absolutely optimized out. `sizeof()` is evaluated at compile time.

Re: Sizeof(char) is 1

#12

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 bears repeating:

"[P]rograms must be written for people to read, and only incidentally for machines to execute." -- Abelson & Sussman, Structure and Interpretation of Computer Programs [1], strangely misquoted by Paul Graham [2]

[1] http://mitpress.mit.edu/sicp/full-text/sicp/book/node3.html

[2] http://www.paulgraham.com/hp.html

Re: Sizeof(char) is 1

#13
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.

(1) sizeof is evaluated at compile time, so it would evaluate to (1 * strlen(params->text)), which the compiler would then optimize to just strlen(params->text). So, sizeof() is not exploitable.

(2) A terminating null character is placed at the end of strings, not at the end of allocated memory. malloc does not alter the memory before it returns a pointer to it; it is up to the programmer to ensure he is writing in the proper area.

Re: Sizeof(char) is 1

#14
post #13
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.

(1) sizeof is evaluated at compile time, so it would evaluate to (1 * strlen(params->text)), which the compiler would then optimize to just strlen(params->text). So, sizeof() is not exploitable. (2) A terminating null character is placed at the end of strings, not at the end of allocated memory. malloc does not alter the memory before it returns a pointer to it; it is up to the programmer to ensure he is writing in t…

The fact that it's strlen(params->text) is precisely why it's exploitable. If we intend to write strlen(params->text)+1 bytes and allocate less, we've set ourselves up for a buffer overrun.

Re: Sizeof(char) is 1

#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 demonstrate their assembly and C knowledge.

One thing I know -- that person is dangerous. How do I know? I was (perhaps still am) that person. It was a bad habit and I am still trying to get rid it. It is a veiled show of immaturity and arrogance.

Re: Sizeof(char) is 1

#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 file could have been a C file in an otherwise C++ project, but even in that case, it's at least understandable why the cast is there.

* If you have a macro that takes the type as a parameter:

    #define alloc(type)      ((type *)malloc(sizeof(type)))
then the casting is a good thing because it makes the compiler warn if you try to assign the result to the wrong pointer type.

* It is true that if you forget to include the stdlib.h header, the cast will silence a useful warning about converting int to pointer.

Re: Sizeof(char) is 1

#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.

Re: Sizeof(char) is 1

#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 it's not necessary any more. Still makes for clearer programs.

Re: Sizeof(char) is 1

#20
post #10

Many things equal one. I like to specify which one I'm talking when I'm writing code.

What a great sentence. I'm using this the next time I teach someone to program and have to explain the value of constants! :)
Post reply on HN