473,379 Members | 1,235 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Settling ideas

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[i];
}

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::ptrdiff_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
Aug 29 '05 #1
2 1170
* 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?
Yes.

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?
No, but you're guaranteed that a T* can be casted to void* and back
preserving the pointer value.

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[i];
}

would read off all memory. LIM here is the maximum number that
std::size_t can hold.

Correct?
No, there is a requirement that pointers point to actual objects or to an
imaginary element past the last element in an array, and p+i is only defined
when p and p+i point to the same object or within the same array (including
an imaginary element past the end of the array), and ditto for less-than and
greater-than comparisions.

4. LIM can be read off by

#include <limits>

std::size_t LIM = std::numeric_limits<std::size_t>.max()

Correct?
As the maximum value of std::size_t, yes.

As a maximum memory address, no.

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::ptrdiff_t)
No.

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;
That depends on the implementation, it is not guaranteed to exist.

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.
It does. In C and C++ everything is measured in bytes.

Hell, it does not even have to be unsigned
A pointer value isn't signed or unsigned, it's a pointer value.

or have any relation to integral types whatsoever.


reinterpret_cast establishes one such relation, but exactly what the
relation is depends on the implementation. Similarly, output via standard
streams establishes one such relation. And again it's unspecified by the
standard, and depends on the implementation.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 29 '05 #2
Gonçalo Rodrigues wrote:
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?
"Read into it"? What's that mean?

There are plenty of things the Standard doesn't define. Like what
a platform is.
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?
Well, no. Replace 'char' with 'void' and you get closer. However,
the Standard doesn't say what's involved in the conversion from a T*
to a void*. Especially nothing is said about their sizes being equal.
So, nothing is _guaranteed_. The point was that no existing system
is implemented so that it's not true.
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[i];
}

would read off all memory. LIM here is the maximum number that
std::size_t can hold.

Correct?
Since 'ptr[i]' may attempt to dereference memory that doesn't necessarily
exist, the code above has undefined behaviour, so the answer is "no".
4. LIM can be read off by

#include <limits>

std::size_t LIM = std::numeric_limits<std::size_t>.max()

Correct?
Yes.
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?
No, _we_ can't guarantee.
Can we guarantee that

sizeof(std::size_t) == sizeof(std::ptrdiff_t)
That would seem a common practice for integral types. An unsigned type
has the same size as its signed counterpart.
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;
I think you need to work on your typing skills. Did you mean

integer address = reinterpret_cast<integer>(&obj);

??? I assume that what you typed was a bunch of typos.
When I made the other post, my impression was that int would do the
job.
It would only "do the job" _iff_ such type exists.
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.
Yes, it does. A byte is defined as "the minimal individually addressable
portion of memory" (or some such). If the addresses are not expressed in
bytes, there is no way to distinguish between individual bytes, for, say
'read' or 'write' operation on POD objects.
Hell, it does not even have to be unsigned or have any
relation to integral types whatsoever.


Huh?

V
Aug 29 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: FLEB | last post by:
I like PHP for its excellent inline integration into standard HTML files, but I like Perl for its quick-moving syntax and simpler data-processing. To resolve this deep-seated inner turmoil (oh, the...
14
by: David MacQuigg | last post by:
I am starting a new thread so we can avoid some of the non-productive argument following my earlier post "What is good about Prothon". At Mr. Hahn's request, I will avoid using the name "Prothon"...
3
by: Slash | last post by:
Hello everyone I'm looking for seminar topics (and project ideas too) for my final year in Computer Engineering. I'm considering doing something on C++/Linux, considering that I love it so much...
3
by: Charulatha Kalluri | last post by:
Hello folks, I'm on the lookout for project ideas for a C++ class. The project should: -Be a "fairly complex" application
12
by: Anthony Greene | last post by:
Hello, I know this isn't really a python centric question, but I'm seeking help from my fellow python programmers. I've been learning python for the past year and a half, and I still haven't...
10
by: Wildemar Wildenburger | last post by:
Hi there :) I don't know how else to call what I'm currently implementing: An object that behaves like a list but doesn't store it's own items but rather pulls them from a larger list (if they...
0
by: bcannon | last post by:
At Guido's suggestion, a new mailing list has been created named Python-Ideas (http://mail.python.org/mailman/listinfo/python-ideas). This list is meant as a place for speculative, pie-in-the-sky...
224
by: Jon Slaughter | last post by:
Sorry for all the cross posting but I'm interesting in getting a serious discussion about how usenet has become lately. Many people are moving away from usenet because of all the spam and cooks...
0
by: mistersexy | last post by:
I have been programming in python for some time but I have become short of ideas. A software exhibition is coming up, and I plan to show python's worth 'cos it is quite underrated in this part of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.