This is the revised version.
It takes 3.5GB memory and the pop function doesn’t free the memory… How can I use new and delete to get those memory back? Now I’m using STL. since new and delete only works for pointers?
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;
here is my node.h file. I think I need to malloc and free or New, delete pointers here?
#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;
so after revise the code should look like this?
#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;
If you’re using
c++, you should usequeue<T>from the standard library. Here is a reference: http://www.cplusplus.com/reference/stl/queue/.For c++ code you should never write your own container classes unless it is absolutely necessary. The standard library provides many useful containers that cover most use cases. They are heavily used and tested and have been for many years.