Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用线程时内存分配出现问题_C++_Multithreading_Memory_Allocation - Fatal编程技术网

C++ 使用线程时内存分配出现问题

C++ 使用线程时内存分配出现问题,c++,multithreading,memory,allocation,C++,Multithreading,Memory,Allocation,我正在尝试与使用数据分配的结构一起使用。尽管在每个函数中都得到了正确的输出,但当这些函数通过线程运行时,我还是遇到了溢出错误。我目前的代码是: #include <iostream> // cout, endl #include <thread> // thread using namespace std; // a structure to hold parameters to pass to a thread struct StatData { //

我正在尝试与使用数据分配的结构一起使用。尽管在每个函数中都得到了正确的输出,但当这些函数通过线程运行时,我还是遇到了溢出错误。我目前的代码是:

#include <iostream> // cout, endl
#include <thread>   // thread

using namespace std;

// a structure to hold parameters to pass to a thread
struct StatData
{
    // the number of numbers
    int n;
    // the array of numbers
    double *numbers;
};

void average(StatData *data, double *avg) {
    int id;
    int size = data->n;
    double sum = 0.0;

    for(int i = 0; i < size; i++){
        sum += data->numbers[i];
    }
    *avg = sum / size;
}

void minimum(StatData *data, double *min) {
    int id;
    int size = data->n;
    *min = data->numbers[0];

    for(int i = 0; i < size; i++){
        if (*min > data->numbers[i]){
            *min = data->numbers[i];
        }
    }
}

void maximum(StatData *data, double *max) {
    int id;
    int size = data->n;
    *max = data->numbers[0];

    for(int i = 0; i < size; i++){
        if (*max < data->numbers[i]){
            *max = data->numbers[i];
        }
    }
}

int main(int argc, char** argv) {
    // checking if arguments were passed
    if (argc <= 1) {
        cout << "No numbers were passed." << endl;
        cout << "At least one number needs to be entered." << endl;
        return 1;
    }

    // declaring worker threads
    thread avg_t, min_t, max_t;

    // variables for values
    double avg_v;
    double min_v;
    double max_v;

    // initalizing data structure to hold numbers
    StatData data;
    data.n = argc - 1;                  // the amount of numbers passed
    data.numbers = new double[data.n];  // allocating space for nums

    // Filling in the array with numbers using atof
    for (int i = 0; i < data.n; i++) {
        data.numbers[i] = atof(argv[i+1]); // converting string to double
    }

    // creating average thread
    avg_t = thread(average, &data, &avg_v);

    // creating minimum thread
    min_t = thread(minimum, &data, &min_v);

    // creating maximum thread
    max_t = thread(maximum, &data, &max_v);

    // wating for threads to finish
    avg_t.join();
    min_t.join();
    max_t.join();

    printf ("The average value is %d\n", &avg_v);
    printf ("The minimum value is %d\n", &min_v);
    printf ("The maximum value is %d\n", &max_v);

    // freeing up dynamically allocated space
    delete[] data.numbers;
}
当我运行值为47、58和6的./test时,我得到一个打印的语句: 平均值为-50245280 最小值为-50245288 最大值为-50245296


我不确定是哪里出了问题,导致代码这样做。

您正在打印值的地址&avg_v是指向avg_v的指针,当打印为%d时,它将打印指针本身的一部分值

要打印双值本身,只需执行以下操作

    printf ("The average value is %lf\n", avg_v);
    printf ("The minimum value is %lf\n", min_v);
    printf ("The maximum value is %lf\n", max_v);
请注意,%d用于打印整数,对于浮点值,请使用%f或%lf,两者是等效的

&仅在scanf中需要,因为scanf接受变量的地址。printf接受实际值,因此不需要向它传递地址,当然,除非您想打印地址

<> P>已排序,可以通过使用更多的C++标准库特征,使程序变得更简单。举几个例子:std::vector、std::accumulate、std::min_元素、std::max_元素、std::async,它为您创建一个线程并返回future和lambda:

#include <iostream>
#include <thread>
#include <vector>
#include <numeric>
#include <algorithm>
#include <future>

int main(int argc, char** argv) {
    if (argc <= 1) {
        std::cout << "No numbers were passed.\n"
            "At least one number needs to be entered.\n";
        return 1;
    }

    std::vector<double> data;
    for (int i = 1; i < argc; i++) {
        data.push_back(atof(argv[i]));
    }

    auto avg_f = std::async([&] {
        return accumulate(data.begin(), data.end(), 0.0) / data.size();
    });
    auto min_f = std::async([&] {
        return *min_element(data.begin(), data.end());
    });
    auto max_f = std::async([&] {
        return *max_element(data.begin(), data.end());
    });

    // the 3 values are now being computed in parallel ...

    double avg_v = avg_f.get(); // get the values from futures (waits as necessary)
    double min_v = min_f.get();
    double max_v = max_f.get();

    std::cout << "The average value is " << avg_v << "\n";
    std::cout << "The minimum value is " << min_v << "\n";
    std::cout << "The maximum value is " << max_v << "\n";
}

您正在打印值的地址&avg_v是指向avg_v的指针,当打印为%d时,它将打印指针本身的一部分值

要打印双值本身,只需执行以下操作

    printf ("The average value is %lf\n", avg_v);
    printf ("The minimum value is %lf\n", min_v);
    printf ("The maximum value is %lf\n", max_v);
请注意,%d用于打印整数,对于浮点值,请使用%f或%lf,两者是等效的

&仅在scanf中需要,因为scanf接受变量的地址。printf接受实际值,因此不需要向它传递地址,当然,除非您想打印地址

<> P>已排序,可以通过使用更多的C++标准库特征,使程序变得更简单。举几个例子:std::vector、std::accumulate、std::min_元素、std::max_元素、std::async,它为您创建一个线程并返回future和lambda:

#include <iostream>
#include <thread>
#include <vector>
#include <numeric>
#include <algorithm>
#include <future>

int main(int argc, char** argv) {
    if (argc <= 1) {
        std::cout << "No numbers were passed.\n"
            "At least one number needs to be entered.\n";
        return 1;
    }

    std::vector<double> data;
    for (int i = 1; i < argc; i++) {
        data.push_back(atof(argv[i]));
    }

    auto avg_f = std::async([&] {
        return accumulate(data.begin(), data.end(), 0.0) / data.size();
    });
    auto min_f = std::async([&] {
        return *min_element(data.begin(), data.end());
    });
    auto max_f = std::async([&] {
        return *max_element(data.begin(), data.end());
    });

    // the 3 values are now being computed in parallel ...

    double avg_v = avg_f.get(); // get the values from futures (waits as necessary)
    double min_v = min_f.get();
    double max_v = max_f.get();

    std::cout << "The average value is " << avg_v << "\n";
    std::cout << "The minimum value is " << min_v << "\n";
    std::cout << "The maximum value is " << max_v << "\n";
}

这是否取决于线程的使用?作为一个新用户,请同时阅读。哦,顺便说一句,使用std::vector!这是否取决于线程的使用?作为一个新用户,请同时阅读。哦,顺便说一句,使用std::vector!