Home > Mobile >  How can I allocate the right amount of memory to this?
How can I allocate the right amount of memory to this?

Time:12-20

This has somehow become susceptible to buffer overflow. Not sure why or how to fix it?

ps. I am new to programming and any tips to improve the overall quality of the code would be greatly appreciated.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int numberSeries[] = { 1 };
int seriesSize;
int checkOrder = 1;
int counter = 0;
int i = 0;
string arr[] = { "test" };
string value;
string import;
string fileLocation;

void CreateArrayFromFile(string fileLocation);
void InputNumbers();
void PrintArray();
int Iteration(int size, int array[]);
void SwapNumbers(int arrayLocation, int array[]);

int main()
{
    cout << "What you like to numbers 'import' from file or 'manually' enter the numbers? \n";
    cin >> import;
    if (import == "import") {
        cout << "Input file location using full path: ";
        cin >> fileLocation;
        CreateArrayFromFile(fileLocation);
    }
    else if (import == "manually") {
        InputNumbers();
        PrintArray();

        while (counter < seriesSize) {
            Iteration(seriesSize, numberSeries);
        }
    }
    else {
        cout << "INVALID SELECTION";
    }
    
}

void InputNumbers() {

    cout << "What is the required size of the array? \n";
    cin >> seriesSize;
    cout << "Enter " << seriesSize << " numbers: \n";
    for (int i = 0; i < seriesSize; i  ) {
        cin >> numberSeries[i];
    }
    cout << "\n";
}

void PrintArray() {
    cout << "Here are your current numbers: \n";

    for (int i = 0; i < seriesSize; i  ) {
        cout << numberSeries[i] << " ";
    }
    
    cout << "\n";
}

int Iteration(int size, int array[]) {
    for (int i = 0; i < (seriesSize - 1); i  ) {
        if (numberSeries[i] > numberSeries[i   1]) {
            SwapNumbers(i, numberSeries);
            counter = 0;
        }
        else if (numberSeries[i] <= numberSeries[i   1]) {
            counter  ;
        }
    }
    if (counter >= seriesSize) {
        cout << "YOUR NUMBERS HAVE BEEN SORTED\n\n";
    }
    else {
        PrintArray();
        cout << "Iteration complete!\n\n";
    }

    return 0;
}

void SwapNumbers(int arrayLocation, int array[]) {
    int tempSwapOne, tempSwapTwo;
    //store each number
    tempSwapOne = array[arrayLocation];
    tempSwapTwo = array[arrayLocation   1];

    //assign number to new location
    array[arrayLocation] = tempSwapTwo;
    array[arrayLocation   1] = tempSwapOne;
}

void CreateArrayFromFile(string fileLocation) {
    ifstream file;
    file.open(fileLocation, ios::in);

    int* ptr = (int*)malloc(sizeof(file));

    cout << *ptr;

    string line;
    int i = 0;

    if (file.is_open()) {
        while (!file.eof())
        {
            getline(file, line);
            arr[i] = line;
            i  ;
        }
        seriesSize = i;
    }
    else {
        cout << "File could not be opened. Check path is correct...\n\n";
        return;
    }
    
    for (i = 0; i < seriesSize; i  ) {
        int tempNumber = stoi(arr[i]);
        numberSeries[i] = tempNumber;
        cout << numberSeries[i] << " ";
    }
    cout << "\nTotal numbers: " << seriesSize;
}

I tried to assign the correct amount of memory but I have no idea how to figure out the correct amount.

CodePudding user response:

Use std::vector instead. It handles the memory management and will automatically resize.

CodePudding user response:

There is sum issues in the function CreateArrayFromFile: first of all, files store information in char form,whic in size of one bit. when you allocate int, you need to multiply it with size of int. otherwise, it will cause a buffer overflow.

int* ptr = (int*)malloc(sizeof(file)*sizeof(int));
  • Related