1

This code C6262 warning keeps showing up causing problems to the program and I tried searching for possible solutions but I am having a difficult time understanding. I would really appreciate if some could help me with this and if you could point out any bad parts of the code that I could improve.

#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;

class sorting {
private:
int size, elements;
int arr[5000], x;


public:
void sort() {
    cout << "Enter number of desired elements for the 1st set" << ">"; cin >> elements;
    arr[elements];
    half(); cout << endl;
    bubble();

    
    for (int i = 0; i < elements; i++) {
        cout << arr[i] << " ";

    }

    

}
void half() {
    for (int i = 0; i < elements / 2; i++) {
        arr[i] = i + 1;
    }
    for (int i = elements / 2; i < elements; i++) {
        arr[i] = rand();
    }
    cout << "This is the elements of the 1st set: ";
    for (int i = 0; i < elements; i++) {
        cout << arr[i] << " ";
    }
}
void random() {
    for (int i = 0; i < elements; i++) {
        arr[i] = i + 1;

    }

    random_shuffle(&arr[0], &arr[elements]);

    cout << "This is the elements of the 2nd set: ";

    for (int i = 0; i < elements; i++) {
        cout << arr[i] << " ";

    }

}
void ascend_descend() {
    int x = elements / 2;
    arr[0] = x;
    for (int i = 0; i < elements / 2; i++) {
        arr[i + 1] = x - 1;
        x--;
    }
    for (int i = elements / 2; i < elements; i++) {
        arr[i] = i + 1;
    }
    cout << "This is the elements of the 3rd set: ";
    for (int i = 0; i < elements; i++) {
        cout << arr[i] << " ";
    }



};
void bubble() {
    for (int i = 0; i < elements; i++) {
        int temp;
        for (int j = i + 1; j < elements; i++) {
            if (arr[j] < arr[i]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }

        }

    };
}
  };

int main()
{   
    sorting sortObject;

    sortObject.sort();
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    `This warning indicates that stack usage that exceeds a preset threshold (constant_2) has been detected in a function.` https://learn.microsoft.com/en-us/cpp/code-quality/c6262?view=vs-2019 Consider a vector. Also, `arr[elements];` doesn't do what you may think it does, and your program doesn't compile as-is. – Retired Ninja Oct 21 '20 at 03:38
  • Your code formatting leaves a lot to be desired. You should not place semicolons after the `}` at the end of a function. – Jonathan Leffler Oct 21 '20 at 04:08
  • What do you think the line `arr[elements];` does? It doesn't resize the array... it only returns a reference to the element with index "elements"... but you don't do anything with it, so the code is redundant. – JHBonarius Oct 21 '20 at 05:30

1 Answers1

2

Right at the top of the class declaration you have int arr[5000], x; That will allocate the array of ints on the stack. Your compiler is warning you that that array might not fit on the stack, and you should allocate it on the heap instead. For an explanation of stack vs heap see this. Assuming you still want to use a c-style array with pointers (see my second solution for what would be more idiomatic), you would allocate the array in the constructor, and free it in the destructor.

class sorting
{
int  size;
int  elements;
int* arr;
int  x;

public:
    sorting()
    {
        arr = new int[5000];
    }

    ~sorting()
    {
        delete arr;
    }
/*the rest of your code...*/
};

This solution takes more of a manual memory management style, and unless you have a good reason for doing so, I would recommend using std::vector instead of manually allocating your array. This will keep track of the elements and size. So your member variables will look like this instead:

class sorting
{
private:
    int x;
    int elements;
    vector<int> arr;
}

Then you can use arr.push_back(int) to add elements the vector. arr[i] to access and set them, and arr.size() to get how many elements are in your array.

Dylan Gentile
  • 146
  • 1
  • 5
  • Your example should obey the rule of three/five – Alan Birtles Oct 21 '20 at 05:54
  • @JHBonarius @Alan Birtles I understand why `unique_ptr` and the rule of 3/5 are important here, but that over complicates the actual problem, and the op appears to be a beginner. If the op doesn't know the difference between heap and stack, and their code has a line like `arr[elements];`, talking about the rule of 3, and smart pointers seems like a bad place to start, regardless of being idiomatic. I'd rather that they learn what it means to allocate memory on the heap with `new`, and then later discover why we use smart pointers and the rule of 3. – Dylan Gentile Oct 21 '20 at 23:01
  • For me that logic sounds like "you need to learn to use a typewriter before you can start using a word processor" and I thus disagree. You should teach based on the newest insights. Raw pointers are just in the language for rare edge cases and legacy reasons. – JHBonarius Oct 22 '20 at 05:07