I make a 3d dynamic array by this using this code
//layer = 2
//levelSize.x = 100
//levelSize.y = 100
level_array = new int**[layer];
for(int i = 0; i < layer; ++i)
{
level_array[i] = new int*[(int)levelSize.x];
for(int j = 0; j < levelSize.x; ++j)
level_array[i][j] = new int[(int)levelSize.y];
}
but when I want to delete it, the program crashes
for(int i = 0; i != levelSize.x; ++i)
{
for(int j = 0; j != levelSize.y; ++j)
{
delete[] level_array[i][j];
}
delete[] level_array[i];
}
delete[] level_array;
I don't know where is wrong in the code of deleting an array.
Please help me check the code, Thanks
You allocate memory for array with dimensions [layer][levelSize.x][levelSize.y], but while deleting you operate with it like with array with dimensions [levelSize.x][levelSize.y][somenting].
for(int i = 0; i != layer; ++i)
// ^^^^^ not levelSize.x
{
for(int j = 0; j != levelSize.x; ++j)
// ^^^^^^^^^^^ not levelSize.y
{
delete[] level_array[i][j];
}
delete[] level_array[i];
}
delete[] level_array;
Add this command to the end, otherwise the pointer will be at the top of the stack.
level_array = nullptr;
it's so important to make difference between deleting a single dim array and multi-dim allocated on the heap:
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int*** ptrInt = new int**[3];
for(int i(0); i < 3; i++)
ptrInt[i] = new int*[3];
for(int i = 0; i < 3; i++)
{
for(int j(0); j < 3; j++)
ptrInt[i][j] = new int[3];
}
for(int i = 0; i < 3; i++)
{
for(int j(0); j < 3; j++)
for(int k(0); k < 3; k++)
ptrInt[i][j][k] = k;
}
for(int i = 0; i < 3; i++)
{
for(int j(0); j < 3; j++)
for(int k(0); k < 3; k++)
cout << "ptrInt[" << i << "][" << j << "][" << k << "]: " << ptrInt[i][j][k] << endl;
}
// now freeing memory:
for(int i = 0; i < 3; i++)
{
for(int j(0); j < 3; j++)
delete[] ptrInt[i][j];
delete[] ptrInt[i];
}
delete[] ptrInt;
cout << endl << endl << endl;
return 0;
}
Related
I am trying to write a function in C++ that multiplies two matrices A, B which have been dynamically allocated. I am currently trying to get the multiplication code working, then I will attempt to turn it into a function. Right now I am getting an error of sorts; "segmentation fault (core dumped)". I have narrowed it down to the multiplication part of my code but I have no idea what is wrong with it. Can someone help me please? My code is shown below.
#include <iostream>
#include <cassert>
int main()
{
int rowsA = 5; // number of rows
int colsA= 3; // number of coloumns
// dynamically allocating A
double** A;
A = new double* [rowsA];
A[0] = new double [rowsA*colsA];
for (int i = 1; i < rowsA; i++)
{
A[i] = A[i-1] + colsA;
}
// Storing elements of matrix A
for(int i = 0; i < rowsA; ++i)
{
for(int j = 0; j < colsA; ++j)
{
std::cout << "Enter element A" << i + 1 << j + 1 << " : ";
std::cin >> A[i][j];
}
}
int rowsB = 3; // number of rows
int colsB = 5; // number of coloumns
// dynamically allocating B
double** B;
B = new double* [rowsB];
B[0] = new double [rowsB*colsB];
for (int i = 1; i < rowsB; i++)
{
B[i] = B[i-1] + colsB;
}
// Storing elements of matrix B
for(int i = 0; i < rowsB; ++i)
{
for(int j = 0; j < colsB; ++j)
{
std::cout << "Enter element B" << i + 1 << j + 1 << " : ";
std::cin >> B[i][j];
}
}
// checking matrix multiplication qualification
assert(colsA == rowsB);
// dynamically allocating C
double** C;
C = new double* [rowsA];
C[0] = new double [rowsA*colsB];
for (int i = 1; i < rowsA; i++)
{
C[i] = C[i-1] + colsB;
}
// Initializing elements of matrix C to 0
for(int i = 0; i < rowsA; ++i)
{
for(int j = 0; j < colsB; ++j)
{
C[i][j]=0;
}
}
// multiplication
for(int i = 0; i < rowsA; ++i)
{
for(int j = 0; j < colsB; ++j)
{
for(int k = 0; k < colsB; ++k)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
// Displaying the multiplication of matrices A, B
std::cout<< "Matrix C: " << std::endl;
for(int i = 0; i < rowsA; ++i)
{
for(int j = 0; j < colsB; ++j)
{
std::cout << " " << C[i][j];
if(j == colsB-1)
{
std::cout << std::endl;
}
}
}
// deallocation
delete[] C[0];
delete[] C;
delete[] B[0];
delete[] B;
delete[] A[0];
delete[] A;
}
First, you say you are allocating the matrix properly but I'm not seeing any proof of that. You are allocating an array of pointers and only initializing the first index.. A[0] for example. This is incorrect. You need to allocate EACH ROW.
You have:
double** A;
A = new double* [rowsA];
A[0] = new double [rowsA*colsA]; //Incorrect. You only allocated A[0].
You need to allocate A[0] through A[rowsA - 1]..
Next, your multiplication loop (the inner most loop) is incorrect. It should be:
Iterate ColsA for that inner loop.
You have:
for(int i = 0; i < rowsA; ++i)
{
for(int j = 0; j < colsB; ++j)
{
for(int k = 0; k < colsB; ++k) //ColsB is incorrect. Should be colsA..
{
C[i][j] += A[i][k] * B[k][j]; //Segfaults here due to bad iteration..
}
}
}
The following would be correct:
#include <iostream>
#include <cassert>
double** create_matrix(int rows, int cols)
{
double** mat = new double* [rows]; //Allocate rows.
for (int i = 0; i < rows; ++i)
{
mat[i] = new double[cols](); //Allocate each row and zero initialize..
}
return mat;
}
void destroy_matrix(double** &mat, int rows)
{
if (mat)
{
for (int i = 0; i < rows; ++i)
{
delete[] mat[i]; //delete each row..
}
delete[] mat; //delete the rows..
mat = nullptr;
}
}
int main()
{
int rowsA = 5; // number of rows
int colsA= 3; // number of coloumns
double** matA = create_matrix(rowsA, colsA);
int rowsB = 3; // number of rows
int colsB = 5; // number of coloumns
double** matB = create_matrix(rowsB, colsB);
//Checking matrix multiplication qualification
assert(colsA == rowsB);
double** matC = create_matrix(rowsA, colsB);
//Multiplication
for(int i = 0; i < rowsA; ++i)
{
for(int j = 0; j < colsB; ++j)
{
for(int k = 0; k < colsA; ++k) //ColsA..
{
matC[i][j] += matA[i][k] * matB[k][j];
}
}
}
//Clean up..
destroy_matrix(matA, rowsA);
destroy_matrix(matB, rowsB);
destroy_matrix(matC, rowsA);
}
Hey I am beginner at C++ programming. I have made a program that is meant to add two 2D arrays together. However, The program outputs the values until the program crashes. Can someone help me to identify the problem?
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int a[10][10], c[10][10], i, j;
for (i = 1; i <= 10; ++i)
{
for(j=0; j < 10; ++j)
{
a[i][j] = i * j;
}
}
// We are able to treat the individual columns as arrays
for (int i = 0; i < 10; ++i)
{
int *b = a[i];
for (int j = 0; j < 10; ++j)
{
cout << b[j] << " ";
}
cout << endl;
}
cout << "****" << endl;
// Declare a multidimensional array on the heap
int **b = new int*[10];
// need to allocate all members individually
for (int i = 0; i < 10; ++i)
{
b[i] = new int[10];
}
// Set the values of b
for (int i = 0; i < 10; ++i)
{
for (j = 0; j < 10; ++j)
{
b[i][j] = (i * 10) + j;
}
}
for (i = 0; i < 10; ++i)
{
for (j = 1; j <= 10; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
}
for (i = 0; i < 10; ++i)
{
for (j = 1; j <= 10; ++j)
{
cout << c[i][j] << endl;
}
}
// Delete the multidimensional array - have to delete each part
for (int i = 0; i < 10; ++i)
{
delete[] b[i];
}
delete[] b;
return 0;
}
I corrected your code.Now, It's working and program didn't crash. You can try it out.
#include<conio.h>
#include<iostream.h>
int main(int argc, char** argv)
{
int a[10][10], c[10][10], i, j;
for (i = 0; i <10; ++i)
{
for(j=0; j < 10; ++j)
{
a[i][j] = i * j;
}
}
//We are able to treat the individual columns as arrays;
for (i = 0; i < 10; ++i)
{
int *b = a[i];
for (int j = 0; j < 10; ++j)
{
cout << b[j] << " ";
}
cout << endl;
}
cout << "****" << endl;
//Declare a multidimensional array on the heap;
int **b = new int*[10];
//need to allocate all members individually
for (i = 0; i < 10; ++i)
{
b[i] = new int[10];
}
//Set the values of b
for ( i = 0; i < 10; ++i)
{
for (j = 0; j < 10; ++j)
{
b[i][j] = (i * 10) + j;
}
}
for (i = 0; i < 10; ++i)
{
for (j = 0; j <10; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
}
for (i = 0; i < 10; ++i)
{
for (j = 0;j < 10; ++j)
{
cout << c[i][j] << " ";
}
cout<<endl;
}
// Delete the multidimensional array - have to delete each part
for (i = 0; i < 10; ++i)
{
delete[] b[i];
}
delete[] b;
return 0;
}
I need to implement a 5x5 dynamic array where
every element in it is equal to the sum of its two indices. For example, the first element, at (0,0), has the value 0+0=0.
Here is my code:
# include<iostream>
using namespace std;
int main()
{
int size =5;
int *array=new int[size];
for (int i = 0; i < size; i++)
delete [] array;
return 0;
}
I need help to implement sum of index.
You need at first to implement a two-dimensional array.:)
Here you are.
#include <iostream>
int main()
{
const size_t N = 5;
int ( *array )[N] = new int[N][N];
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ ) array[i][j] = i + j;
}
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ ) std::cout << array[i][j] << ' ';
std::cout << std::endl;
}
delete [] array;
return 0;
}
And do not pay attention that the answer is down voted. There is nothing wrong with the answer. :)
Firstly, you should create a 2d-array, not just a array.
void foo() {
int **a = new int*[5];
for (int i = 0; i < 5; i++)
a[i] = new int[5];
}
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
a[i][j] = i + j;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++)
cout << a[i][j] << " ";
cout << endl;
}
for (int i = 0; i < 5; i++)
delete[] a[i];
delete[] a;
}
And, of course, don't forget to clear your memory)
I am trying to create a 2d array pointer with my own class, Tile, as type. I have looked at the code example at How do I declare a 2d array in C++ using new?. The following code works perfectly:
int** ary = new int*[sizeX];
for(int i = 0; i < sizeX; ++i)
ary[i] = new int[sizeY];
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
ary[i][j] = 5;
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
cout << ary[i][j];
However when I try to change type from int to my own class, Tile, I get an
No viable overloaded '='
error in XCode, and I can't figure out what this means. I use the following code:
Tile** t;
t = new Tile*[8];
for(int i = 0; i < 8; ++i)
t[i] = new Tile[8];
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
t[i][j] = new Tile(new NoPiece());
}
}
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
cout << (t[i][j].get_piece()).to_string();
}
}
Here is the code for Tile.cpp:
#include "Tile.h"
Tile::Tile() {
}
Tile::Tile(Piece p) {
piece = &p;
}
Piece Tile::get_piece() {
return *piece;
}
And the code for Tile.h:
#include <iostream>
#include "Piece.h"
class Tile {
Piece * piece;
public:
Tile();
Tile(Piece p);
Piece get_piece();
};
The difference between two code snippets is that the one using int treats array elements like values, i.e. assigns
ary[i][j] = 5;
while the one using Tile treats array elements like pointers:
t[i][j] = new Tile(new NoPiece()); // new makes a pointer to Tile
Change the assignment to one without new to fix the problem:
t[i][j] = Tile(new NoPiece());
There is nothing wrong to making a 2D array of pointers, too - all you need is to declare it as a "triple pointer", and add an extra level of indirection:
Tile*** t;
t = new Tile**[8];
for(int i = 0; i < 8; ++i)
t[i] = new Tile*[8];
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
t[i][j] = new Tile(new NoPiece());
}
}
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
cout << (t[i][j]->get_piece()).to_string();
}
}
// Don't forget to free the tiles and the array
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
delete t[i][j];
}
delete[] t[i];
}
int main (void)
{
int** arr = new int*[4];
for (int i = 0; i < 4; i++) arr[i] = new int[4] {1, 0, 0, 1};
const int* p = &(arr[0][0]);
TFigure* test = new TFigure(arr, 4, 4);
test->resolve();
for (int i = 0; i < 4; i++) delete[] arr[i];
delete[] arr;
return 0;
}
where constructor declaration is
line 57:
TFigure(int **ia, int n, int m)
N = n;
M =m;
landscape = new int*[n];
puddles = new int*[n];
for (int i = 0; i < n; i++){
landscape[i] = new int[m];
puddles[i] = new int[n];
for (int j = 0; j < m; j++)
landscape[i][j] = *ia[i][j];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < 0; j++)
if (i == 0 || i == N || j == 0 || j == M)
puddles[i][j] = 0;
else
puddles[i][j] = 1;
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++)
std::cout << puddles[i][j] << ' ';
std::cout << std::endl;
}
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++)
std::cout << landscape[i][j] << ' ';
std::cout << std::endl;
}
};
but I have an error
57:43: error: invalid type argument of unary «*» (have «int»)
I don't understand what causes this.
The problem is with this line:
landscape[i][j] = *ia[i][j];
ia[i][j] gives you an int which you then try to dereference. It seems like you really just want:
landscape[i][j] = ia[i][j];
I'm not sure if this was a mistake when copy and pasting or not, but your constructor definition is missing an opening {.
TFigure(int **ia, int n, int m) {
// Here ^