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

Home Posts Topics Members FAQ

Will this compile?

#define NULL 0

int main()
{

unsigned int p = NULL;

/* Empty your mind */
if (p!=NULL) delete &p;

p = NULL;

/* if a pointer is cast into an int, it becomes the int */
int* r = (int*)p;

struct my_struct
{
int b;
};

/* if a pointer is cast into a struct, it becomes the struct */
my_struct* q = (my_struct*)p;

/* be a pointer my friend */
return 0;
}

Nov 28 '06 #1
8 1427

le****@gmail.co m napsal:
#define NULL 0

int main()
{

unsigned int p = NULL;

/* Empty your mind */
if (p!=NULL) delete &p;
Here you are attempting to delete variablke on stack. It is nonsense
and it shouldn't work.
p = NULL;

/* if a pointer is cast into an int, it becomes the int */
int* r = (int*)p;
You are assuming, that pointer has the same size as int. But you are
wrong. Did you try 64-bit Linux? There is 64-bit pointer and 32-bit int
in gcc.
struct my_struct
{
int b;
};

/* if a pointer is cast into a struct, it becomes the struct */
my_struct* q = (my_struct*)p;

/* be a pointer my friend */
return 0;
}
Nov 28 '06 #2
On 2006-11-28 21:17, le****@gmail.co m wrote:
>
Will this compile?
Yes, it will probably compile on most machines. Is it in any way
meaningful? No!
#define NULL 0

int main()
{

unsigned int p = NULL;

/* Empty your mind */
if (p!=NULL) delete &p;
If p != 0 delete &p? You don't even do this with pointers, so what is it
you are trying to do?
>
p = NULL;

/* if a pointer is cast into an int, it becomes the int */
int* r = (int*)p;
But you are casting an int to a pointer.
>
struct my_struct
{
int b;
};

/* if a pointer is cast into a struct, it becomes the struct */
my_struct* q = (my_struct*)p;
Again, you are casting an int to a pointer to a struct, not the other
way around.
>
/* be a pointer my friend */
return 0;
}
return NULL; ??

As a rule, don't cast pointers to non-pointers and never ever cast
non-pointers to pointers, in fact, try to keep the casting to a minimum
is it can be helped.

--
Erik Wikström
Nov 28 '06 #3
le****@gmail.co m wrote:
#define NULL 0
Lose that macro. But at least don't use it for integers.
int main()
{

unsigned int p = NULL;

/* Empty your mind */
if (p!=NULL) delete &p;
Only give pointers to objects to delete that you got from new. p is a local
object and is automatically destroyed when it goes out of scope. Giving its
address to delete invokes undefined behavior.
p = NULL;

/* if a pointer is cast into an int, it becomes the int */
int* r = (int*)p;
You are casting an int into a pointer, not the other way round. Usually, you
only need that if you have to manually deal with absolute addresses.
Happens when you do low-level programming like operating system kernels.
struct my_struct
{
int b;
};

/* if a pointer is cast into a struct, it becomes the struct */
my_struct* q = (my_struct*)p;
You aren't casting a pointer into a struct. You are casting an int into a
pointer. Also, I have no idea what you mean by "it becomes the struct".
What you have here is a pointer that contains a bogus value.
/* be a pointer my friend */
return 0;
}
Nov 28 '06 #4

Rolf Magnus wrote in message ...
le****@gmail.co m wrote:

The OP seems to be confused about pointers. I suggest:

Alf P. Steinbach's "Pointers" document:
http://home.no.net/dubjai/win32cpptu...ters/ch_01.pdf

.....and probably does not have a book:

Get "Thinking in C++", 2nd ed. Volume 1&2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

--
Bob R
POVrookie
Nov 29 '06 #5
Rolf Magnus wrote:
le****@gmail.co m wrote:
#define NULL 0

Lose that macro. But at least don't use it for integers.
int main()
{

unsigned int p = NULL;

/* Empty your mind */
if (p!=NULL) delete &p;

Only give pointers to objects to delete that you got from new. p is a local
object and is automatically destroyed when it goes out of scope. Giving its
address to delete invokes undefined behavior.
The posted code doesn't give anything to "delete" (the test
p!=NULL always fails)
/* if a pointer is cast into a struct, it becomes the struct */
my_struct* q = (my_struct*)p;

You aren't casting a pointer into a struct. You are casting an int into a
pointer. Also, I have no idea what you mean by "it becomes the struct".
What you have here is a pointer that contains a bogus value.
Also it may cause undefined behaviour due to alignment issues.

Nov 29 '06 #6

le****@gmail.co m wrote:
#define NULL 0

