JY One
JY One

Reputation: 70

c++ Dynamic Allocation of 2D Array Class

Very new beginner here and I'm at the end of my rope with this assignment and would really appreciate any help :). Apologies in advance for the length.

I'm having some trouble with retrieving and setting values for my dynamically allocated 2D array. I have a class, defined below, that should construct a 2D array, at which point I need the option to set a value to a given point in the array and also to retrieve a value at a given point.

I ran the debugger, and got as far as to figure out that I have a segmentation fault when the setValue function runs. Can anyone help me understand what I'm doing wrong? As a beginner, the easier terms the better :). Thank you kindly in advance.

#include <iostream>
using namespace std;


class array2D
{
    protected:
        int xRes;
        int yRes;
        float ** xtable;

    public:
        array2D (int xResolution, int yResolution);
        ~array2D() {}
        void getSize(int &xResolution, int &yResolution);
        void setValue(int x,int y,float val);
        float getValue(int x,int y);
};


array2D::array2D(int xResolution, int yResolution)
{
    xRes=xResolution;
    yRes=yResolution;

    float ** xtable = new float*[yResolution];

    for(int i=0;i < yResolution;i++)
    {
        xtable[i] = new float[xResolution];
    }

    for(int i=0;i < yRes;i++)
    {
        for(int j=0;j < xRes;j++)
        {
            xtable[i][j]=45;
        }
    }
}

void array2D::getSize(int &xResolution, int &yResolution)
{
    xResolution=xRes;
    yResolution=yRes;
    cout << "Size of Array: " << xResolution << ", " << yResolution << endl;
}

void array2D::setValue(int x,int y,float val)
{
    xtable[x][y] = val;
}

float array2D::getValue(int x,int y)
{
    return xtable[x][y];
}

int main()
{
    array2D *a = new array2D(320,240);
    int xRes, yRes;
    a->getSize(xRes,yRes);
    for(int i=0;i < yRes;i++)
    {
        for(int j=0;j < xRes;j++)
        {
            a->setValue(i,j,100.0);
        }
    }

    for(int i=0;i < yRes;i++)
    {
        for(int j=0;j < xRes;j++)
        {
            cout << a->getValue(i,j) << " ";
        }

        cout << endl;
    }

    delete[] a;
}

Upvotes: 3

Views: 135

Answers (2)

Satya
Satya

Reputation: 1779

int r,c;
cin>>r>>c;
int** p=new int*[r];
for(int i=0;i<r;i++) {
    p[i]=new int[c];
}
for(int i=0;i<r;i++) {
delete [] p[i]; 
}
delete [] p;

Using this code you can create a 2D Dynamic Array in C++ which would allocate the memory of this array in the Heap Memory.

Upvotes: 0

R Sahu
R Sahu

Reputation: 206567

The line

float ** xtable = new float*[yResolution];

creates a function local variable. The member variable of the class still remains uninitialized. That's not what you want. To allocate memory and assign it to the member variable, remove the type specifier from that line. Just use:

xtable = new float*[yResolution];

Also, you need to switch the use of yResolution and xResolution in those lines. Otherwise, getValue and setValue will be using the indices incorrectly.

Swap the use of xResolution and yResolution in the following lines so that you use:

float ** xtable = new float*[xResolution];
for(int i=0;i < xResolution;i++)
{
    xtable[i] = new float[yResolution];
}

Swap the use of xRes and yRes in the following lines so that you use:

for(int i=0;i < xRes;i++)
{
    for(int j=0;j < yRes;j++)
    {
        xtable[i][j]=45;
    }
}

Since your class acquires resources using dynamic memory allocation, you should read up on The Rule of Three and update your class accordingly.

Upvotes: 6

Related Questions