2010-01-19 62 views
2

我深化发展成像库和我正在与该图像数据的数据类型什么数据类型用于图像数据以避免std:bad_alloc?

挣扎由于图像可以具有可变的数据类型(每像素8位,每像素16位)我想实施我的图像数据的指针到

void* pimage_data; 

但是无效*导致各类污秽丑恶的,包括指针算术如

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

现在我必须同意这一空白*不会直接导致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

+0

当您拨打新电话时,您正在操作的图像的大小是多少? (什么是'size_x,size_y'?) – GManNickG 2010-01-19 03:01:01

+2

我们可以看到CImage的构造函数吗? – 2010-01-19 04:19:45

回答

1

bad_alloc可能意味着你没有空闲的内存(因为你说sizeof(CImage)== 28,你很可能会在一个紧密或无限循环)。它也可能意味着你已经损坏了freestore,虽然以前的顽皮内存行为并且它在下一个分配/释放周期中被捕获。一个良好的调试会话可以帮助区分差异。

+0

接受已损坏的freestore。我在其他地方覆盖了内存,只是有时候新的内存会把它全部杀死 – Eric 2010-01-19 22:41:29

2

当新抛出bad_alloc的,这意味着它无法分配请求的大小。造成这种情况的常见原因是使用的垃圾值比预期的要大得多。 (也可能真的耗尽内存。)但是,对于您的代码,无论是sizeof(CImage)是真的很大或bad_alloc从其他一些新的表达式中抛出。

它看起来像你想要一个构造函数而不是create_image,并且可能派生类(每个图像类型一个),而不是存储data_type。

+0

sizeof(CImage)== 28 – Eric 2010-01-19 03:31:07

2

如果你需要一个带有变量BPP的原始数据的缓冲区,可以考虑使用一个unsigned char的数组。封装类中的访问--CImage应该包含一个在构造时分配的数组。更好的是,使用std::vector

相关问题