這是修改後的版本。如何爲隊列釋放內存?礦不免費
它需要3.5GB的內存和彈出功能不釋放內存......我如何使用新的和刪除來恢復這些內存?現在我正在使用STL。因爲新增和刪除只適用於指針?
queue<Graphnode> ss;
for(i=0;i<30000000;i++)
{
ss.push(*g.root);
}
printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
for(i=0;i<30000000;i++)
{
ss.pop();
}
printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
//delete &ss;
這裏是我的node.h文件。我想我需要malloc和free或new,在這裏刪除指針?
#include <stdio.h>
#include <stdlib.h>
#include <tr1/array>
typedef struct point
{
std::tr1::array<int, 16> state;
int x;
}point;
typedef struct Graphnode
{
struct point pvalue;
int depth;
struct Graphnode *up;
struct Graphnode *down;
struct Graphnode *left;
struct Graphnode *right;
}Graphnode;
所以修改後的代碼應該看起來像這樣?
#include <stdio.h>
#include <stdlib.h>
#include <tr1/array>
typedef struct point
{
std::tr1::array<int, 16> state;
int x;
int depth;
}point;
typedef struct Graphnode
{
point *pvalue = (point *)malloc(sizeof(point));
Graphnode *up = (Graphnode*)malloc(sizeof(Graphnode));
Graphnode *down= (Graphnode*)malloc(sizeof(Graphnode));;
Graphnode *left= (Graphnode*)malloc(sizeof(Graphnode));;
Graphnode *right= (Graphnode*)malloc(sizeof(Graphnode));;
}Graphnode;
您應該添加「push」函數的代碼,以及如何創建隊列的元素。你用'C++'標記了你的問題,但你試圖使用'free'。 –
對不起,這是爲隊列不堆棧。我改變了這一點。我想知道C中的free()函數有什麼功能? – weeo
有'delete'運算符。請參閱http://www.cplusplus.com/reference/std/new/operator%20delete/。如果你用'new'分配對象,你應該使用'delete'。另外,在常規的C++標準庫中,您可以使用'push_front()'或'push_back()'將元素添加到容器的頭部和尾部,因此可以命名。 –