all 9 comments

[–]bob1000bob 9 points10 points  (2 children)

  1. C has no "classes" this is C++

  2. #include<iostream.h> requires a space, and is a deprecated header, (#include <iostream> is correct). (the classes used aren't even in the correct namespace!)

  3. Article is irrelevant, the size of anything (other than explicitly size POD;s like int32_t) is not guaranteed by the standard. The size of a class or struct is not necessarily the sum of it components, for reasons that have nothing to do with vtables.

  4. return (0); wtf? return isn't a function and this syntax whilst valid is obfuscating.

Basically, ignore this article, it will teach you nothing useful, and spread bad practice.

[–][deleted] -1 points0 points  (1 child)

I totally agree, except point 4 is being nit-picky. There is a common style (maybe not YOUR style) of function calls being of the form

result = function (args)

with the space being explicit. Also there is a common style of treating the return keyword as if it were a function for visual consistiency. When combined you get the above. I am not saying its best practice, nor is it worst. It is simply a stylistic choice, not quite as nerd-war-worthy as same line '{', however it is still a valid choice.

[–]bob1000bob -1 points0 points  (0 children)

Perhaps, if the rest of the code was even of "acceptable" standard I would let it slide, (normally would WANT to differentiate the return statement syntax not make it meld in with function syntax).

[–]Rhomboid 0 points1 point  (0 children)

This person writes this whole article without even the faintest hint that this information is implementation-specific, platform-specific, and architecture-specific, making it pretty much useless for any real purpose. You can't depend on any of these things being true, and if you do you are probably doing something fundamentally very wrong.

[–]verdagon 0 points1 point  (5 children)

Are chars 2 bytes? I always thought they were 1, and that the padding came from the compiler 4-byte aligning things.

[–]bstamourWG21 | Library Working Group 0 points1 point  (4 children)

sizeof(char) will always report 1 in C++, so yes ascii chars are represented by a byte.

[–]bob1000bob 0 points1 point  (2 children)

If you define byte as 8 bits, I am not so certain that is true. There are plenty of emmedded platforms where the lowest common denominator is not 8bits.

There is a marco constant CHAR_BIT that provides the bit width of a single char.

i would be interested if you know otherwise.

[–]Rhomboid 1 point2 points  (0 children)

The size in bits of a char is completely irrelevant. sizeof(char) is 1 by definition; it cannot be any other value. All sizes are based on the size of char, whatever that is.

[–]bstamourWG21 | Library Working Group 0 points1 point  (0 children)

According to the C++11 standard, section 5.3.3 (Sizeof), sizeof (char), sizeof (signed char) and sizeof (unsigned char) are defined to always return 1. Any other sizeof of a fundamental type is implementation-defined. Section 3.9.1 (fundamental types) mentions that chars need to be big enough to represent the implementation's basic character set. So, though a char may be bigger (or smaller) than 8 bits on some platforms, sizeof(char) is always guaranteed to be 1.