Live data from Hacker News

Sizeof(char) is 1

drj11.wordpress.com

31–40 of 55 posts

Re: Sizeof(char) is 1

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

I think you miss the point.

A byte is always 8 bits, and therefore contains unsigned numbers 0 to 255 inclusive.

A char is always defined to be the base size of the machine.

By definition, therefore, sizeof(char) is always 1.

However, a char is not always 1 byte. On some machines a char can hold the values 0..511, and on others 0..65535. (for example)

Re: Sizeof(char) is 1

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

Actually, I have someone who did think that about parentheses - we'd have arguments about which approach was clearer. We never did agree.

Re: Sizeof(char) is 1

#33
Personally I don't agree with the post, starting from his analysis, of the first snippet of code:

  group->text = (char *) malloc(sizeof(char) * strlen(params->text));
  if (group->text == NULL) {
	res = MP_MEM;
  }
  strcpy(group->text, params->text);
I don't think that the sizeof(char) or the casting of the pointer are of any danger and increase readability (we are not coding to show how well we know the standard).

The poster is right about the missing space in the malloc call for the NULL char, but I see a bigger threat calling a malloc with the size coming from a strlen: it can be really dangerous if somehow you miss a NULL terminator.

And again, same complain about the usage of strcpy. The standard gives us strncpy that put a limiting size in the copy operation.

_Personally I think that strcpy has to be avoided as a plague being a source of buffer overruns error as no other call in the C library._

Re: Sizeof(char) is 1

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

Which is actually pretty useful and readable, imho (if formatted correctly):

  return a ? b :
         c ? d :
         e ? f :
         g;
Side note: this does not work in PHP, as the operator is left-associative instead of right-associative there. The above in PHP would be evaluated as:

  return ((a ? b : c) ? d : e) ? f : g;
Which is almost never what you want. I can't even format this properly to convey intent.

Re: Sizeof(char) is 1

#35
post #14
post #13

Earlier quoted context omitted.

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

But that has nothing to do with the sizeof(). It's a completely independent issue.

Re: Sizeof(char) is 1

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

I think you miss the point. A byte is always 8 bits, and therefore contains unsigned numbers 0 to 255 inclusive. A char is always defined to be the base size of the machine. By definition, therefore, sizeof(char) is always 1. However, a char is not always 1 byte. On some machines a char can hold the values 0..511, and on others 0..65535. (for example)

> A byte is always 8 bits, and therefore contains unsigned numbers 0 to 255 inclusive.

That definitely isn't true. Historically, bytes went from 5 to 16 bits, it was just the number of bits required to handle a character.

If you want to talk specifically about 8-bit bytes in an architecture-independent manner, use the word "octet" (which happens to be the word french people generally use).

`char` is a technically independent (though often related) datatype defined as being >= 8 bits by the ANSI standard and the definition of `sizeof(char) == 1` is an axiom of the ANSI standard, not the consequence of anything but itself, though the standard definitely seems to confound (confuse?) chars and bytes:

2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant. 3 When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. When applied to an operand that has array type, the result is the total number of bytes in the array.88) When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.

Re: Sizeof(char) is 1

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

OK, define char. If it is a type built-in C - then of course sizeof(char) = 1. And there is no reason to write such posts.

The problem is people are confusing the definition and the usage, because this particular type "char" was supposed to hold a numerical representation of a symbol, which in case of a multi-byte encoding is not 1 byte anymore.

So, sizeof(char) is always 1, and what they trying to say is sizeof(numeric-representation-of-a-symbol) which is not a concern of the C language.

Re: Sizeof(char) is 1

#38
post #34

Earlier quoted context omitted.

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.

Which is actually pretty useful and readable, imho (if formatted correctly): return a ? b : c ? d : e ? f : g; Side note: this does not work in PHP, as the operator is left-associative instead of right-associative there. The above in PHP would be evaluated as: return ((a ? b : c) ? d : e) ? f : g; Which is almost never what you want. I can't even format this properly to convey intent.

The only sane thing is to use if statements, imho. No ninja cowboy engineer nonsense.

Re: Sizeof(char) is 1

#39
post #19

Earlier quoted context omitted.

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 + Co…

Yep, I've seen similar monstrosities often.

There's actually two problems with such code: one is that it doesn't actually make the code easier to read, but harder.

The other is more subtle: using constants like this is working at the wrong level of abstraction. The correct way to write this kind of code is to use a wrapper function which knows how to put together URLs, since there are lots of special rules to how URLs are put together (encoding, etc.) But a lot of people don't bother, and instead simply concatenate strings with a SLASH between them, and think that because they define the slash in some constant that makes the code correct.

Re: Sizeof(char) is 1

#40
It should be noted that not all C compilers are fully standard compliant. Many embedded system vendor compilers for example use incorrect operator precedence. Available library can also do things in non-standard ways.

This does not look like advice about coding for the real world, it seems to be about how to invest your time to maximise feelings of smug superiority.

Post reply on HN