Will this compile?

L

lennin

#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;
}
 
O

Ondra Holub

(e-mail address removed) 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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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

Rolf Magnus

#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.
 
O

Old Wolf

Rolf said:
Lose that macro. But at least don't use it for integers.


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)
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.
 
S

Salt_Peter

#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.
 
R

Rolf Magnus

Old said:
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.
 
O

Old Wolf

Rolf said:
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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top