将C++容器与 C 语言结构混合使用

Mixing C++ Containers with C language structs

本文关键字:结构 混合 语言 C++      更新时间:2023-10-16

我花了 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调试模式下会引发异常,但在发布模式下不会引发异常。

抱歉,以下有多个问题:

  1. 标准C++(11 或更高版本)中是否有任何声明未定义的内容 使用typedef而不是struct作为模板的行为 参数std::list

  2. 这会是一个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;
}