473,803 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

contradiction in TC++PL?

Here's a code fragment from TC++PL Special Edition, p291:

void f1(T a)
{
T v[200];
T* p = &v[0];
p--; // please note (my comment)
*p = a; // oops: 'p' out of range, uncaught
++p;
*p = a; // ok
}

And here's an excerpt from p92:

"The result of taking the address of the element before the initial element
is undefined and should be avoided."

The author says, as I understand, it's ok with ++p after p--, which I think
contradict the statement in the excerpt. What do you think?

--
ES Kim
Jul 22 '05
42 2114
ES Kim posted:
"JKop" <NU**@NULL.NULL > wrote in message
news:1M******** ***********@new s.indigo.ie...

If you use reinterpret_cas t to convert a pointer value to an unsigned
integral value (assuming the integral type is large enough to hold the
value) and then reconvert it back, then you're left with a valid
pointer.

So while the following may be invalid:

int jack[6];

int* p = jack;

--p; //There may be overflow, for instance if p == 0


That is, if p is null pointer?
Surely you're joking, Mr. JKop. ;-)

The value for a null pointer is implementation defined.

Just because when you write:

int* k = 0;

it sets it to a null pointer, doesn't mean that the null pointer value is in
actual fact 0. Consider the following to be the definition of a pointer:

class *
{
public:
*& operator=(int value)
{
if ( !value )
{
//Set to null pointer value
}
else

...

}
};
-JKop
Jul 22 '05 #21
JKop wrote:
From the C90 draft that I have:

Wrong language dude.

Actually I had searched in C++98 unsuccessfully, that's why I fell back
to C90 (which is a subset of C++98 except of the cases where something
else is provided).
Anyway, regarding C++:
5.2.10

5 A value of integral type or enumeration type can be explicitly converted
to a pointer.64) A pointer converted
to an integer of sufficient size (if any such exists on the implementation)
and back to the same pointer type
will have its original value; mappings between pointers and integers are
otherwise implementation-defined.

The above means that you can't assign a pointer value to an unsigned,
increment the unsigned and reassign the result back to the pointer and
expect well-defined behaviour.
You can only assign a pointer to an integer than can store it, and
reassign that back.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #22
JKop wrote:
int jack[6];

int* p = jack;

--p; //There may be overflow, for instance if p == 0


That is, if p is null pointer?
Surely you're joking, Mr. JKop. ;-)


The value for a null pointer is implementation defined.

Just because when you write:

int* k = 0;

it sets it to a null pointer, doesn't mean that the null pointer value is in
actual fact 0. Consider the following to be the definition of a pointer:

class *
{
public:
*& operator=(int value)
{
if ( !value )
{
//Set to null pointer value
}
else

...

}
};


A null pointer never points to a valid object.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #23
On Tue, 19 Oct 2004 07:55:41 GMT, JKop <NU**@NULL.NULL > wrote:
ES Kim posted:
Here's a code fragment from TC++PL Special Edition, p291:

void f1(T a)
{
T v[200];
T* p = &v[0];
p--; // please note (my comment)
*p = a; // oops: 'p' out of range, uncaught
++p;
*p = a; // ok
}

And here's an excerpt from p92:

"The result of taking the address of the element before the initial
element is undefined and should be avoided."

The author says, as I understand, it's ok with ++p after p--, which I
think contradict the statement in the excerpt. What do you think?
Well consider this:

If you use reinterpret_cas t to convert a pointer value to an unsigned
integral value (assuming the integral type is large enough to hold the
value) and then reconvert it back, then you're left with a valid pointer.

So while the following may be invalid:

int jack[6];

int* p = jack;

--p; //There may be overflow, for instance if p == 0


How would p == 0? p can't be a null pointer since it points to jack
(!)

The reason that it is undefined is that it is illegal to form a
pointer that doesn't point within an object, or one past the end of an
object, or to "0". A debugging implementation might generate code to
enforce this.
I'd advocate that the following *is* legal:
int jack[6];

int* p = jack;

unsigned long address = reinterpret_cas t<unsigned long const>(p);

--address;
//We don't have to check for overflow because it's
//guaranteed not to occur with unsigned integrals.

p = reinterpret_cas t<int* const>(address) ;

