0

Why does this simple c++ code snippet do not compile?

#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);

}

The compiler returns the following error related to the std::sort call

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>                      ^

I'm using visual studio enterprise 2017 version 15.5.2 and the intel compiler 64 bit version 17.0.4.210 Build 20170411.

The code is successfully compiled using the default visual studio compiler.

Can't find out what I'm doing wrong.

acco93
  • 128
  • 2
  • 11
  • you may want to have a look at how to initialize data structures in C++. `malloc` is already considered mostly bad practice in C++, but for initializing an STL vector it just won't work at all. – Zinki Dec 20 '17 at 08:18
  • @Zinki there is no STL vector (but probably there should be one) – 463035818_is_not_an_ai Dec 20 '17 at 08:19
  • 3
    what do you want to do? Why are you using `malloc`? why all those casts? why not `std::vector` ? – 463035818_is_not_an_ai Dec 20 '17 at 08:20
  • 3
    Despite the use of malloc etc. the code looks like valid C++. – juanchopanza Dec 20 '17 at 08:24
  • 3
    I'd agree the code looks valid - if "a bit" strange. It also runs fine for me, so the error must be something specific about the intel compiler. @acco93 you should add the intel compiler tag to your question. – TheSHEEEP Dec 20 '17 at 08:28
  • works here : https://ideone.com/doJBeA – 463035818_is_not_an_ai Dec 20 '17 at 08:28
  • It used to run for me too. After the latest visual studio update (the version is reported in the question) and if compiling with the intel compiler it suddenly returns this strange error. I've added an "intel" tag, unfortunately I don't have enough reputation to create an "intel compiler" tag. Maybe someone else could. – acco93 Dec 20 '17 at 08:43
  • 1
    @acco93 To me the icc tag seems even more appropriate than the intel tag. – TobiMcNamobi Dec 20 '17 at 12:30
  • You're right thanks. – acco93 Dec 20 '17 at 13:39

1 Answers1

0

I've found out that unfortunately visual studio update 15.5.x breaks Intel Compiler 2017 as can be seen in the intel forum where I asked this same question. Hope it will be useful to others too.

acco93
  • 128
  • 2
  • 11