#What does sizeof() actually do?

39 messages · Page 1 of 1 (latest)

lilac frost
#

I'm reading Bjarne's C++ book, and he mentions that there exists embedded processors that are only word addressable where a char is 4 bytes. Then, he later mentions that sizeof() returns the number of multiples of chars the given type is, where sizeof(char) is defined to be 1. I've always thought that sizeof() returns the number of bytes for a given type (and a quick google seach seems to support this), so is this the case or is it the case that sizeof() returns the number of multiples of chars? As a more concrete example, lets take one of those embeddeed processors where a char is 4 bytes and an int is 4 bytes. Will sizeof(int) return 1 or 4?

I completely realize that this questions is probably completely useless, but I was just curious, so thanks for the help!

jolly lanternBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hollow valley
#

1

#

Because c++ defines a byte as being the smallest addressable size, and that is always a char

#

But it doesn't have to be 8 bits

lilac frost
#

ahh, thanks!

#

!solved

jolly lanternBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

hollow valley
#

Yw :3

rich hound
#

fwiw, char is not guaranteed to be 1 byte, just guaranteed to at least be 8 bits

#

though it's pretty standard for it to be 8 bits in width

hollow valley
#

It's guaranteed to be the smallest addresable width, which c++ defines as being 1 byte

#

Right?

tawny saddle
#

Then the question doesn't make sense as a char couldn't be 4 bytes

hollow valley
#

It can be 4 real bytes, but it'd be 1 c++ byte

rich hound
#

I'm trying to find rald's definition. The only thing I've found so far is at least 8 bits in width

hollow valley
#

It's in the fundamental type section of the standard

#

I don't remember the section number tho, sorry

rich hound
#

that's the section I'm reading rn 😛

hollow valley
#

I'm trying to find the section where it says char must be the smallest addressable unit

rich hound
#

It says that about a byte, but it doesn't define a type for it

warm nexus
#

the narrow character types are all 1 byte in size

hollow valley
#

sizeof(char) is guaranteed to be 1, right?
(Starting to doubt myself, and it's getting harder to type with cold fingers KEKW )

rich hound
#

It also says that the underlying bytes of an object can be copied into an array of char/uchar/std::byte

rich hound
warm nexus
#

It is fundamental to the correct usage of functions such as malloc and fread that
sizeof(char) be exactly one. In practice, this means that a byte in C terms is the smallest
unit of storage, even if this unit is 36 bits wide; and all objects are composed of an integer
number of these smallest units. Also applies if memory is bit addressable.
Section 6.5.3.4 "The sizeof operator" Paragraph 1 C99 rationale
here is the reasoning

rich hound
#

oh good call

#

yeah, it's defined in the sizeof operator of C++ to be 1

#

ty ty

hollow valley
#

Thanks!

rich hound
#

So I guess that makes it so C++ guarantees a byte is at least 8 bits?

#

because of the definition of the minimum width of narrow chars

warm nexus
#

that's stated in the definition of CHAR_BIT

#

and also the limits of the narrow character types as well

rich hound
warm nexus