Does the C++ standard define what should happen in case of NULL pointer
dereferencing. If not, does it say that it is illegal?
Where, if so, does it say it?
Jul 23 '05
51 3196
ajitho wrote: #include <stdio.h> [..] void main() { [.. example of undefined behaviour snipped ..]
Yes, we get it, you know how to reproduce undefined behaviour.
Give it a rest already.
ajitho wrote: ...
What are you trying to demonstrate by posting these already
beaten-to-death examples here? Both code samples you posted so far
produce undefined behavior. Are you trying to say that sometimes
undefined behavior might manifest itself in seemingly predictable
manner? But we all know that. And it is completely irrelevant and
doesn't make any difference.
--
Best regards,
Andrey Tarasevich
How about this:
Look at the definition of the "offsetof" macro in whatever C/C++
compiler you use.
It is obviously dereferencing a null pointer. It is not making use of
the <b>value</b> of the dereference. There is a suble difference.
ajitho wrote: How about this:
Look at the definition of the "offsetof" macro in whatever C/C++ compiler you use. It is obviously dereferencing a null pointer. It is not making use of the <b>value</b> of the dereference. There is a suble difference.
It is also part of the standard library, not user code. The writer of
the standard library can take advantage of knowledge of compiler details
that users cannot portably rely on.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Pete Becker wrote: ajitho wrote: How about this:
Look at the definition of the "offsetof" macro in whatever C/C++ compiler you use. It is obviously dereferencing a null pointer. It is not making use
of the <b>value</b> of the dereference. There is a suble difference.
It is also part of the standard library, not user code. The writer of
the standard library can take advantage of knowledge of compiler
details that users cannot portably rely on.
For example, the X window system defines XtOffset(type, member) in
several different ways. ONE of those ways is ((type*)NULL)->member.
However, it is not the only one, indicating that at least on some
systems that doesn't work as planned.
Of course, that is C code, but I don't think there is a difference in
this case.
I'm sure you know much more about the language than me. I thought the
"grey area" was legal .... (Falling back on the reasoning that the
dereferenced data is not actually accessed -- we're just playing around
with its address).
You're probably right ....
On Tue, 15 Feb 2005 12:07:59 -0800, Andrey Tarasevich
<an************ **@hotmail.com> wrote: ajitho wrote: http://www.comeaucomputing.com/iso/cwg_closed.html#315
And? Have you tried to follow the link and read the DR#315 yourself? Have you ever heard about the difference between static and no-static members of the class?
Also, try reading
http://www.comeaucomputing.com/iso/cwg_active.html#232 http://www.comeaucomputing.com/iso/cwg_active.html#453
linked from DR#315.
In your first example the function is not static. The behavior is undefined regardless of what the outcome of these still open DRs.
In your second example you are trying to create a null-reference. This is undefined behavior. And if you read the proposed resolutions in the above DRs, you'll see that there's no intent whatsoever to legalize null-references in C++. The behavior is undefined.
I think it's some weird kind of troll. I thought originally that it was
just confused (which is not an unusual state), but just posting examples
of undefined behaviour is weird...
Chris C This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jason Curl |
last post by:
I've been reading this newsgroup for some time and now I am thoroughly
confused over what NULL means.
I've read a NULL pointer is zero (or zero typecast as a void pointer),
others say it's compiler dependent (and that NULL might be anything, but
it is always NULL).
The source snippet is below. The question is:
- When I use calloc to allocate a block of memory, preinitialising it to
zero, is this equivalent (and portable C) to...
|
by: Mikhail Teterin |
last post by:
Hello!
Consider the following simple accessor function:
typedef struct {
int i;
char name;
} MY_TYPE;
const char *
|
by: Alexei A. Frounze |
last post by:
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2 = {0,1,2,4,9,16};
int aInt3 = {0,1,2,4,9};
|
by: David W |
last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like
this:
void f( int& p )
{
printf( "%d\n", p );
}
int main (int argc, char *argv)
{
|
by: Denis Petronenko |
last post by:
Hello,
in the following code i have segmentaion fault instead of exception.
Why? What i must to do to catch exceptions in such situation? Used
compiler: gcc version 3.3.6 (Debian 1:3.3.6-13)
int main()
{
try{
int* p = NULL;
*p = 4;
| |
by: valentin tihomirov |
last post by:
As explained in "Using pointers vs. references"
http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca ,
the pointers are allowed to be null, while references must refer an existing
variable of required type. The null is normally used for making optional
parameters. But there is no way to pass null reference in C#. Something is
missing.
|
by: Alan Woodland |
last post by:
Hi,
I'm fairly sure this is undefined behaviour, despite the fact that
it compiles and 'runs' (prints "this doesn't exist") on all my platforms:
#include <iostream>
class foo {
public:
void bar() {
|
by: prashant.khade1623 |
last post by:
I am not getting the exact idea.
Can you please explain me with an example.
Thanks
|
by: rahul |
last post by:
#include <stdio.h>
int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}
This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
|
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...
|
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...
| |
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...
|
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...
|
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();...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |