0

Possible Duplicate:
Deleting a pointer to const (T const*)

Lets take a look to this lines of code:

char * x = new char [2];
x[0] = 'a'; // OK
x[1] = 'b'; // we can modify data using x
const char * y = x; 
y[0] = 'c'; // Error
y[1] = 'd'; // we can't modify data using y
delete [] y; //but we can deallocate memory (i.e. delete data) using y

So in my opinion it is more naturally to have restriction to not be able deallocate memory using const pointer to it. Why there is no such restriction?

Community
  • 1
  • 1
Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111
  • On the philosophical side, `const` says that you cannot change the object pointed to. You don't, you just make it disappear! – Bo Persson May 12 '11 at 06:47
  • I had the similar doubt..see this question: http://stackoverflow.com/questions/755196/deleting-a-pointer-to-const-t-const – Naveen May 12 '11 at 06:47
  • But `free` cannot deallocate `const` pointers. It has the signature `free(void*)`, not `free(void const*)`. – neuront May 12 '11 at 07:15

4 Answers4

2
const T* p = new T();
// uh oh; how do I destroy the object pointed to by 'p'?
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • You contradict yourself. In the original question you said "we can't modify data using `y`." Here you say that we can in fact modify `y` (by using `const_cast(y)[1] = 'd';`. – James McNellis May 12 '11 at 07:04
  • 2
    I think what you are really asking is "why is the destruction of an object not considered modification of the object?" Aside from the fact that it would be awkward (at best) if we couldn't destroy const-qualified objects, I think it's fair to argue that destruction is not modification because once the object is destroyed it ceases to exist: there is no way to observe that the object was modified because there is no way to observe the object at all. – James McNellis May 12 '11 at 07:08
  • No @James I answered your question `how do I destroy the object pointed to by 'p'?`. And also I don't think it is good to create an object and assign it to pointer to const, this can be needed quiet rarely. – Mihran Hovsepyan May 12 '11 at 07:08
  • 3
    Suppose that `delete p;` was ill-formed where `p` is a `const T*`. Your assertion is that you can still destroy the object pointed to by `p` using `delete const_cast(p);`. My point is that if you say that, then it's just as legitimate to say that you can in fact modify the data pointed to by `p` by using `const_cast(p)`. – James McNellis May 12 '11 at 07:26
  • @James All we know that in c++ const is not logical constant, but physical, and we should write such code that makes it logical. So I think it is more "phisical" to don't allow deleting memory via pointer to const. – Mihran Hovsepyan May 12 '11 at 07:33
0

If you couldn't, you would not be able to write things like:

const std::string name = "fred";
0

A better question is why wouldn't you be able to deallocate? You are asking for a restriction, but what problem does that restriction solve. Does it really help your code or does it just get in the way?

const keeps you from modifying data that you probably shouldn't be modifying. Remember that delete is going to get rid of the memory. Your const pointer will still point to the same address, and it will still complain if you try to modify whatever data is there after the deletion. Its up to you not point to deleted memory.

You can think of it as a "view" of the memory. There may be other views that are non const. delete operates on the actual data, not the "view".

BigSandwich
  • 2,768
  • 2
  • 22
  • 26
  • "delete operates on the actual data, not the "view"." Are you saying that modifications only operate on a view and not the actual data? – James McNellis May 12 '11 at 06:56
  • No, I'm saying that type only matters if you want to modify the data, delete doesn't care about the type, only the underlying memory. Delete doesn't modify anything, it destroys it. – BigSandwich May 12 '11 at 15:58
0

Beacause you allocate memory with NEW the data is stored on the HEAP zone, so you can use the DELETE statement. Y points to the x who is on the heap memory zone.

(const char * y = x; it means that you can't change the value of y because is declared as constant. if it was declared: char *const y=x, you can change the value of y but not his adress) Peace

justCurious
  • 720
  • 7
  • 13