分配动态矩阵有什么问题?

时间:2016-11-03 19:21:32

标签: c++ dynamic

以下是代码。当我试图调试这个程序时,我得到了信息:

Unhandled exception at 0x009E1BBC in MES1.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.

调试器为这一行加下划线:

function.tab[0][0] = 4;

Source.cpp:

#include <iostream>
#include <conio.h>
#include "Function.h"
using namespace std;

int main()
{
    Function function;
    function.k = 4;
    function.tab[0][0] = 4;
    cout << function.k;
    system("pause");
    return 0;
}

Function.cpp:

#include "Function.h"

void Function::Allocate(float **tab, float*tabW)
{
    tab = new float *[3];
    for (int i = 0; i < 3; ++i)
    {
        tab[i] = new float[3];
    }
    tabW = new float[4];
}

Function::Function()
{
    k = 50;
    alfa = 10;
    S = 2;
    L = 5;
    L1 = 2.5;
    L2 = 2.5;
    q = -150;
    t = 400;

    Allocate(&*tab,&*tabW);
}

Function::~Function()
{
    delete[] tab;
    tab = nullptr;
}

Function.h:

#ifndef Function_h
#define Function_h

class Function
{
public:
    float** tab;
    float* tabW;
    float k;
    float alfa;
    float S;
    float L;
    float L1;
    float L2;
    float q;
    float t;

    float t1;
    float t2;
    float t3;

    float C;

    Function();
    ~Function();
    void Allocate(float **tab, float*tabW);
};

#endif

0 个答案:

没有答案