473,671 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it UB once it's an L-value?


Anything wrong with the following code?:
#include <cstdlib>

int main()
{
for (unsigned i = 0; i != 1000; ++i)
{
int *p = reinterpret_cas t<int*>( std::rand() );
}
}
In the code, I'm creating an L-value pointer whose value is invalid.
However, the invalid memory address is never accessed.

Does the program exhibit Undefined Behaviour?

-Tomás
May 28 '06 #1
23 2181
On 2006-05-28 17:22, Tomás wrote:
Anything wrong with the following code?:
#include <cstdlib>

int main()
{
for (unsigned i = 0; i != 1000; ++i)
{
int *p = reinterpret_cas t<int*>( std::rand() );
}
}
In the code, I'm creating an L-value pointer whose value is invalid.
However, the invalid memory address is never accessed.

Does the program exhibit Undefined Behaviour?


Can't see why, if just having an invalid pointer was enough to cause UB
then the following code would be a problem:

int main()
{
int *p;
return 0;
}

Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
May 28 '06 #2
Tomás wrote:
int *p = reinterpret_cas t<int*>( std::rand() ); Does the program exhibit Undefined Behaviour?


Yes, and sketching the result in hardware will illustrate why.

I can conceive of a CPU that efficiently stores pointers in special
registers, and which faults if a bit pattern copies into such a register
that doesn't refer to valid memory. So a compiler must compile your p
optimistically, and use this register even though you never dereference p.

This is among the reasons why accessing a deleted pointer is undefined.

I think the Motorola 68000 faults if an An register gets an odd number...

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!

May 28 '06 #3
Erik Wikström wrote:
Can't see why, if just having an invalid pointer was enough to cause UB
then the following code would be a problem: int *p;


You copied no value in. So in my register example, the compiler reserved the
register and did not change its current value. So the surrounding opcodes
must keep that value healthy.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 28 '06 #4
Phlip wrote:
Tomás wrote:
int *p = reinterpret_cas t<int*>( std::rand() );

Does the program exhibit Undefined Behaviour?


Yes, and sketching the result in hardware will illustrate why.

[elaboration about a hypothetical implementation snipped]

No. The behavior is not undefined but implementation defined:

[5.2.10/5] (expr.reinterpr et.cast)

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.
Best

Kai-Uwe Bux
May 28 '06 #5
Phlip posted:

Yes, and sketching the result in hardware will illustrate why.

I can conceive of a CPU that efficiently stores pointers in special
registers, and which faults if a bit pattern copies into such a
register that doesn't refer to valid memory. So a compiler must
compile your p optimistically, and use this register even though you
never dereference p.

This is among the reasons why accessing a deleted pointer is
undefined.

I think the Motorola 68000 faults if an An register gets an odd
number...

But looking at it from an "official" viewpoint, why does it exhibit
undefined behaviour? Is it because I've turned an invalid pointer value
into an L-value?

Does the following code exhibit undefined behaviour:

#include <cstdlib>

int main()
{
reinterpret_cas t<int*>( std::rand() );
}
-Tomás
May 28 '06 #6
Kai-Uwe Bux posted:

[5.2.10/5] (expr.reinterpr et.cast)

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 query has nothing to do with integer-to-pointer conversion -- it's to
do with storing an invalid pointer value in a pointer variable.
-Tomás
May 28 '06 #7
Kai-Uwe Bux wrote:
No. The behavior is not undefined but implementation defined:

[5.2.10/5] (expr.reinterpr et.cast)

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.


He ain't converting from a pointer. rand() returns an int.

But while you have the Standard out, answer his next question; I don't know
the answer! ;-)

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 28 '06 #8
Tomás posted:

Anything wrong with the following code?:

For the purpose of this example, assume that the resultant pointer value
gotten from converting the int is an actual valid pointer value (i.e. you
won't get any trapping values or whatever).
-Tomás
May 28 '06 #9
Tomás posted:

Anything wrong with the following code?:

Actually scrub all that. I've thought of a much better example.

Is the following UB:

int main()
{
double array[4];

double *p = array + 7;
}
And what about the following:

int main()
{
double array[4];

array + 7;
}

-Tomás
May 28 '06 #10

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

Similar topics

0
2025
by: ss | last post by:
Dear All, I use ODBC to connect php (installed on linux) to a MS SQL Server 2000 database. I use iODBC and Freetds. Everything looks fine, until I try to open a multiple connection at once to the SQL server (I didn't close a connection before I opened a new one). I've got an error with strange characters : Warning: SQL error: ä , SQL state  l ŭ˙˙˙ in SQLExecDirect in .....
6
1707
by: Bob Bedford | last post by:
I've a query quite important to execute. I've to fill an array with all result, so the query is called once wich returns all ID's Then I call the same query a second time as I shown only 10 results in my page. How can I avoid to call the query twice ?
2
2866
by: Arpan De | last post by:
Suppose I have the following ASP code: <% Dim strName ..................... ..................... ..................... ..................... ..................... .....................
2
1557
by: zhebincong | last post by:
what is the difference between no-touch and click-once deployment?
27
8563
by: Jacob Jensen | last post by:
Hi I have a problem creating C++ code that is multiplatform compilable. My problem is using the "#pragma once" directive which can be used by microsoft Visual Studio pre-compiler but which gives warnings when used by gcc for example. I would like to use it when compiling with Visual C++ compiler, but how can I create a smart macro or something, so that it is truly ignored when compiler with gcc. I have tried the following which actually...
2
2651
by: shree | last post by:
Hi all, I'm trying to call javascript function via onLoad but running into a problem. I would appreciate any help and suggestion. My application is as follows. When a page loads, I want to check whether the vistor has the site's cookie present in their browser. If its not present, then to redirect user to a login page (script). If the cookie is present, then the rest of the html page should load. I'm able to get function to detect...
5
2376
by: ShaunO | last post by:
BACKGROUND I have the following classes as part of a program that opens 3 Asynchronous Sockets. Each socket is in a separate instance of a wrapping class. 1x User Interface class 1x Client Manager class 3x Client class Instances The Client class raises events when the client connects. The User interface has registered with that event.
0
1434
by: reifen-straub | last post by:
Hi, we have a C# click once application with a report. This report has a reference to an assembly to calculate some things. If we run this application locally with the 'click once' checkbox unchecked, everything works. But if we publish the application and for this automatically click once gets checked, we get the following error message within the report: Error during the local report processing. The definition of the report "main...
3
1224
by: DarkPilgrim | last post by:
I'm a bit new to PHP and there is something I'm not sure whether PHP can grant. Given my web application has some codes that only need to be executed once(eg.configuration used over the whole site), but every php script which needs that config seems to execute that once to get configuration when a request comes. Is there any mechanism that can make a script processed only once and all other pages can use its output? (Maybe this is...
0
1183
by: Mark | last post by:
Hi, We are a software company that wish to use Click-Once deployment to allow our customers to deliver smart clients to their worker's desktops. We have experimented successfully in-house with publishing our application from VS2005 to local web servers, and have tested the download, installation and upgrade proceedures.
0
8926
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...
0
8824
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7444
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5703
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
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...
1
2818
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
2
2060
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1815
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.