将C++容器与 C 语言结构混合使用
Mixing C++ Containers with C language structs
我花了 2 天时间追查一个涉及容器、std::list
和 C 样式结构C++问题。 是否存在以下未定义的行为(请注意std::list
参数的类型)?
#include <list>
#include <iostream>
using std::cout;
using std::endl;
struct Point_Struct
{
int x;
int y;
};
typedef struct Point_Struct Point;
int main()
{
std::list<Point> line;
Point p;
p.x = 3;
p.y = 10;
line.push_back(p);
cout << "Size of container: " << line.size() << "n";
// Here's the issue:
line.pop_back();
// The size should be zero.
cout << "Size of container after pop_back(): " << line.size() << "n";
return 0;
}
我在Visual Studio 2017上运行了这个,在调用pop_back
时出现异常错误0xC0000005(内存访问)。
任何更改list
中项顺序的方法(例如赋值和sort
)也会引发异常。
如果我将line
的类型更改为std::list<Point_Struct>
,则不会引发异常。
注意:该问题是在较大的程序中发现的。 上面的代码说明了问题的根本原因:std::list<typedef struct>
vs.std::list<struct>
注意:在VS2017调试模式下会引发异常,但在发布模式下不会引发异常。
抱歉,以下有多个问题:
标准C++(11 或更高版本)中是否有任何声明未定义的内容 使用
typedef
而不是struct
作为模板的行为 参数std::list
?这会是一个Visual Studio错误吗?
我还没有尝试过 G++ 或其他编译器。
编辑 1:VS2017 版本信息
Microsoft Visual Studio Professional 2017 版本 15.9.14
已安装产品: Visual C++ 2017
- 00369-60000-00001-AA071
编译信息
配置:调试
平台:Win32
警告级别:级别 3 (/W3)优化:已禁用 (/od)
启用C++异常:是 (/EHsc)
基本运行时检查:两者 (/RTC1)
禁用语言扩展:否
一致性模式:否
平台
平台:视窗 7
我在 Eclipse(Ubuntu 18)中使用 g++ 11 编译并运行了您的代码,它运行良好,
输出:
Size of container: 1
Size of container after pop_back(): 0
您是否尝试过/是否可以将typedef
换成using
?这可能会解决它:
#include <list>
#include <iostream>
using std::cout;
using std::endl;
struct Point_Struct
{
int x;
int y;
};
using Point = Point_Struct;
int main()
{
std::list<Point> line;
Point p;
p.x = 3;
p.y = 10;
line.push_back(p);
cout << "Size of container: " << line.size() << "n";
// Here's the issue:
line.pop_back();
// The size should be zero.
cout << "Size of container after pop_back(): " << line.size() << "n";
return 0;
}
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 结构内部结构的联合 - 混合 C 和 C++
- 将C++容器与 C 语言结构混合使用
- 结构化绑定和转发引用是否混合良好?
- 在 mingw 的 gcc-5.3 下具有混合位域和枚举递减的结构的意外大小
- 混合 C/C++ 代码中的结构指针
- 混合属性的类层次结构令人讨厌
- Qt数据报结构结果:混合序列
- 将OOP与结构代码混合
- 在c++类中混合指向构造结构的指针——在参数列表中声明结构
- C/ c++混合源码:在C代码中获取sizeof(c++结构体)