int main()
{

unsigned int p = NULL;
p here is a variable, not a pointer. its set to 0.
>
/* Empty your mind */
if (p!=NULL) delete &p;
p is zero so the above is a nop. Which is a good thing since delete
without a new is a disaster.
>
p = NULL;

/* if a pointer is cast into an int, it becomes the int */
int* r = (int*)p;
No it doesn't. a pointer is a pointer, it can never be an int. How
dangerous are pointers?
Dangerous enough when you ignore that an integer might not occupy the
same real estate as a pointer. As is the case on this platform.
By definition, as well, a pointer points to nothing until initialized.
Pointers are dumb. They only know their type and they have no way of
knowing when they are actually pointing to anything.
>
struct my_struct
{
int b;
};

/* if a pointer is cast into a struct, it becomes the struct */
my_struct* q = (my_struct*)p;
No, a pointer can never be a struct and much less an instance of a
struct.
>
/* be a pointer my friend */
return 0;
}
Pointers are the black holes of the programming world.

Nov 29 '06 #7
Old Wolf wrote:
Rolf Magnus wrote:
>le****@gmail.co m wrote:
#define NULL 0

Lose that macro. But at least don't use it for integers.
int main()
{

unsigned int p = NULL;

/* Empty your mind */
if (p!=NULL) delete &p;

Only give pointers to objects to delete that you got from new. p is a
local object and is automatically destroyed when it goes out of scope.
Giving its address to delete invokes undefined behavior.

The posted code doesn't give anything to "delete" (the test
p!=NULL always fails)
That's right, but the intention was probably to give it to delete,
otherwise, the OP would have left that line out completely.

Nov 29 '06 #8
Rolf Magnus wrote:
Old Wolf wrote:
Rolf Magnus wrote:
le****@gmail.co m wrote:
unsigned int p = NULL;
if (p!=NULL) delete &p;

Only give pointers to objects to delete that you got from new. p is a
local object and is automatically destroyed when it goes out of scope.
Giving its address to delete invokes undefined behavior.
The posted code doesn't give anything to "delete" (the test
p!=NULL always fails)

That's right, but the intention was probably to give it to delete,
otherwise, the OP would have left that line out completely.
I'm having a hard time figuring out the OP's intentions, FWIW :)

Nov 29 '06 #9

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

Similar topics

7
1522
by: BigMan | last post by:
Should the following piece of code compile or not according the C++ standard? Why? class t { public: t( ) { } t( t& a ) { } template< typename ttt >
10
1266
by: A Moore | last post by:
/// /// Both g++ 3.4.1 and comeau 4.3.3 report "i undefined" /// #include <iostream> template<typename T> class Base { protected: int i;
2
2538
by: Kathy Houtami | last post by:
Hi there I've been encountered with weird compile error using Access 97. The error message is "Member or data member is not found" and it highlighted a line of code that has not be changed and has been compiled with no error for days. The code was Me.CircuitQualifier.Requery
52
6830
by: entropy123 | last post by:
Hey all, I'm working with some legacy C code and I would like to compile it as a CPP file. I get the following error message: driver.cpp:87: cannot convert `void *' to `GenericStruct *' in assignment
6
12819
by: FTSoJ | last post by:
On a file A.c I define the structure T1 typedef struct { int dummy; int age; char lastname ; char firstname; } T1_user; On a file B.c I define a strcuture T2
6
4450
by: Arthur | last post by:
I had two Visual Studio .NET C++ solutions that I combined into one. These project solutions were very similar. In fact, the two solutions were sharing many files. Now that they are one solution, I #ifdef'd pre-processor variables throughout the code that will compile the code differently. The solution needs to be #define'd differently depending on which computer I compile it on:
4
2371
by: mike | last post by:
I need to do a minor update to a 8051 program written in assembler in 1994 and assembled on a machine/language that no longer exists. I wrote the program, so I have some idea how it works. The assembler was asm80 A Cross Assembler for the Intel 8080-8085 With CPM syntax
6
1394
by: Howard Gardner | last post by:
/* As it sits, this program won't compile for me. It will compile using either of the versions of tpt that I've commented out. Which versions SHOULD compile? I think that the code is interesting because I think that it is on the path to building a useful non-intrusive type introspection facility. The version that uses "has" indicates the direction.
5
5048
by: wong_powah | last post by:
#include <vector> #include <iostream> using std::cout; using std::vector; enum {DATASIZE = 20}; typedef unsigned char data_t;
2
3535
by: Colin Han | last post by:
Hi, all, If I write follow code in c# method. The IDE will compile it to a complex construct method of System.Linq.Expression. Expression<Func<int>ex = () =10; will be compile to: Expression<Func<int>ex = Expression.Lambda<Func<int>(Expression.Literal(10)) It is so smart. I can travel in this expression tree. I can do some smart abilities on this design. Now, I want write a console application. If user input follow string from console,...
0
9714
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
9594
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
10599
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
10346
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
9173
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
5531
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.