jeudi 13 février 2020

How do I fix compiler warnings C6386 and C6385?

For the program I am writing I need to create an array with a user-defined size, as well as random values between -15 and 15. As such i am using srand along with dynamic array casting. This is my full code below:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    //initialize variables
    double minValue;
    double maxValue;
    double firstQuartile;
    double thirdQuartile;
    double skewness;

    int size;
    std::cout << "Please enter the size of the data set: ";                 //get data set size from user
    std::cin >> size;
    int* dataSet = new int[size];                                                           
    cout << endl << "These are the randomly generated values: ";

    srand(static_cast<unsigned int>(time(NULL)));

    int i = size;
    for (int x = 0; x < i; x++)                                         //generate random numbers
    {
        dataSet[i] = (rand() % 31) - 15;
        cout << dataSet[i] << " ";
    }


} 

While it still compiles, I need it to do so without errors and the section I've highlighted below is causing two errors I do not know how to fix.

int i = size;
    for (int x = 0; x < i; x++)                                         //generate random numbers
    {
        dataSet[i] = (rand() % 31) - 15;
        cout << dataSet[i] << " ";
    }

C6386 Buffer overrun while writing to 'dataSet': the writable size is 'size*4' bytes, but 'i' bytes might be written.

AND

C6385 Reading invalid data from 'dataSet': the readable size is 'size*4' bytes, but 'i' bytes may be read.




Aucun commentaire:

Enregistrer un commentaire