程序使用我所有的记忆(Program using up all my memory)

编程入门 行业动态 更新时间:2024-10-23 23:24:29
程序使用我所有的记忆(Program using up all my memory)

我正在进行纹理合成项目。 我差不多完成了,但是我的过程在运行时会花费大约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); }

更多推荐

本文发布于:2023-08-03 09:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385265.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我所   记忆   程序   memory   Program

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!