recycle in the heap memory

Status
Not open for further replies.

smithmay

Beta member
Messages
4
I have coded a program:
#include<iostream>
using namespace std;
class node
{
public:
node();
node(int i); //constructor,assign i to weight.
....//other members
priavte:
int weight;
};
void main()
{
ma *pnode; //define a pointer pointe to a array of nodes
pnode=new node[4];
for(int i(0);i<4;i++)
{
pnode=node(i); // initiation for the pointer
}
?????//problem here!
}

After some behaviors,I just want to use 3 members of the array--I only want to use pnode[0],pnode[1] and pnode[2],except pnode[3].I wish to delete pnode[3] to recycle one of the memories from the previous behavior "pnode=new node[4]".Just recycle pnode[3],only one element.
How to do that?
The following approaches have been tried,but all failed:
1. delete [] pnode[3];
2. delete pnode[3];
3. ma *tmp=&pnode[3];
delete tmp;
 
The short answer is that what you're trying to do isn't possible.

You must first store the values you want to save and then delete the entire array.

Then you must create a new array and fill it with the values.

No way around this using simple heap memory operations like new and delete.

The reason for this is that heap memory is allocated in contigious blocks, so for obvious reasons, it may not be possible to *grow* the size of an array, therefore this is not permitted, and as a matter of consistency, they cannot be shrunk either.

Honestly, there is no practical reason to do what you're trying to do. It simply just isn't that crucial to delete just one (or a few) elements of heap memory. There is considerable overhead involved in memory allocation and memory freeing operations, so programmers try to avoid doing them frequently.

For instance, a common method for 'dynamic arrays' is to begin by making the array some reasonable initial size N. Then, whenever the array is filled, the array is destroyed and recreated with size 2*N. This process is repeated everytime the array needs to be grown. By this method, you balance the fact that a few extra bytes of memory is a minimal cost, whereas constantly burning CPU to perform slow operations such as memory management is a much greater cost. Waste more, run faster.

If you would like to do the most elegant design, I would recommend you just use a class that encapsulates 'array' functionality in a doubly (or singly) linked list. You would therefore overload the [] operator and other useful operators (for instance, + could be used for array concatenation) and you could manage the array in any way you wished. Of course then you would have the ability to grow or shrink the 'array', as well as delete an element at any arbitrary position, or add an element at any arbitrary position.

If you explore the usage of templates in C++, you can even make this class type-generic so that it could be used with INTs, FLOATs, or even structs or other objects.
 
Thanks for your reply.I realized that the designers of C++ may have noticed that some behavios like mine could brought somethings out of tune with the disciplines,so I have to compromise.
 
Status
Not open for further replies.
Back
Top Bottom