sort function C++ segmentation fault

前端 未结 3 1632
面向向阳花
面向向阳花 2021-02-04 08:06

In this code, for vector size, n >=32767, it gives segmentation fault, but upto 32766, it runs fine. What could be the error? This is full code.

#include

        
3条回答
  •  生来不讨喜
    2021-02-04 08:45

    Try using all the values from n = 32766 up to 32770. I suspect you'll find that you are experiencing some sort of overflow. This is because 2^15 (32768) is the biggest number that can be represented using 16 bits (assuming you also allow negative numbers). You will have to use a different data type.

    Suggestion:

    Get it to output the vector's maxsize:

    cout << p.max_size();
    

    Let us know what that does. All things being normal, I'd expect it to be in the hundreds of millions (536870911 on my computer). But if it's more like 32768, then that could be the problem.

提交回复
热议问题