c++ - 在 C++ 类中创建的二维动态分配数组

标签 c++ arrays dynamic-allocation

<分区>

我在 C++ 和其他面向对象语言方面相对较新(已经完成了一个学期的 C 类(class),现在我正在上 C++ 类(class))。我在与同学一起上课时遇到有关制作动态分配的二维数组的问题。

练习本身是:

Prepare a class named "matrix" capable of storing two-dimensional arrays dynamically allocated (for float variables). Remember that information such as height and width have to be properly stored somewhere, element pointers too.

The class needs to contain constructors that allow the creation of objects using one of the following strategies: The creation of an array consisting of MxN elements, example:

Array A (4, 5);

The creation of an empty array:

Array B;
 The creation of an array that is a copy of another previous one:

Array C (A);  

在尝试找出它不能正常工作的原因一段时间后,我们的代码目前是这样的: obs:“Matriz”是我们语言中对二维数组的称呼。

矩阵.h

#pragma once
class Matriz{
    public:
        int l, c;
        float** matriz;
        void setL(int _l);
        void setC(int _c);
        int getL();
        int getC();
        Matriz();
        Matriz(int _l, int _c);
        Matriz(Matriz& m);
        float **getMatriz();
        float getElement(int pL, int pC);
        void setElement(int pL, int pC, float value);
    };

矩阵.cpp

 #include "Matriz.h"

Matriz::Matriz(){
    l = c = 0;
    matriz = new float*[l];

    for (int i = 0; i<l; i++) {
        matriz[l] = new float[c];
    }
}

Matriz::Matriz(Matriz& m){
    l = m.getL();
    c = m.getC();
    matriz = new float*[l];

    for (int i = 0; i<l; i++) {
        matriz[l] = new float[c];
    }

    for (int i = 0; i<l; i++) {
        for (int j = 0; j<l; j++) {
            matriz[i][j] = m.matriz[i][j];
        }
    }

}

Matriz::Matriz(int _l, int _c){
    l = _l;
    c = _c;

    matriz = new float*[l];

    for (int i = 0; i<l; i++) {
        matriz[l] = new float[c];
    }
}

float **Matriz::getMatriz(){
    return matriz;
}

int Matriz::getC(){
    return c;
}

int Matriz::getL(){
    return l;
}

void Matriz::setC(int _c){
    c = _c;
}
void Matriz::setL(int _l){
    l = _l;
}

float Matriz::getElement(int pL, int pC){
    return matriz[pL][pC];
}

void Matriz::setElement(int pL, int pC, float value){
    matriz[pL][pC] = value;
}

主要.cpp

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    int l = 2, c = 2;
    float **m;
    m = new float*[l];

    for (int i=0; i<2; i++) {
    m[i] = new float[c];
    }

    Matriz a(2, 2);
    a.setC(2);
    a.setL(2);

    cout << " c = " << a.getC() << " l= " << a.getL() << "\n";

    for (int i = 0; i<l; i++) {
        for (int j = 0; j<c; j++) {
            a.setElement(i, j, 0);
            cout << " Elemento " << 1 << " " << 1 << " = " << a.getElement(l, c) << "\n";
        }
    }

    a.setElement(0, 0, 1); // <- this is just for testing



    system("pause");
}

iostream 和类头都包含在 stdafx.h 中

在 MSVS 2013 中编译它会在

中断
void Matriz::setElement(int pL, int pC, float value){
    matriz[pL][pC] = value;
}

我们不太确定为什么,调试器给我 “ConsoleApplication15.exe 中 0x01092E27 处的未处理异常:0xC0000005:访问冲突写入位置 0xCDCDCDD1。”

但是我们怀疑数组中的某些东西是错误的,并且当程序试图将某些东西写入其中的一个元素时,它根本不存在/无法到达,因此无法更改该元素的值特定元素。

我们感谢您提供的任何帮助或建议,可以随意提出改进建议或编码建议,学习新事物总是好的 =)。

最佳答案

我认为你的错误实际上在这里:a.getElement(l, c)lc 是数组的边界,例如2,当你最大的索引只能是 1 时。

另一个严重缺陷 ( pointed out by twsaef ) 是你的构造函数:

for (int i = 0; i<l; i++) {
    matriz[l] = new float[c];
}

应该是

for (int i = 0; i<l; i++) {
    matriz[i] = new float[c];
}

虽然我在这里,但这是多余的:

Matriz a(2, 2);
a.setC(2);
a.setL(2);

因为 Matriz 的构造函数将为您设置 lc

另外,你打算用这个做什么:

float **m;
m = new float*[l];

for (int i=0; i<2; i++) {
m[i] = new float[c];
}

目前它还没有用于任何用途。

然后,作为 PaulMcKenzie pointed out,当 Matriz 实例超出范围时,动态分配的内存会发生什么情况?

作为 Matt McNabb pointed out,如果在调用 setC()setL() 时需要调整 Matriz 实例的大小怎么办?目前,他们只设置成员变量,不对内存做任何事情。

关于c++ - 在 C++ 类中创建的二维动态分配数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29761197/

相关文章:

javascript - 如何在javascript中基于相同的键和值合并两个对象数组?

c++ - 如何在Matlab中正确使用C++ dll中动态分配的内存

c++ - 字符串未在此范围内声明 C++

c++ - 我如何告诉 cppCheck 不要将函数视为分配函数?

c - 数组的单个括号中的嵌套索引

javascript - 我正在尝试使用 css 高度栏通过排序数字更新 DOM

c++ - 如果我不释放/删除动态分配的数组会发生什么?

Calloc 导致段错误

c++ - GCC 无法向量化这个简单的循环 ('number of iterations cannot be computed' ) 却在同一代码中管理了一个类似的循环?

c++ - C/C++ : source file after preprocessing-Diab compiler?