VS2008工程,因结构体字节对其问题导致的string赋值出现崩溃,求大佬解答

lhw330472787 2019-03-14 07:48:22


未手动字节对其的情况下,sizeof(结构体) = 77, 在手动对其的情况下赋值不出现崩溃, 求大佬求解!
...全文
290 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgl7903 2019-04-02
  • 打赏
  • 举报
回复
引用 5 楼 lhw330472787 的回复:
已经有写了赋值操作符,这是一整个结构体代码 typedef struct stInputParams { int iLine; int iCorner; int total_layer; float pixel_to_mm; float min_length; float max_length; float max_angle_thresh; bool isDrawBarcode; // 是否在图片上绘制条码 bool isDrawLine; bool isShowData; bool isShowAngle; bool isDetectAngle; cv::Rect detected_rect; std::string strBarcode; stInputParams() { iLine = 1; iCorner = 1; total_layer = 5; pixel_to_mm = 0.01f; min_length = 0.1; max_length = 1.4f; max_angle_thresh = 90.0f; isDrawBarcode = false; isDrawLine = true; isShowData = true; isDetectAngle = true; isShowAngle = true; detected_rect = cv::Rect(0, 0, 0, 0); strBarcode = std::string("ERROR"); } stInputParams operator=(const stInputParams& params) { iLine = params.iLine; iCorner = params.iCorner; total_layer = params.total_layer; pixel_to_mm = params.pixel_to_mm; min_length = params.min_length; max_length = params.max_length; max_angle_thresh = params.max_angle_thresh; isDrawBarcode = params.isDrawBarcode; isDrawLine = params.isDrawLine; isShowData = params.isShowData; isDetectAngle = params.isDetectAngle; isShowAngle = params.isShowAngle; detected_rect = params.detected_rect; strBarcode = params.strBarcode; return(*this); } }stInputParams;
单独测试这个没有问题啊, 是不是其他地方引起的越界/或类信息被修改的操作?
lhw330472787 2019-03-15
  • 打赏
  • 举报
回复
已经有写了赋值操作符,这是一整个结构体代码

typedef struct stInputParams
{
int iLine;
int iCorner;
int total_layer;
float pixel_to_mm;
float min_length;
float max_length;
float max_angle_thresh;

bool isDrawBarcode; // 是否在图片上绘制条码

bool isDrawLine;
bool isShowData;
bool isShowAngle;
bool isDetectAngle;

cv::Rect detected_rect;
std::string strBarcode;

stInputParams()
{
iLine = 1;
iCorner = 1;
total_layer = 5;
pixel_to_mm = 0.01f;
min_length = 0.1;
max_length = 1.4f;
max_angle_thresh = 90.0f;

isDrawBarcode = false;

isDrawLine = true;
isShowData = true;
isDetectAngle = true;
isShowAngle = true;
detected_rect = cv::Rect(0, 0, 0, 0);
strBarcode = std::string("ERROR");
}

stInputParams operator=(const stInputParams& params)
{
iLine = params.iLine;
iCorner = params.iCorner;
total_layer = params.total_layer;
pixel_to_mm = params.pixel_to_mm;
min_length = params.min_length;
max_length = params.max_length;
max_angle_thresh = params.max_angle_thresh;

isDrawBarcode = params.isDrawBarcode;

isDrawLine = params.isDrawLine;
isShowData = params.isShowData;
isDetectAngle = params.isDetectAngle;
isShowAngle = params.isShowAngle;
detected_rect = params.detected_rect;
strBarcode = params.strBarcode;

return(*this);
}

}stInputParams;
sevancheng 2019-03-15
  • 打赏
  • 举报
回复
这个结构体要写深拷贝构造和重载赋值操作符
lhw330472787 2019-03-15
  • 打赏
  • 举报
回复
结构体以这种形式定义也不会出现崩溃
int iLine;
int iCorner;
int total_layer;
float pixel_to_mm;
float min_length;
float max_length;
float max_angle_thresh;

bool isDrawBarcode; // 是否在图片上绘制条码

bool isDrawLine;
bool isShowData;
bool isShowAngle;
bool isDetectAngle:2;

cv::Rect detected_rect;
std::string strBarcode;

lhw330472787 2019-03-15
  • 打赏
  • 举报
回复
#pragma pack 我试过不会崩溃, 结构体变量位置调整也不会出现崩溃。我只是想知道为啥string赋值会崩了
zgl7903 2019-03-15
  • 打赏
  • 举报
回复
#pragma pack 指定结构体字节对齐试试看

#pragma pack(push, 8)
typedef struct _tagxxxx_t
{
}xxxx;
#pragma pack(pop)


16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