Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

vector size bug

xie__xie
Beginner
213 Views

When I am running this program, it will return wrong results, which are shown in the comments.

 

#include <iostream>
#include <vector>

int main()
{
  size_t n = 1000000000;
  std::vector<double> V(n);
  std::cout << V.size() << std::endl;
  // output: 1000000000

  std::cout << V.size() << std::endl;
  // output: 18446744073635809792

  std::cout << V.capacity() << std::endl;
  // output: 1000000000

  return 0;
}
 

icc for Mac

Version 19.0.1.144 Build 20181018

0 Kudos
7 Replies
Viet_H_Intel
Moderator
213 Views

these are results from my executions:

vahoang@orcsle147:/tmp$ icpc t.cpp
vahoang@orcsle147:/tmp$ ./a.out
1000000000
1000000000
1000000000

$ cat t.cpp
#include <iostream>
#include <vector>

int main()
{
  size_t n = 1000000000;
  std::vector<double> V(n);
  std::cout << V.size() << std::endl;
  // output: 1000000000
   std::cout << V.size() << std::endl;
  // output: 18446744073635809792


    std::cout << V.capacity() << std::endl;
  // output: 1000000000
  return 0;
}

vahoang@orcsle147:/tmp$ icc -V
Version 19.0.1.144 Build 20181018
 

xie__xie
Beginner
213 Views

But my results are still wrong:

iMac:Documents x$ icpc test.cpp

iMac:Documents x$ ./a.out

1000000000

18446744073635809792

1000000000

iMac:Documents x$ icc -V

Intel(R) C Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 19.0.1.144 Build 20181018

Copyright (C) 1985-2018 Intel Corporation.  All rights reserved.

xie__xie
Beginner
213 Views

The program will show the right results only with "icpc -O1 test.cpp" instruction. If I use '-O2' or the higher optimization option, the results will be wrong.

xie__xie
Beginner
213 Views

Could you tell me what are the specifications of your computer and your default optimization option?

Viet Hoang (Intel) wrote:

these are results from my executions:

vahoang@orcsle147:/tmp$ icpc t.cpp
vahoang@orcsle147:/tmp$ ./a.out
1000000000
1000000000
1000000000

$ cat t.cpp
#include <iostream>
#include <vector>

int main()
{
  size_t n = 1000000000;
  std::vector<double> V(n);
  std::cout << V.size() << std::endl;
  // output: 1000000000
   std::cout << V.size() << std::endl;
  // output: 18446744073635809792

    std::cout << V.capacity() << std::endl;
  // output: 1000000000
  return 0;
}

vahoang@orcsle147:/tmp$ icc -V
Version 19.0.1.144 Build 20181018
 

Viet_H_Intel
Moderator
213 Views

I accidentally compiled at -O1; I am now seeing at -O2. I'll report this bug to our Developer.

Thanks,

Viet

 

Viet_H_Intel
Moderator
213 Views

I have submitted this bug: CMPLRIL0-30860

Thanks,

Viet

xie__xie
Beginner
213 Views

OK

Viet Hoang (Intel) wrote:

I have submitted this bug: CMPLRIL0-30860

Thanks,

Viet

Reply