简体   繁体   中英

Issue With Freeing Dynamically Allocated Memory In C++

Before my program can free up memory and end it crashes. Crashes seem to happen on transition from the function UserDataCollection and back to main. This is only my second program using pointers so I'm still quite the newbie considering the whole point of c++ is to use pointers.

Here is the aforementioned code:

#include <iostream>

//Prototypes
void UserDataCollection(int * &movieData_ptr, int &numSurveyed); // Movie Statistics
void DisplayOutput(int *movieData_ptr, int numSurveyed); //Mean, Median, Mode (Display To Console)

//Global Constants

int main()
{
    //Variables
    int numSurveyed = 0;

    //Pointers
    int * movieData_ptr = nullptr;
    movieData_ptr = new int[numSurveyed];

    //"Program Start"
    std::cout << "Program start...\n\n";
    UserDataCollection(movieData_ptr, numSurveyed);
    DisplayOutput(movieData_ptr, numSurveyed);

    //Release Memory
    delete[] movieData_ptr;
    std::cout << "Memory Cleared.";

    return 0;
}

void UserDataCollection(int * &movieData_ptr, int &numSurveyed)
{
    //Get Number of Students Surveyed
    std::cout << "How many students were surveyed: ";
    std::cin >> numSurveyed;

    //Student Data Input Loop
    for (int i = 0; i < numSurveyed; i++)
    {
        //Get Student Data
        std::cout << "Enter How many movies student " << i + 1 << " has seen in ONE month: ";
        std::cin >> *(movieData_ptr + i);

        //Validation Check
        while (*(movieData_ptr + i) >= 337)
        {
            std::cout << "\nImpossible value!" << std::endl
                << "Hours in a month: 730. Average movie length: 130 minutes."
                << "Total Possible movies: 337";

            std::cout << "\n\nEnter How many movies student " << i + 1 << " has seen in ONE month: ";
            std::cin >> *(movieData_ptr + i);
        } //end while (Validation Check)
    } // end for (Data Input)
}

void DisplayOutput(int *movieData_ptr, int numSurveyed)
{
    //Display loop for pointer array
    for (int i = 0; i < numSurveyed; i++)
    {
        std::cout << *(movieData_ptr + i) << " ";
    }

    //End Message
    std::cout << "\n\nProgram end.";
}

You never allocated any memory.

int numSurveyed = 0;

//Pointers
int * movieData_ptr = nullptr;
movieData_ptr = new int[numSurveyed];

This is the equivalent of

int *movieData_ptr = new int[0];

You are allocating size of 0 ints. This is undefined behaviour. You can't do anything useful with that pointer without a segmentation fault. You need to either pre-allocate a certain amount, and make sure you don't overflow, or dynamically allocate every time you plan to add data.

Since this is C++, it's probably better not to use raw pointers, but use vector or something instead.

Sorry:

From 5.3.4/7

When the value of the expression in a direct-new-declarator is zero, the allocation function is called to allocate an array with no elements.

From 3.7.3.1/2

The effect of dereferencing a pointer returned as a request for zero size is undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM