gpt4 book ai didi

c++ - 如何在 STL 容器中使用 'push_back' 大尺寸对象?

转载 作者:太空狗 更新时间:2023-10-29 23:20:34 25 4
gpt4 key购买 nike

我正在尝试将大型对象“推回”到 STL 容器(双端队列)中。但是我不能'push_back' 使用大内存的对象。

Try-catch block 获取 std::length_error 异常。

如何在STL容器中存储大型复杂对象?

typedef std::deque <
LargeData,
boost::pool_allocator <
LargeData
, boost::default_user_allocator_new_delete
, boost::details::pool::default_mutex
, 32
, 0
>
> LargeDataDeque;

LargeDataDeque v;
LargeData d;
try
{
v.push_back(d);
}
catch( std::length_error &e )
{
cout << e.what();// this line outputs 'too long'
}

//LargeData body
struct LargeData
{
std::wstring name;
CRect desktop_rect;

// computer vision attributes
bool is_cv_target;
bool is_vm_master;
bool is_face_detect_target;
bool is_facemask;
FaceMaskPart facemask_part;
std::deque<std::wstring> masters;
std::deque<std::wstring> slaves;
CPoint facetrack_com;
//

CImage image;
CPoint com;
Vec2 curr_coord_ratio; // %
Vec2 prev_coord_ratio; // %
Vec2 prev_size_ratio;
//CPoint translation;
Vec2 translation;
CPoint trans_diff;
double scale;
double scale_diff;
double degree;
CRect region;
CartesianRect rot_region;
CRect initial_region;
CartesianRect initial_rot_region;

CRect orig_bmp_region; // special tmp data for bmp-wipes
int cap_number;
std::wstring id;
CRect tmp_region;
int depth;
int alpha;

// cv
IplImage *effect_frame;
IplImage *effect_tmp_frame;
IplImage *gray_frame;
IplImage *sepia_frame;

bool use_brightness;
double brightness;
bool use_contrast;
double contrast;
bool use_blur;
int blur_intensity;

bool blur_face;
cv::Mat ft_frame, smallImg;
cv::CascadeClassifier cascade_face_cpu;
std::deque<FaceTrackRegions> facetrack_regions;

bool flip_vertical;
bool flip_horizontal;
bool flip_hv;
bool use_negaposi;
bool use_sepia;

CvMat *cvm_clusters;
CvMat *cvm_points;
CvMat *cvm_color;// = cvCreateMat (MAX_CLUSTERS, 1, CV_32FC3);
CvMat *cvm_count;// = cvCreateMat (MAX_CLUSTERS, 1, CV_32SC1);
CvMat *cvm_centers;// = cvCreateMat (MAX_CLUSTERS, 1, CV_32SC1);

bool use_erode;
bool use_dilate;
int erode_iteration;
int dilate_iteration;

bool use_threshold;
int min_threshold;
int max_threshold;
int threshold_type;
// cv

std::deque<WipeEffect> effects;

bool selected;
bool activated;
bool visible;

WipeSourceType type;
WipeAlignment alignment;

int imagesource_index;
CString image_path;
FIBITMAP* fib_image;
double image_ratio;
bool use_alpha_blend;
bool use_chromakey;

// WS_Bitmap specific
CPoint last_diff_from_ecom;

// Direct2D
// captured bitmap
D2D1_SIZE_U d2dcapbitmap_size;
unsigned int d2dbitmap_bytes;
unsigned int d2dbitmap_pitch;
CComQIPtr<ID2D1Bitmap> d2dcapimage;

LPVOID *d2dtmpbits;
boost::shared_ptr<BYTE> d2dcapbits;
HBITMAP hbm_d2dtmpbits;
HBITMAP hbm_d2dtmpbits_old;
HDC hdc_d2dtmpbits;
BITMAPINFO bi_d2dtmpbits;

// CaptureWorker thread stuffs
boost::shared_ptr<CaptureWorker> capture_worker;
boost::shared_ptr<boost::thread> capture_worker_thread;
//boost::mutex capture_mutex;

// static bitmap
D2D1_SIZE_U d2dstaticbitmap_size;
BITMAPINFO bi_d2dtmpbmpbits;

BYTE *d2dorigbmpbits;
HDC hdc_d2dorigbmpbits;
HBITMAP hbm_d2dorigbmpbits;

// static bitmap's working resources
BYTE *d2dworkbmpbits;
HDC hdc_d2dworkbmpbits;
HBITMAP hbm_d2dworkbmpbits;
CComQIPtr<ID2D1Bitmap> d2dbitmap;

/*CamImageGrabber camgrabber;
CSize prev_cam_reso;*/
CamDeviceInfo caminfo;


// Video frame stuffs
boost::shared_ptr<Video> vid;
bool vid_ready;
bool vid_loop;
bool vid_use_slomo;
boost::shared_ptr<BYTE> vid_buff;
}

最佳答案

std::deque::push_back 不应抛出 std::length_error 除非双端队列中有数十亿个元素。 (在这种情况下,可能没有足够的内存来容纳所有元素,而且它与双端队列本身无关。)

如果您实际上使用了太多内存,您会得到 std::bad_alloc(至少,如果您使用默认分配器)。

由于情况并非如此,我的猜测是您的代码中某处存在未定义的行为,这会导致双端队列的内部内存以某种方式被破坏。

关于c++ - 如何在 STL 容器中使用 'push_back' 大尺寸对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24924386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com