读取/写入向量时访问冲突写入位置0x00000000

时间:2017-12-02 10:35:44

标签: c++ arrays vector

我需要将Java alghoritm转换为C ++。我很难做到这一点,因为C ++绝对不是我的语言,与Java相比,它有时更难和不合逻辑。 (至少对我来说,是全职Java开发人员)。

无论如何,我失败了。代码在逻辑方面几乎与Java相同,但仍然不起作用:(最糟糕的是将传统的Java数组转换为C ++的vector<int>。我认为我搞砸了。

这是我的C ++代码:

#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>

using namespace std;

double calculate(int n, int k, vector<int>& values);

int main()
{
    printf("%f\n", calculate(7, 4, vector<int>{ 2, 2, 2, 1, 2, 2, 2 }));
    system("pause");
    return 0;
}

double calculate(int n, int k, vector<int>& values) {
    int amount = k;
    int offset = 0;
    int predictions = (n - k) + 1;

    vector<vector<int>> arrays;

    int index = 0;
    while (predictions > 0) {
        for (int i = 0; i < predictions; i++) {
            vector<int> arr;

            for (int y = 0; y < amount; y++) {
                arr[y] = values[offset + y]; // HERE IS THE ERROR!!!
            }

            arrays[index] = arr;
            offset++;
            index++;
        }
        amount++;
        offset = 0;
        predictions--;
    }
    vector<double> averages;
    for (size_t a = 0; a < arrays.size(); a++) {
        vector<int> arr = arrays[a];

        int sum = 0;
        for (int value : arr) 
            sum += value;

        averages[a] = (double)sum / (double)arr.size();
    }
    double biggestAverage = 0;
    for (double average : averages) {
        if (average > biggestAverage)
            biggestAverage = average;
    }
    return biggestAverage;
}

这里的Java代码:(完美无缺地工作)

import java.text.DecimalFormat;
import java.util.Arrays;

public class Main {

    private static final DecimalFormat df = new DecimalFormat("#.000");

    public static void main(String[] args) {
        System.out.println(calc(7, 4, 2, 2, 2, 1, 2, 2, 2));
    }

    public static double calc(int n, int k, int... values) {
        int amount = k;
        int offset = 0;
        int predictions = (n - k) + 1;

        int[][] arrays = new int[n * n][n];

        int index = 0;
        while (predictions > 0) {
            for (int x = 0; x < predictions; x++) {
                int[] arr = new int[amount];

                System.arraycopy(values, offset, arr, 0, amount);

                //CLEAR CODE!!! WORKS TOO!!!
                //for (int y = 0; y < amount; y++) {
                //    arr[y] = values[offset + y];
                //}


                arrays[index] = arr;
                offset++;
                index++;
            }
            amount++;
            offset = 0;
            predictions--;
        }
        double[] averages = new double[arrays.length];
        for (int a = 0; a < arrays.length; a++) {
            int[] arr = arrays[a];

            int sum = 0;
            for (int anArr : arr)
                sum += anArr;

            averages[a] = (double) sum / (double) arr.length;
        }

        double biggestAverage = 0;
        for (double average : averages) {
            if (average > biggestAverage)
                biggestAverage = average;
        }
        return Double.valueOf(
                df.format(biggestAverage)
                        .replace(",", "."));
    }

}

完整错误堆栈跟踪(C ++):

Exception thrown at 0x010C10B5 in Application.exe: 0xC0000005: Access violation writing location 0x00000000.
PS:这个网站比任何使命召唤社区都毒性更大。天哪。禁止6个月和-5代表。对于一个相当正常的问题,咸c ++开发人员攻击我的Java“转换”。我知道我不应该发布它需要转换Java代码。知道了,但我认为开发人员智商高于小孩子。我错了。我想如果我发布一个没有任何Java代码等的正常问题,我会得到一个正常的回答,而不是一个“你不能”的废话,因为显然我可以,帮助我的人可以帮助我,不是自私的刺。如果您不想提供帮助,请不要帮忙,如果它确实违反了“规则”(xD)或其他内容,请删除该问题。但真的吗? 6个月禁止一个咸咸的C ++开发人员不喜欢的问题?真?我以前喜欢这个页面,但社区boi ...

1 个答案:

答案 0 :(得分:1)

当你执行

时,你需要在为它们赋值之前分配向量元素
vector<int> arr; // defines an empty vector.
arr[y] = ...     // access element at position y.

arr这里是空的,指向一个零数组,你试图写入内存0x0000。

结帐vector documentation,查看初始化它的不同方法。

在C ++中,你需要处理你的内存,你不能只是从不同的高级语言中移植代码。

有不同的方法,例如

vector<int> arr;
arr.reserve(amount);
for (int y = 0; y < amount; ++y) {
  arr.push_back(values[offset + y]);
}
相关问题