Relative Content

Tag Archive for sortingoopc++17stdvectorstatic-methods

Static comparison function results in heap buffer overflow

class Solution { private: static bool cmp(const int& a, const int& b) { return a >= b; } public: long long function(vector<int>& nums) { sort(nums.begin(), nums.end(), cmp); return 0; } }; Above code results in heap buffer over-flow if nums is: [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] but works fine if nums is: [1,1,1,1,1,1,1,1,1,1,1,1,1] I resolved this issue by making: […]