Pointer question

I was wondering, what would happen if i did something along the lines of

1
2
3
4
5
float x = 4.0;

// then let's input the pointer to x into a method:
Method1(&x);


and then in Method1 we delete the pointer.......

1
2
3
4
5
6

void SomeClass::Method1(float *num)
{
  delete num;
}

You cant delete num that way.

In fact this piece of code will do nothing
1
2
3
4
void SomeClass::Method1(float *num)
{
  delete num;
}


To show that it is not doing anything,
you can run this just to make sure

1
2
3
4
5
6
7
void Method1(float *num)
{
  delete num;
  
  cout << num << endl;
  cout << *num << endl;
}


Result will be
1
2
0x22ff74 //ur value mite be different
4
ah ok, excellent. i'd seen something in some code i was looking at, and it puzzled me some what!
I think that that is undefined behaviour. You should delete only things that were allocated with new
Topic archived. No new replies allowed.