Computer ForumsComputers  

Go Back   Computer Forums > Programmers Lounge > Programming Discussions

Reply
 
LinkBack Thread Tools Display Modes
Old 03-04-2006, 12:03 AM   #1 (permalink)
Newb Techie
 
Join Date: Feb 2006
Posts: 4
Default recycle in the heap memory

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[i]=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;
smithmay is offline   Reply With Quote
Old 03-04-2006, 05:20 AM   #2 (permalink)
Ultra Techie
 
Join Date: Jul 2005
Posts: 530
Send a message via AIM to TheHeadFL
Default

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.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline   Reply With Quote
Old 03-06-2006, 08:02 PM   #3 (permalink)
Newb Techie
 
Join Date: Feb 2006
Posts: 4
Default

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.
smithmay is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 10:13 PM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0