我正在进行纹理合成项目。 我差不多完成了,但是我的过程在运行时会花费大约2GB的内存。 我以为我释放了所有的calloc'ed阵列,但我可能是错的。 C ++不是我的专长。
我知道三重指针在C ++中是错误的,但我现在对它们感觉更舒适。 如果您需要查看更多代码,请告知我们。 谢谢。
MetaPic声明
class MetaPic { public: Pic* source; Pixel1*** meta; int x; int y; int z; MetaPic(); MetaPic(Pic*); MetaPic(const MetaPic&); MetaPic(int, int, int); MetaPic& operator=(const MetaPic&); ~MetaPic(); void allocateMetaPic(); void copyPixelData(); void copyToOutput(Pic*&); void copyToMetaOutput(MetaPic&, int, int); void copyToSample(MetaPic&, int, int); void freeMetaPic(); };分配三重指针
void MetaPic::allocateMetaPic() { meta = (Pixel1***)calloc(x, sizeof(Pixel1**)); for(int i = 0; i < x; i++) { meta[i] = (Pixel1**)calloc(y, sizeof(Pixel1*)); for(int j = 0; j < y; j++) { meta[i][j] = (Pixel1*)calloc(z, sizeof(Pixel1)); } } }释放三重指针
void MetaPic::freeMetaPic() { for(int j = 0; j < y; j++) { for(int i = 0; i < z; i++) free(meta[i][j]); } for(int i = 0; i < x; i++) free(meta[i]); free(meta); }析构函数
MetaPic::~MetaPic() { freeMetaPic(); }=操作员
MetaPic& MetaPic::operator=(const MetaPic& mp) { freeMetaPic(); source = mp.source; x = mp.x; y = mp.y; z = mp.z; allocateMetaPic(); copyPixelData(); return *this; }复制构造函数
MetaPic::MetaPic(const MetaPic& mp) { source = mp.source; x = mp.x; y = mp.y; z = mp.z; allocateMetaPic(); copyPixelData(); }I am working on a texture synthesis project. I am almost finished, however my process is taking ~2gb of memory while it is running. I thought I was freeing all of my calloc'ed arrays, but I could be wrong. C++ isn't my specialty.
I know triple pointers were the wrong way to go about it in C++ but I am just more comfortable with them at the moment. Let me know if you need to see more code. Thank you.
MetaPic declaration
class MetaPic { public: Pic* source; Pixel1*** meta; int x; int y; int z; MetaPic(); MetaPic(Pic*); MetaPic(const MetaPic&); MetaPic(int, int, int); MetaPic& operator=(const MetaPic&); ~MetaPic(); void allocateMetaPic(); void copyPixelData(); void copyToOutput(Pic*&); void copyToMetaOutput(MetaPic&, int, int); void copyToSample(MetaPic&, int, int); void freeMetaPic(); };Allocating the triple pointer
void MetaPic::allocateMetaPic() { meta = (Pixel1***)calloc(x, sizeof(Pixel1**)); for(int i = 0; i < x; i++) { meta[i] = (Pixel1**)calloc(y, sizeof(Pixel1*)); for(int j = 0; j < y; j++) { meta[i][j] = (Pixel1*)calloc(z, sizeof(Pixel1)); } } }Deallocating the triple pointer
void MetaPic::freeMetaPic() { for(int j = 0; j < y; j++) { for(int i = 0; i < z; i++) free(meta[i][j]); } for(int i = 0; i < x; i++) free(meta[i]); free(meta); }Destructor
MetaPic::~MetaPic() { freeMetaPic(); }= Operator
MetaPic& MetaPic::operator=(const MetaPic& mp) { freeMetaPic(); source = mp.source; x = mp.x; y = mp.y; z = mp.z; allocateMetaPic(); copyPixelData(); return *this; }Copy constructor
MetaPic::MetaPic(const MetaPic& mp) { source = mp.source; x = mp.x; y = mp.y; z = mp.z; allocateMetaPic(); copyPixelData(); }最满意答案
当你释放你似乎在你的循环中使用错误的终止值。 只需使freeMetaPic函数能够撤消分配函数的作用即:
void MetaPic::freeMetaPic() { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { free(meta[i][j]); } free(meta[i]) } free(meta); }When freeing you seem to be using the wrong terminating values in your loops. Just make the freeMetaPic function undo what the allocate function does, ie:
void MetaPic::freeMetaPic() { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { free(meta[i][j]); } free(meta[i]) } free(meta); }更多推荐
发布评论