//Now 'p' points to one before the start of the array.
But that makes p an invalid pointer value, so the above cast and
assignment have UB.
In short, I would say that it's safe to increment or decrement a pointer
however you see fit, but just watch out for overflow!
Overflow isn't really the issue, rather it is a more fundamental one
that pointer rvalues must point within an object (or one past the
end). This is partly to allow for an implementation that has special
pointer registers where pointer values are checked for validity
whenever they are loaded into the register.
To simply say "it's UB to do this" even though there's a perfect logic
behind it to illustrate what's going on, is simply being formal and
pedantic.


I don't think that talking about overflow sheds any light on the
topic; overflow is not necessarily the underlying problem.

Tom
Jul 22 '05 #24


So what do you think of the following, does it exhibit UB in your opinion?:

int main()
{
long* p_blah = reinterpret_cas t<long* const>(297239);
}
In my own opinion, it does *not* exhibit UB.
-JKop
Jul 22 '05 #25
On Tue, 19 Oct 2004 11:38:11 GMT, JKop <NU**@NULL.NULL > wrote:


So what do you think of the following, does it exhibit UB in your opinion?:

int main()
{
long* p_blah = reinterpret_cas t<long* const>(297239);
}
In my own opinion, it does *not* exhibit UB.


Actually, I think my statement of UB might have been a bit strong -
it's implementation defined behaviour (which may of course be defined
to do anything). For example, on DOS, such expressions can be used to
access video memory IIRC. On other platforms they might cause a crash:

"A value of integral type or enumeration type can be explicitly
converted to a pointer. A pointer converted
to an integer of sufficient size (if any such exists on the
implementation) and back to the same pointer type
will have its original value; ___mappings between pointers and
integers are otherwise implementation-defined___." (my emphasis)

So it's up to the implementation what it does when casting 297239 to a
pointer. Casting the --val back to a pointer (in the previous example)
is similarly implementation defined.

Tom
Jul 22 '05 #26
JKop wrote:
So what do you think of the following, does it exhibit UB in your opinion?:

int main()
{
long* p_blah = reinterpret_cas t<long* const>(297239);
}
In my own opinion, it does *not* exhibit UB.

It invokes implementation defined behaviour.

However
long x;

long *p=&x;

p--;

is undefined behaviour.

Some explanation on this: A pointer type is not required to be
implemented as an integer type.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #27
Ioannis Vranos posted:
JKop wrote:
So what do you think of the following, does it exhibit UB in your
opinion?:

int main()
{
long* p_blah = reinterpret_cas t<long* const>(297239); }
In my own opinion, it does *not* exhibit UB.

It invokes implementation defined behaviour.


For the love of Christ it does nothing!
-JKop
Jul 22 '05 #28
JKop wrote:
int main()
{
long* p_blah = reinterpret_cas t<long* const>(297239); }
In my own opinion, it does *not* exhibit UB.

It invokes implementation defined behaviour.



For the love of Christ it does nothing!


It invokes implementation defined behaviour. This style is used in many
systems to access specific portions of memory that provide specific
functionality.
The assignment is implementation defined, with no side effects. However
if the assignment is not valid, any attempt to dereference the pointer
invokes undefined behaviour.
This is all "legal" stuff. The standard tries to provide as much
guarantees as possible, portably.
So for example, this assignment being implementation defined guarantees
that your program will not crash or something by this assignment alone.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #29
In message <7O************ *******@news.in digo.ie>, JKop <NU**@NULL.NULL >
writes
Ioannis Vranos posted:
JKop wrote:
So what do you think of the following, does it exhibit UB in your
opinion?:

int main()
{
long* p_blah = reinterpret_cas t<long* const>(297239); }

In my own opinion, it does *not* exhibit UB.


It invokes implementation defined behaviour.


For the love of Christ it does nothing!

Hardly "nothing". It initialises a pointer with a bit pattern that may
not represent a valid hardware address. On some platforms which have
dedicated "address registers" and check their validity, that may be
sufficient to generate some kind of hardware exception.

--
Richard Herring
Jul 22 '05 #30

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

Similar topics

3
1745
by: Guofu Chen | last post by:
Hi, In TC++PL, Chapter 10, there is an exercise which ask us to modify the following program: #include <iostream> int main() { std::cout << "Hello, world!/n" ;
8
2978
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.