Live data from Hacker News

Sizeof(char) is 1

drj11.wordpress.com

1–10 of 55 posts

Re: Sizeof(char) is 1

#2
Rarely have so many bytes^H octets have been wasted in the course of indulgence in such a pointlessly misguided indignation. Really, of all the software engineering problems that industrial reality presents in front of us, this one deserves the least attention. I am only thankful this is not some celebrity news of which there has been too many here in recent weeks, but that is not a very high standard to measure against.

Re: Sizeof(char) is 1

#3
Is this really important? Sure, the source may be slightly polluted, but it's not going to slow down the application once it's compiled.

Really, these sort of things are probably good in code: they make it explicit that you are dealing with char's, and not some other datatype. If left out, it may look like a bug, causing debugging hassles.

Re: Sizeof(char) is 1

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

Re: Sizeof(char) is 1

#5
Maybe this is just the shitty programmer in me, but I'm going to go right ahead using sizeof(char) (I knew it's hardcoded at 1 before this, and I'll know after this), it appeals to the foolishly consistent programmer in me.

Re: Sizeof(char) is 1

#6
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 the code.

It's like when people define a 64MB buffer as something like:

#define BUFSIZE (64 * 1024 * 1024);

Even though you could've written 67108864, the former is far more comprehensible.

Re: Sizeof(char) is 1

#7
post #3

Is this really important? Sure, the source may be slightly polluted, but it's not going to slow down the application once it's compiled. Really, these sort of things are probably good in code: they make it explicit that you are dealing with char 's, and not some other datatype. If left out, it may look like a bug, causing debugging hassles.

sizeof(char) is a flag. Not bad in itself but often found inside bad code.

Re: Sizeof(char) is 1

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

Re: Sizeof(char) is 1

#9
post #3

Is this really important? Sure, the source may be slightly polluted, but it's not going to slow down the application once it's compiled. Really, these sort of things are probably good in code: they make it explicit that you are dealing with char 's, and not some other datatype. If left out, it may look like a bug, causing debugging hassles.

Agreed. I'm a strong proponent of lean codebases, but I still write `sizeof(char)`, because otherwise it's a dead ringer for a common mistake. Code should be concise only to the point that it doesn't become harder to read.
Post reply on HN