-1

there! I am new to c++ and encountered a problem of deleting pointer to an array. There was a similar question before if you search "how to properly delete a pointer to array". The conclusion of it is that it's a good habit to keep constent in using new and delete. If you use new <data-type> [] to allocate momery, delete[] should follow.

I am not satisfied with this answer. What does computer do when delete or delete[] is excuted? So, I tried this.

#include <iostream>
using namespace std;

int main()
{
    int* pvalue = NULL;
    pvalue = new int[3]; 
    cout << "address of pvalue:" << pvalue << endl;

    for (int i = 0; i < 3; ++i)
    {
        *(pvalue+i) = i+1;
    }
    for (int i = 0; i < 3; ++i)
    {
        cout << "Value of pvalue : " << *(pvalue+i) << endl;
    }

delete pvalue;
cout << "address of pvalue:" << pvalue << endl;
for (int i = 0; i < 3; i++)
{
    cout << "Value of pvalue : " << *(pvalue+i) << endl;
}

return 0;
}

The output is:

address of pvalue:0x150e010
Value of pvalue : 1
Value of pvalue : 2
Value of pvalue : 3
address of pvalue:0x150e010
Value of pvalue : 0
Value of pvalue : 0
Value of pvalue : 3

Address of that pointer is not NULL as I expected, and only two values are deleted. If I substitute delete pvalue to delete [] pvalue, I would expect that all three values of array are deleted. But, the result is the same.

  • Does delete pvalue tell the computer that this address is free to use somewhere else and doesn't actually change the address that pvalue holds?
  • Does delete pvalue or delete [] pvalue change the values that pointer points to?
  • What's the difference between delete pvalue and delete [] pvalue? It seems like that they behave the same.
Ðаn
  • 10,934
  • 11
  • 59
  • 95
Haiyu
  • 19
  • 3
    *The conclusion of it is that it's a good habit to keep constent in using "new" and "delete"* - The conclusion is actually not to use raw new and delete at all. Use RAII. – StoryTeller - Unslander Monica Jun 13 '17 at 10:17
  • 3
    `delete[]` runs destructors on the individual items of an array, as well as the array itself. Since the elements have trivial d'tors, `delete` and `delete[]` usually do the same. The former is undefined behavior, though. – IInspectable Jun 13 '17 at 10:17
  • 1
    In your code, `delete pvalue` has undefined behaviour, since `pvalue` is the result of a `new []`. – Peter Jun 13 '17 at 10:20
  • 3
    Just use `std::vector` and never worry about `new` and `delete` again. – Henri Menke Jun 13 '17 at 10:21
  • 1
    "I am new to c++ and encountered a problem of deleting pointer" ... if you are new to c++ then the actual problem is that you encountered a `delete` ;). Unless you are taking a course that tries to teach you ancient basics, just stay away from `new` and `delete`. You wont need it for quite some time – 463035818_is_not_an_ai Jun 13 '17 at 10:21
  • 1
    In general, you won't get very far by reading what's in memory after you have deleted things, since you shouldn't be reading things that have gone away. Some thing might get zeroed out for you - trying to read what number you have isn't the best way to see what's happening. You seem to expect a "value to be deleted" to mean it gets set to zero. – doctorlove Jun 13 '17 at 10:24
  • @StoryTeller new and delete are a way to use RAII. Are you trying to say to use smart pointers? If so, that's not what you said. – xaxxon Jun 13 '17 at 10:26
  • 1
    @xaxxon - No I said what I meant to say. `new` and `delete` are not a way to use RAII (did *you* mean to say that RAII can be applied to memory?). RAII is an idiom which can be used to avoid **raw** new and delete, which is what I advised against. – StoryTeller - Unslander Monica Jun 13 '17 at 10:30
  • 1
    To be fair, the answer lies within OS theory. Research memory management in modern OS'es. I'd answer the question like this: "If it's a class, the destructor is invoked as appropriate taking into account inheritance, if any. If it's a POD, nothing happens. Then, a system call to free the memory takes place, and that's when and why you need to research memory management in an OS". – iksemyonov Jun 13 '17 at 10:32
  • 1
    @iksemyonov: The question isn't about OS theory. It's about C++. And in C++ `delete new int[3];` is undefined behavior. – IInspectable Jun 13 '17 at 10:33
  • @IInspectableWith all due respect, the formulation is "What does *computer* do". Nowhere is it stated that the question is limited to the part of the stack that is called C++. – iksemyonov Jun 13 '17 at 10:34
  • 1
    @iksemyonov one small modification there... the business of freeing memory might not involve the OS at all until the application terminates, depending on the implementation details of the memory allocator being used. – Rook Jun 13 '17 at 10:37
  • @Rook Thanks! That's news to me. Does the said allocator affect memory (no pun) allocation for classes other than containers? Where in the stack is it located? I think my experience is restricted to seeing the term "*allocator*" in STL container descriptions. – iksemyonov Jun 13 '17 at 10:40
  • 1
    @iksemyonov I'm hazy on the details; this is something I've come across recently. I'm using 'allocator' to mean the udnerlying implementation of `malloc` and `free`. `malloc` might involve a system call to get a fresh page of memory from the OS, but `free` generally just releases the memory to be re-used in the current process. Have a look at this question, for example: https://stackoverflow.com/questions/2215259 – Rook Jun 13 '17 at 10:45
  • 3
    @iksemyonov: The computer can do **anything**, when a program exhibits undefined behavior. What is and is not undefined is defined by the C++ Standard, making this a C++ question only. Any runtime can choose to handle memory management any way it wants. There are no rules imposed by the language specification (other than meeting the required guarantees). – IInspectable Jun 13 '17 at 10:48
  • @Rook Thanks a bunch, that link is insightful! OK, allocator being the `malloc()` and `free()` implementation is just the conclusion that I've come to in the last 5 minutes, thinking about the problem. – iksemyonov Jun 13 '17 at 10:48
  • @IInspectable I see your point, other than that, I also think we may get down the DRAM physics if we go down the rabbit hole. But, still, I prefer to think of the whole stack, when possible. I tend to think that there are a number of common approaches and it's useful to understand them. Good note on the UB, I'll pay attention to that. – iksemyonov Jun 13 '17 at 10:53

2 Answers2

5
  • Does "delete pvalue" tell the computer that this address is free to use somewhere else and doesn't actually change the address that pvalue holds?

Yes. Exactly this.

  • Does "delete pvalue" or "delete [] pvalue" change the values that pointer points to?

In the very simple case you have shown, no. In general, the destructor of the pointed-to values will be called. This may or may not change the values. Finally (and this is important), there is no way for a valid C++ program to tell - once you have called delete, it is undefined behaviour to examine the values. (Undefined behaviour means anything can happen - including "what you erroneously expected".)

  • What's the difference between "delete pvalue" and "delete [] pvalue"? It seems like that they behave the same.

The difference is that memory allocated by new must be deleted by delete, and memory allocated by new[] must be deleted by delete[]. For some implementations there is no difference at all. For others, using delete on memory allocated by new[] will just fail to run the destructors. For others, your program may halt.

3

Does "delete pvalue" tell the computer that this address is free to use somewhere else and doesn't actually change the address that pvalue holds?

Yes

Does "delete pvalue" or "delete [] pvalue" change the values that pointer points to?

It may call the destructor of the object/objects

What's the difference between "delete pvalue" and "delete [] pvalue"? It seems like that they behave the same.

One deletes one object, the other delets an array of objects.