• Advertisement
Sign in to follow this  

checking for bytes

This topic is 2551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how would i do this?

sizeof(variable) == sizeof(char[5])

is there a replacement i could use for sizeof(char[5]) which would signify 5 bytes?
for example....a char is not always == to a byte...even tho on most compiler's it is....this can change in the future..my question is how can i check the a certain variable has 8 bits being used and is by definition a byte

Share this post


Link to post
Share on other sites
Advertisement
is there anyways c++ actually allows messing with actual BIT's to do comparison on sizes? i feel this would be a more secure/safe/portable way -thx

Share this post


Link to post
Share on other sites

is there anyways c++ actually allows messing with actual BIT's to do comparison on sizes? i feel this would be a more secure/safe/portable way -thx




Nope, don't think so. Bytes are the unit of memory. You can't get an address of a bit.

Share this post


Link to post
Share on other sites
From the wiki entry to stdint I gather that you can rely on the uintN_t types to be integers that hold exactly N bytes. Thus sizeof(5 * uint8_t) should do the job. However you need to verify if the platforms you require support at least this part of the C99 standard.

Share this post


Link to post
Share on other sites

how would i do this?

sizeof(variable) == sizeof(char[5])

is there a replacement i could use for sizeof(char[5]) which would signify 5 bytes?

Yes the literal 5 :)

for example....a char is not always == to a byte...even tho on most compiler's it is....this can change in the future..my question is how can i check the a certain variable has 8 bits being used and is by definition a byte

No a char is always a Language byte, how many bits this has is a different matter and one which CHAR_BIT will tell you.


Share this post


Link to post
Share on other sites

[quote name='nuclear123' timestamp='1296093135' post='4765388']
[size="1"]is there a replacement i could use for sizeof(char[5]) which would signify 5 bytes?
Yes the literal 5 :)[/quote]Yep, first thing wikipedia says is that sizeof is a number of bytes, so you want [color="#1C2837"]sizeof(variable) == 5
In the programming languages C and C++, the unary operator 'sizeof' is used to calculate the sizes of datatypes, in number of bytes
[/font]

Share this post


Link to post
Share on other sites
Why would one care anyway? Unless you write something very, very, very specific, you should not need to worry. First, it is not likely for you to ever encounter a non-8 bit system, and even so... if bytes are not 8 bits, they still are "the unit", a char is still a byte, and the size of any other POD or struct is still a multiple of char.

So... don't make your life complicated if you don't have to.

Share this post


Link to post
Share on other sites

how would i do this?

sizeof(variable) == sizeof(char[5])

is there a replacement i could use for sizeof(char[5]) which would signify 5 bytes?


Yes. It's called "5".

for example....a char is not always == to a byte...[/quote]

Yes, it is. However, a byte is not always the same thing as an octet.

If a char has 64 bits in your environment, then a byte has 64 bits in your environment. As far as the language of the C++ standard is concerned, there is nothing about a byte that involves having exactly 8 bits in it.

even tho on most compiler's it is....this can change in the future..[/quote]

It has to do with more than just your compiler.

my question is how can i check the a certain variable has 8 bits being used[/quote]

A char must have at least 8 bits. It is also not allowed to have a type smaller than char. Every variable's size is made up of char-sized units. (sizeof(char) == 1, by definition).

The real question is, why do you care about the exact number of bits in use by any of your data types? Even if you knew and had a good reason to care, there is very little you can do about it at this high of a level of programming. Yes, for the exceedingly few people who have a reason to care about this sort of thing, C++ (or even C) is probably much too high-level.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement