【发布时间】:2017-12-20 08:16:35
【问题描述】:
为什么这个简单的c++代码sn-p不能编译?
#include <algorithm>
#define SIZE (1000)
struct S {
int *vect;
};
int main() {
struct S* s = static_cast<struct S*>(malloc(sizeof(struct S)));
s->vect = static_cast<int*>(malloc(sizeof(int) * SIZE));
for(int i = 0; i < SIZE; i++) {
s->vect[i] = i;
}
std::sort(s->vect, s->vect + SIZE);
}
编译器返回以下与 std::sort 调用相关的错误
1>C:\Program Files (x86)\Microsoft Visual
Studio\2017\Enterprise\VC\Tools\MSVC\14.12.25827\include\algorithm(3138):
error : access violation
1> return (pair<_RanIt, _RanIt>(_Pfirst, _Plast));
1> ^
我正在使用 Visual Studio Enterprise 2017 版本 15.5.2 和英特尔编译器 64 位版本 17.0.4.210 Build 20170411。
使用默认的 Visual Studio 编译器成功编译代码。
找不到我做错了什么。
【问题讨论】:
-
您可能想了解如何在 C++ 中初始化数据结构。
malloc在 C++ 中已经被认为是最糟糕的做法,但对于初始化 STL 向量,它根本不起作用。 -
你想做什么?你为什么使用
malloc?为什么所有这些演员?为什么不std::vector? -
尽管使用了 malloc 等,但代码看起来像有效的 C++。
-
我同意代码看起来是有效的——如果“有点”奇怪的话。它对我来说也运行良好,因此错误必须与英特尔编译器有关。 @acco93 你应该在你的问题中添加英特尔编译器标签。
-
@acco93 对我来说,icc 标签似乎比 intel 标签更合适。
标签: c++ compiler-errors icc