图像数据使用什么数据类型以避免 std:bad_alloc?

发布于 2024-08-18 03:53:07 字数 1343 浏览 8 评论 0原文

我正在开发一个图像库,并且正在努力处理图像数据数据类型,

因为图像可以具有可变数据类型(每像素 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温暖的光 2024-08-25 03:53:07

当 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.

故人如初 2024-08-25 03:53:07

如果您需要一个带有可变 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 a std::vector.

蓝眼睛不忧郁 2024-08-25 03:53:07

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文