for循环中间的访问冲突写入位置0x00000000

问题描述 投票:0回答:1

我正在尝试将mnist数据集用于神经网络,但我收到访问冲突写入位置0x00000000

代码是

for (int i = 0; i < length; i++) {
    innerarray = (int8_t*)malloc(width * height);
    for (int j = 0; j < width * height; j++) {
        int8_t value = 0;
        innerarray[j] = value;
    }
    temparray[i] = innerarray;
}
for (int i = 0; i < length; i++) {
    for (int j = 0; j < width * height; j++) {
        int8_t grayscale;
        rf.read((char*)&grayscale, 1);
        temparray[i][j] = grayscale; //error happens here
    }
}

可变值:

int length = 10000;
int width = 28;
int height = 28;

奇怪的是,仅当i >= 2512时才发生。同样用grayscale替换0不起作用。我可以在最后嵌套的for循环之前将temparray[2512][0]设置为0。像这样:

for (int i = 0; i < length; i++) {
        innerarray = (int8_t*)malloc(width * height);
        for (int j = 0; j < width * height; j++) {
            int8_t value = 0;
            innerarray[j] = value;
        }
        temparray[i] = innerarray;
    }
    temparray[2512][0] = 0; //works 
    for (int i = 0; i < length; i++) {
        for (int j = 0; j < width * height; j++) {
            int8_t grayscale;
            rf.read((char*)&grayscale, 1);
            temparray[i][j] = 0; //error still happens here
        }
    }

完整的代码是:

#include<iostream>
#include<fstream>
#include<cstdint>
#include<cstdlib>
#include<array>

using namespace std;

struct images {
    int32_t height = 0;
    int32_t width = 0;
    int32_t magicnumber = 0;
    int32_t numberofimages = 0;
    int8_t** images[];
    void setimages(int8_t** newimages) {
        delete[] this->images;
        int8_t** images = (int8_t**)malloc(numberofimages);
        int8_t* innerarray;
        for (int i = 0; i < numberofimages; i++) {
            innerarray = (int8_t*)malloc(width * height);
            images[i] = innerarray;
        }
        for (int i = 0; i < numberofimages; i++) {
            for (int j = 0; j < width * height; j++) {
                images[i][j] = newimages[i][j];
            }
        }
    };
};

struct labels {
    int32_t magicnumber = 0;
    int32_t numberoflabels = 0;
    int8_t labels[];
};

int32_t litleendiantobig(int32_t litle) {//reverse works as well
    int32_t big = ((4278190080 & litle) >> 24) + ((255 & litle) << 24) + ((16711680 & litle) >> 8) + ((65280 & litle) << 8);
        return big;
}

images loadimages(string filename, int32_t magicalnumber) {
    ifstream rf(filename, ios::out | ios::binary);
    if (!rf) {
        cout << "Cannot open file! " << filename << endl;
        exit(1);
    }
    int32_t magicnumberoffile;
    rf.read((char*)&magicnumberoffile, 4);
    magicnumberoffile = litleendiantobig(magicnumberoffile);
    if (magicalnumber != magicnumberoffile) {
        cout << "Wrong magic number!" << endl;
        cout << "expected:" << magicalnumber << endl;
        cout << "got:" <<  magicnumberoffile << endl;
        exit(1);
    }
    images img;
    int32_t length;
    rf.read((char*)&length, 4);
    length = litleendiantobig(length);
    img.numberofimages = length;
    int32_t width;
    rf.read((char*)&width, 4);
    width = litleendiantobig(width);
    img.width = width;
    int32_t height;
    rf.read((char*)&height, 4);
    height = litleendiantobig(height);
    img.height = height;
    int8_t** temparray = (int8_t**)malloc(length);
    int8_t* innerarray;

    for (int i = 0; i < length; i++) {
        innerarray = (int8_t*)malloc(width * height);
        for (int j = 0; j < width * height; j++) {
            int8_t value = 0;
            innerarray[j] = value;
        }
        temparray[i] = innerarray;
    }
    for (int i = 0; i < length; i++) {
        for (int j = 0; j < width * height; j++) {
            int8_t grayscale;
            rf.read((char*)&grayscale, 1);
            temparray[i][j] = grayscale; //error happens here
        }
    }
    img.setimages(temparray);
    rf.close();
    return img;
}

int main() {
    images testimages;
    loadimages("t10k-images.bin", 2051);
    cout << testimages.images;
    return 0;
}

我现在不解决问题,在其他任何地方都找不到。感谢您的帮助。

visual-studio c++17 access-violation dynamic-arrays
1个回答
0
投票

您使用malloc的工作已完成。

 int* array = (int*)malloc(width* height);   // allocate width * height bytes.

 array[i] = x;  // Sets the [i] _integer_ of array to x.
                // but you allocated space for BYTE size elemennts.

使用malloc分配整数的正确方法:

 int* array = (int*)malloc(width* height * sizeof(int));   // allocate width * height ints

或者是您最初的意图是分配8位像素。在这种情况下,您的指针应声明为unsigned char *。

© www.soinside.com 2019 - 2024. All rights reserved.