图像数据使用什么数据类型以避免 std:bad_alloc?
我正在开发一个图像库,并且正在努力处理图像数据数据类型,
因为图像可以具有可变数据类型(每像素 8 位,每像素 16 位),我想实现我的图像数据指针,
void* pimage_data;
但是 void* 会导致所有类型包括丑陋的指针算术,例如
pimage_data = &((unsigned char*)pimage_parent->m_pdata)[offset_y * pimage_parent->m_pitch + offset_x];
我怀疑这有问题,因为当我将它传递给另一个方法时,
CImage* roi = CImage::create_image(size_x, size_y, pimage_parent->m_data_type, pimage_data);
CImage* CImage::create_image(int size_x, int size_y, E_DATA_TYPE data_type, void* pimage)
{
assert(size_x > 0);
assert(size_y > 0);
CImage* image = new CImage(size_x, size_y, data_type);
image->m_pdata = pimage;
return image;
}
新的返回 std::bad_alloc
现在我必须同意 void* 不会直接导致bad_alloc 但我很确定这里出了问题。有什么提示吗?
编辑:
CImage 几乎没有
CImage::CImage(int size_x, int size_y, E_DATA_TYPE data_type)
{
assert(size_x > 0);
assert(size_y > 0);
// Copy of the parameter to the class members
this->m_size_x = size_x;
this->m_size_y = size_y;
this->m_data_type = data_type;
this->m_pitch = size_x;
// The ctor simply create a standalone image for now
this->m_pimage_child = NULL;
this->m_pimage_parent = NULL;
}
尺寸为 x:746,y:325
I'm developping an imaging library and I'm struggling with the image data datatype
Since images can have variable datatypes (8 bits per pixel, 16 bits per pixel) I thought of implementing my image data pointer to
void* pimage_data;
however void* leads to all kind of nastiness including ugly pointer arithmetics such as
pimage_data = &((unsigned char*)pimage_parent->m_pdata)[offset_y * pimage_parent->m_pitch + offset_x];
I suspect that something is wrong with this since when I pass it to another method
CImage* roi = CImage::create_image(size_x, size_y, pimage_parent->m_data_type, pimage_data);
CImage* CImage::create_image(int size_x, int size_y, E_DATA_TYPE data_type, void* pimage)
{
assert(size_x > 0);
assert(size_y > 0);
CImage* image = new CImage(size_x, size_y, data_type);
image->m_pdata = pimage;
return image;
}
the new returns std::bad_alloc
Now I must agree that void* does not directly lead to bad_alloc but I'm pretty sure something is wrong with it here. Any hints?
EDIT:
CImage does close to nothing
CImage::CImage(int size_x, int size_y, E_DATA_TYPE data_type)
{
assert(size_x > 0);
assert(size_y > 0);
// Copy of the parameter to the class members
this->m_size_x = size_x;
this->m_size_y = size_y;
this->m_data_type = data_type;
this->m_pitch = size_x;
// The ctor simply create a standalone image for now
this->m_pimage_child = NULL;
this->m_pimage_parent = NULL;
}
sizes are x:746, y:325
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当 new 抛出 bad_alloc 时,这意味着它无法分配请求的大小。造成这种情况的常见原因是使用了比预期大得多的垃圾值。 (实际上也有可能耗尽内存。)但是,对于您的代码,sizeof(CImage) 要么非常大,要么从其他新表达式中抛出 bad_alloc。
看起来您想要一个构造函数而不是 create_image,并且可能需要带有工厂的派生类(每种图像类型一个)而不是存储 data_type。
When new throws bad_alloc, that means it couldn't allocate the requested size. The common cause for that is using garbage values which are much greater than intended. (It's possible to actually run out of memory too.) For your code, however, either sizeof(CImage) is really huge or bad_alloc is being thrown from some other new expression.
It looks like you want a constructor rather than create_image, and possibly derived classes (one for each image type) with a factory instead of storing data_type.
如果您需要一个带有可变 BPP 的原始数据缓冲区,请考虑仅使用
unsigned char
数组。将访问封装在类中——CImage 应包含一个在构造时分配的数组。更好的是,使用std::vector
。If you need a buffer for raw data with variable BPP, consider just using an array of
unsigned char
. Encapsulate the access inside a class -- CImage should contain an array that it allocates on construction. Better yet, use astd::vector
.bad_alloc 可能意味着您没有可用内存(因为您说 sizeof(CImage) == 28,所以您很可能会在紧密或无限循环中执行此操作)。这也可能意味着您通过之前顽皮的内存行为损坏了自由存储,并且它只是在下一个分配/释放周期中捕获了它。良好的调试会话可以帮助区分。
bad_alloc can mean you are out of free memory (since you say sizeof(CImage) == 28, you would most likely be doing it in a tight or infinite loop). It can also mean you have corrupted the freestore though previous naughty memory behavior and it just caught it on the next allocation/release cycle. A good debugging session can help tell the difference.