G
Gonçalo Rodrigues
Hi all, once again
Thanks to those who replied to my earlier post "2 questions on
primitive types". I have been reading the FAQ and the the dinkum C++
library, so let me make a few more questions so that I can settle
these things on my head.
1. 'int' is the natural signed integral type for a given platform (OS
+ compiler). The standard does not define exactly what natural is, so
not much can be read into it.
Is the above correct?
2. If T is type, then it is guaranteed that
sizeof(T*) == sizeof(char*)
What I mean by type here is, roughly, a first-class object that can be
stored in variables, passed into functions, etc. In particular, all
bets are off for functions, since they are not first-class (although
you can have pointers to them). The same for members.
Is the above correct?
3. Digging around in the dinkum C++ docs I see that there is in stddef
a size_t type that is supposed to hold "the result of the sizeof
operator."
So, if for the moment we view memory as a char array and we have a
char* ptr
pointing at the mythical 0 index of this array, then, minus OS
restrictions and what nots,
for (std::size_t i = 0; i < LIM; ++i) {
ptr;
}
would read off all memory. LIM here is the maximum number that
std::size_t can hold.
Correct?
4. LIM can be read off by
#include <limits>
std::size_t LIM = std::numeric_limits<std::size_t>.max()
Correct?
5. stddef also defines ptrdiff_t which is supposed to "store the
result of subtracting two pointers."
Can we guarantee that ptrdiff_t is the signed counterpart of size_t?
Can we guarantee that
sizeof(std::size_t) == sizeof(std:trdiff_t)
6. Is there a "big-enough" integral type capable of holding the adress
of every object (Type object as 'defined' above in 2.), e.g. calling
this integral type integer, via a cast as in
T obj;
integer address = (integer)&ptr;
When I made the other post, my impression was that int would do the
job. Then my guess was that std::size_t would suffice, but then I
realized that an address is a type in itself (type of
pointer-to-something) and does not need to come in sizeof(char) = 1
byte units. Hell, it does not even have to be unsigned or have any
relation to integral types whatsoever.
TIA, with my best regards,
G. Rodrigues
Thanks to those who replied to my earlier post "2 questions on
primitive types". I have been reading the FAQ and the the dinkum C++
library, so let me make a few more questions so that I can settle
these things on my head.
1. 'int' is the natural signed integral type for a given platform (OS
+ compiler). The standard does not define exactly what natural is, so
not much can be read into it.
Is the above correct?
2. If T is type, then it is guaranteed that
sizeof(T*) == sizeof(char*)
What I mean by type here is, roughly, a first-class object that can be
stored in variables, passed into functions, etc. In particular, all
bets are off for functions, since they are not first-class (although
you can have pointers to them). The same for members.
Is the above correct?
3. Digging around in the dinkum C++ docs I see that there is in stddef
a size_t type that is supposed to hold "the result of the sizeof
operator."
So, if for the moment we view memory as a char array and we have a
char* ptr
pointing at the mythical 0 index of this array, then, minus OS
restrictions and what nots,
for (std::size_t i = 0; i < LIM; ++i) {
ptr;
}
would read off all memory. LIM here is the maximum number that
std::size_t can hold.
Correct?
4. LIM can be read off by
#include <limits>
std::size_t LIM = std::numeric_limits<std::size_t>.max()
Correct?
5. stddef also defines ptrdiff_t which is supposed to "store the
result of subtracting two pointers."
Can we guarantee that ptrdiff_t is the signed counterpart of size_t?
Can we guarantee that
sizeof(std::size_t) == sizeof(std:trdiff_t)
6. Is there a "big-enough" integral type capable of holding the adress
of every object (Type object as 'defined' above in 2.), e.g. calling
this integral type integer, via a cast as in
T obj;
integer address = (integer)&ptr;
When I made the other post, my impression was that int would do the
job. Then my guess was that std::size_t would suffice, but then I
realized that an address is a type in itself (type of
pointer-to-something) and does not need to come in sizeof(char) = 1
byte units. Hell, it does not even have to be unsigned or have any
relation to integral types whatsoever.
TIA, with my best regards,
G. Rodrigues