访问违规阅读位置VS2015

Access violation reading location VS2015

本文关键字:位置 VS2015 访问      更新时间:2023-10-16

首先是我的代码http://ideone.com/qckstz

#include<stdio.h>
#include<cstring>
#include<utility>
using namespace std;
#define nameLength 50
// all 'Rate' attribute appear to be in '%' unit
//exaple: fatRate=70 which mean fatRate is 70%
class ingredient
{
private:
    friend class mix_ingredient;
    char* name;
    unsigned weigh;
    unsigned fatWeigh;
    unsigned glutenWeigh;
    unsigned condenceRate;
public:
    virtual unsigned getWeigh() { return weigh; }
    virtual unsigned getFatWeigh() { return fatWeigh; }
    virtual unsigned getGlutenWeigh() { return glutenWeigh; }
    virtual unsigned getCondenceRate() { return condenceRate; }
public:
    //virtual void ini() = 0;
public:
    ingredient() { name = new char[nameLength]; }
    ingredient(const ingredient& x) :name(new char[nameLength])
    {
        strcpy(name, x.name);
        weigh = x.weigh;
        fatWeigh = x.fatWeigh;
        glutenWeigh = x.glutenWeigh;
        condenceRate = x.condenceRate;
    }
    ingredient(ingredient&& x)noexcept: name(new char[nameLength])
    {
        strcpy(name, x.name);
        weigh = x.weigh;
        fatWeigh = x.fatWeigh;
        glutenWeigh = x.glutenWeigh;
        condenceRate = x.condenceRate;
        x.name = NULL;
    }
    virtual ingredient& operator=(const ingredient& x)
    {
        ingredient tmp(x);
        *this = std::move(tmp);
        return *this;
    }
    ingredient& operator=(ingredient&& x)noexcept
    {
        name = x.name;
        x.name = NULL;
        weigh = x.weigh;
        fatWeigh = x.fatWeigh;
        glutenWeigh = x.glutenWeigh;
        condenceRate = x.condenceRate;
        return *this;
    }
    ~ingredient()
    {
        delete[] name;
    }
};
typedef class raw_ingredient: public ingredient
{
private:
    char* name;
    unsigned weigh;
    unsigned fatRate;
    unsigned glutenRate;
    unsigned condenceRate;
public:
    virtual unsigned getFatWeigh() { return fatRate*(weigh * 5)*condenceRate / 1000000; }
    virtual unsigned getGlutenWeigh() { return glutenRate*weigh * 30 / 10000; }
    virtual unsigned getWeigh() { return weigh; }
    virtual unsigned getFatRate() { return fatRate; }
    virtual unsigned getGlutenRate() { return glutenRate; }
    virtual unsigned getCondenceRate() { return condenceRate; }
public:
public:
    raw_ingredient() { name = new char[nameLength]; }
    raw_ingredient(const char* name, unsigned w, unsigned fR, unsigned gR, unsigned cR)
    {
        this->name = new char[nameLength];
        strcpy(this->name, name);
        weigh = w;
        fatRate = fR;
        glutenRate = gR;
        condenceRate = cR;
    }
    raw_ingredient(const raw_ingredient& x) :name(new char[nameLength])
    {
        strcpy(name, x.name);
        weigh = x.weigh;
        fatRate = x.fatRate;
        glutenRate = x.glutenRate;
        condenceRate = x.condenceRate;
    }
    raw_ingredient(raw_ingredient&& x)noexcept: name(new char[nameLength])
    {
        strcpy(name, x.name);
        weigh = x.weigh;
        fatRate = x.fatRate;
        glutenRate = x.glutenRate;
        condenceRate = x.condenceRate;
        x.name = NULL;
    }
    virtual raw_ingredient& operator=(const raw_ingredient& x)
    {
        raw_ingredient tmp(x);
        *this = std::move(tmp);
        return *this;
    }
    raw_ingredient& operator=(raw_ingredient&& x)noexcept
    {
        name = x.name;
        x.name = NULL;
        weigh = x.weigh;
        fatRate = x.fatRate;
        glutenRate = x.glutenRate;
        condenceRate = x.condenceRate;
        return *this;
    }
    ~raw_ingredient()
    {
        delete[] name;
    }
}raw;
typedef class mix_ingredient: public ingredient
{
private:
    ingredient** ingre;
    unsigned n = 0;//Number of ingredient // 0 mean empty
public:
    void ini();
    int put(ingredient* x);
public:
    virtual unsigned getWeigh() { return weigh; }
    virtual unsigned getFatWeigh() { return fatWeigh; }
    virtual unsigned getGlutenWeigh() { return glutenWeigh; }
    virtual unsigned getCondenceRate() { return condenceRate; }
public:
    mix_ingredient(int n) { ingre = new ingredient*[n]; this->n = n; }
    mix_ingredient(ingredient* x) : ingre(new ingredient*), n(1)
    {
        ingre = &x;
    }
    mix_ingredient(const mix_ingredient& x) :ingre(new ingredient*[n])
    {
        for (int i = 0;i < n;i++)
            ingre[i] = x.ingre[i];
        weigh = x.weigh;
        fatWeigh = x.fatWeigh;
        glutenWeigh = x.glutenWeigh;
        condenceRate = x.condenceRate;
    }
    mix_ingredient(mix_ingredient&& x)noexcept: ingre(new ingredient*[n])
    {
        for (int i = 0;i < n;i++)
            ingre[i] = x.ingre[i];
        weigh = x.weigh;
        fatWeigh = x.fatWeigh;
        glutenWeigh = x.glutenWeigh;
        condenceRate = x.condenceRate;
        x.ingre = NULL;
    }
    virtual mix_ingredient& operator=(const mix_ingredient& x)
    {
        mix_ingredient tmp(x);
        *this = std::move(tmp);
        return *this;
    }
    mix_ingredient& operator=(mix_ingredient&& x)noexcept
    {
        ingre = x.ingre;
        x.ingre = NULL;
        weigh = x.weigh;
        fatWeigh = x.fatWeigh;
        glutenWeigh = x.glutenWeigh;
        condenceRate = x.condenceRate;
        return *this;
    }
    ~mix_ingredient()
    {
        delete[] ingre;
    }
}mix;
void mix_ingredient::ini()
{
    weigh = 0;
    for (int i = 0;i < n;i++)
        weigh += ingre[i]->getWeigh();
    fatWeigh = 0;
    for (int i = 0;i < n;i++)
        if (ingre[i]->getWeigh() > 50)
            fatWeigh += ingre[i]->getFatWeigh();
    glutenWeigh = 0;
    for (int i = 0;i < n;i++)
        if (ingre[i]->getCondenceRate() < 60)
            glutenWeigh += ingre[i]->getGlutenWeigh();
    unsigned tmp = 0;
    for (int i = 0;i < n;i++)
        tmp = ingre[i]->getWeigh()*ingre[i]->getCondenceRate();
    condenceRate = tmp / weigh;
}
int mix_ingredient::put(ingredient * x)
{
    for (int i = 0;i < n;i++)
        if (ingre[i] == NULL)
        {
            ingre[i] = x;
            return 1;
        }
    return 0;
}
    class cake
{
private:
    char* name;
    ingredient** ingre;
    int n;
    unsigned weigh;
    unsigned fatWeigh;
    unsigned glutenWeigh;
    unsigned condenceRate;
public:
    unsigned getWeigh() { return weigh; }
    unsigned getFatWeigh() { return fatWeigh; }
    unsigned getGlutenWeigh() { return glutenWeigh; }
    unsigned getCondenceRate() { return condenceRate; }
public:
    cake(const char* name, int n)
    {
        this->name = new char[nameLength];
        strcpy(this->name, name);
        this->ingre = new ingredient*[n];
        this->n = n;
    }
    int put(ingredient* x)
    {
        for (int i = 0;i < n;i++)
            if (ingre[i] == NULL)
            {
                ingre[i] = x;
                return 1;
            }
        return 0;
    }
    void ini();
    void print();
public:
    cake() { name = new char[nameLength]; }
    cake(const cake& x) :name(new char[nameLength])
    {
        strcpy(name, x.name);
        ingre = new ingredient*(*(x.ingre));        
    }
    cake(cake&& x)noexcept: name(new char[nameLength])
    {
        strcpy(name, x.name);
        x.name = NULL;
        ingre = x.ingre;
        x.ingre = NULL;
    }
    cake& operator=(const cake& x)
    {
        cake tmp(x);
        *this = std::move(tmp);
        return *this;
    }
    cake& operator=(cake&& x)noexcept
    {
        name = x.name;
        x.name = NULL;
        ingre = x.ingre;
        x.ingre = NULL;
    }
    ~cake()
    {
        delete[] name;
        delete ingre;
    }
};
void cake::ini()
{
    weigh = 0;
    for (int i = 0;i < n;i++)
        weigh += ingre[i]->getWeigh();
    fatWeigh = 0;
    for (int i = 0;i < n;i++)
        if (ingre[i]->getWeigh() > 50)
            fatWeigh += ingre[i]->getFatWeigh();
    glutenWeigh = 0;
    for (int i = 0;i < n;i++)
        if (ingre[i]->getCondenceRate() < 60)
            glutenWeigh += ingre[i]->getGlutenWeigh();
    unsigned tmp = 0;
    for (int i = 0;i < n;i++)
        tmp = ingre[i]->getWeigh()*ingre[i]->getCondenceRate();
    condenceRate = tmp / weigh;
}
void cake::print()
{
    printf("%s", name);
}
int main()
{
    //Nguyên liệu nguyên chất
    //1
    raw powder("Bot my",100,0,76,100);
    //2
    raw egg("Trung", 100, 4, 1, 60);
    //3
    raw milk("Sua", 100, 1, 5, 100);
    //4
    raw gelatin("Gelatin", 100, 0, 14, 25);
    //5
    raw cream("Kem tuoi", 100, 8, 14, 70);
    //6
    raw creamCheese("Pho mai kem", 100, 11, 4, 70);
    //7
    raw sugar("Duong", 100, 0, 100, 100);
    //8
    raw cornPowder("Bot ngo", 100, 0, 79, 100);
    //9
    raw water("Nuoc", 100, 0, 0, 0);
    //10
    raw caramen("Caramen", 100, 1, 23, 90);
    //11
    raw flour("Bot nep", 100, 0, 21, 100);
    //12
    raw salt("Muoi", 100, 0, 0, 100);
    //13
    raw bakingPowder("Bot no", 100, 0, 28, 100);
    //Nguyên liệu hỗn hợp
    mix cakeMixture(4);
    cakeMixture.put(&powder);
    cakeMixture.put(&egg);
    cakeMixture.put(&milk);
    cakeMixture.put(&water);
    cakeMixture.ini();
    mix ladyFinger(5);
    ladyFinger.put(&powder);
    ladyFinger.put(&egg);
    ladyFinger.put(&sugar);
    ladyFinger.put(&cornPowder);
    ladyFinger.put(&salt);
    ladyFinger.ini();
    mix mochiMixture(3);
    mochiMixture.put(&sugar);
    mochiMixture.put(&water);
    mochiMixture.put(&caramen);
    mochiMixture.ini();
    mix mousseMixture(2);
    mousseMixture.put(&gelatin);
    mousseMixture.put(&cream);
    mousseMixture.ini();
    //Bánh
    cake pancake("Pancake",1);
    pancake.put(&cakeMixture);
    pancake.ini();
    cake mousse("Mousse", 1);
    mousse.put(&mousseMixture);
    mousse.ini();
    cake cheesecake("Banh pho mai", 2);
    cheesecake.put(&cream);
    cheesecake.put(&creamCheese);
    cheesecake.ini();
    cake tiramisu("Tiramisu", 1);
    tiramisu.put(&cream);
    tiramisu.ini();
    cake cupcake("Cupcake", 1);
    cupcake.put(&cakeMixture);
    cupcake.ini();
    cake muffin("Muffin", 1);
    muffin.put(&cakeMixture);
    muffin.ini();
    cake mochi("Mochi", 1);
    mochi.put(&mochiMixture);
    mochi.ini();
    cake flan("Flan", 2);
    flan.put(&cakeMixture);
    flan.put(&caramen);
    flan.ini();
    cake gato("Gato", 1);
    gato.put(&cakeMixture);
    gato.ini();
    cake donut("Donut", 2);
    donut.put(&powder);
    donut.put(&bakingPowder);
    donut.ini();
    cake bakery[] = { pancake,mousse,cheesecake,tiramisu,cupcake,muffin,mochi,flan,gato,donut };
    for (int i = 0;i < 10;i++)
    {
        bakery[i].print();
        printf("n");
    }
    printf("Cac loai banh mem: n");
    for (int i = 0;i < 10;i++)
    {
        if (bakery[i].getCondenceRate() < 20)
            bakery[i].print();
        printf("n");
    }
    printf("Banh ngot va it beo: n");
    for (int i = 0;i < 10;i++)
    {
        if (bakery[i].getGlutenWeigh()*100/bakery[i].getWeigh() >40 &&
            bakery[i].getFatWeigh() * 100 / bakery[i].getWeigh() <50)
            bakery[i].print();
        printf("n");
    }
    return 0;
}

看来该网站正确地运行了我的代码,但是当我尝试使用VS2015社区时,它会返回"访问侵犯阅读位置",并在第196行中断。我不知道为什么以及如何解决这个问题。我已经阅读了一些类似的问题,这些问题已经在堆栈溢出上发布了,但是找到了解决这个问题的好方法。所以,请节省我的一天!真诚的,谢谢。

p/s:对不起,任何冒犯和语法不正确的事情。

mix_ingredient的构造函数中,您创建了一个Pointers ingre = new ingredient*[n]的数组,但您不使用值初始化数组。

稍后在mix_ingredient::put中,您检查这些值不是NULL。如果ingre中的数组值未初始化,则可能存在任何内容。它不必是NULL,如果用作地址,很可能不会指向您的程序使用的任何地址空间。

mix_ingredient::ini中,您在 inge中使用的值,就像指向对象 ingredient一样,如果地址没有指向有效的对象,这将导致访问违规。