I'm having trouble deleting a char matrix ,
I'm using visual studio and I'm getting an error once the code reaches this line :
delete[] AlloBoard[i];
this is the full code:
#include <iostream>
using namespace std;
char** buildMat(int h, int w)
{
char**mat;
mat = new char*[w];
for (int i = 0; i < w; i++)
mat[i] = new char[h];
return mat;
}
int main()
{
char** AlloBoard=buildMat(4,5);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
AlloBoard[i][j] = 'x';
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
cout << AlloBoard[i][j];
cout << endl;
}
for (int i = 0; i < 5; i++)
delete[] AlloBoard[i];
delete[] AlloBoard;
cout << "DONE" << endl;
}
appreciate the help!
You initially create 5 arrays of 4 chars each, but then you treat it like 4 arrays of 5 chars each. If your intent is to have matrix like this:
xxxxx
xxxxx
xxxxx
xxxxx
You need to change
buildMat(4,5);
to
buildMat(5,4);
And when deleting, do the loop to 4 not 5
for (int i = 0; i < 4; i++)
delete[] AlloBoard[i];
delete[] AlloBoard;
https://ideone.com/4cgCt3
Related
cout<<count<<endl; sould provide an output according to the conditions , but it isn't printing anything, what is the fault/error/defects in the code that are causing such results ?
it is my first question , sorry if i am not completely understandable.
i used the following code , i can't understand whats happening here.this is a simple input output question. the output provides us info about matching two team's uniform.
#include <stdio.h>
#include <iostream>
using namespace std;
main(){
int a;
cin>>a;
int **b;
b=new int*[a];
for (int i = 0; i < a; i++)
{
b[i]=new int[2];
for (int j = 0; j <2 ; j++)
{
cin>>b[i][j];
}
}
int count=0;
for (int i = 0; i < a*(a-1); i++)
{ for (int j = 0; j < a; j++)
if (b[i][0]==b[j][1])
count=count+1;
}
cout<<count<<endl;
for (size_t i = 0; i < a; i++)
{
delete b[i];
}
delete b;
}
input:
3
1 2
2 4
3 4
output does not contain anything
You use the array out of bounds and delete when you should delete[]. Comments in code:
#include <iostream> // use the correct headers
#include <cstddef>
// not recommended: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
using namespace std;
int main() { // main must return int
size_t a; // better type for an array size
cin >> a;
int** b;
b = new int*[a];
for(size_t i = 0; i < a; i++) {
b[i] = new int[2];
for(size_t j = 0; j < 2; j++) {
cin >> b[i][j];
}
}
int count = 0;
std::cout << a * (a - 1) << "\n"; // will print 6 for the given input
for(size_t i = 0; i < a * (a - 1); i++) {
// i will be in the range [0, 5]
for(size_t j = 0; j < a; j++)
if(b[i][0] == b[j][1]) count = count + 1;
// ^ undefined behaviour
}
cout << count << endl;
for(size_t i = 0; i < a; i++) {
delete[] b[i]; // use delete[] when you've used new[]
}
delete[] b; // use delete[] when you've used new[]
}
I am currently working on a program that prints a 5 variable truth table. I am using a 2d array. My code currently produces the table, but says it is corrupt and "the stack around the variable "table" was corrupted. Any help?
#include <iostream>
using namespace std;
int main() {
bool table[5][32];
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}
This is homework, so I would like to understand it, not just have an answer.
The index is wrong. Only table[0] to table[4] are available, so accessing table[5] to table[31] is illegal.
Try this:
#include <iostream>
using namespace std;
int main() {
bool table[32][5]; // swap 32 and 5
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}
There is attempt to read out of bound values from array.
If you need 5x32 matrix Use code below:
for (int i = 0; i < 5; i++) { // 32-> 5
for (int j = 0; j < 32; j++) { // 5->32
If you need 32x5 matrix then replace code below:
bool table[32][5]; //it was table[5][32];
I am getting an heap corruption error when running a code from my textbook.However, when I run it through an online compiler, it works. I am using visual studio 2013. I assume the code is correct; Is it might be something wrong with my visual studio? Any help will be appreciated, thanks!
#include <iostream>
#include<cstdlib>
#include<ctime>
#include<iomanip>
using namespace std;
int main() {
// your code goes here
srand(time(0));
int* counts[10];
// Allocate the rows
for (int i = 0; i < 10; i++)
{
counts[i] = new int[i + 1];
for (int j = 0; j <= 1; j++)
{
counts[i][j] = 0;
}
}
const int RUNS = 1000;
// Simulate 1,000 balls
for (int run = 0; run < RUNS; run++)
{
// Add a ball to the top
counts[0][0]++;
// Have the ball run to the bottom
int j = 0;
for (int i = 1; i < 10; i++)
{
int r = rand() % 2;
// If r is even, move down,
// otherwise to the right
if (r == 1)
{
j++;
}
counts[i][j]++;
}
}
// Print all counts
for (int i = 0; i < 10; i++)
{
for (int j = 0; j <= i; j++)
{
cout << setw(4) << counts[i][j];
}
cout << endl;
}
// Deallocate the rows
for (int i = 0; i < 10; i++)
{
delete[] counts[i];
}
return 0;
}
Here
for (int i = 0; i < 10; i++)
{
counts[i] = new int[i + 1];
for (int j = 0; j <= 1; j++)
{
counts[i][j] = 0;
}
}
for counts[0] you allocate memory for only one int (counts[0] = new int[0+1]). In inner loop you try to access counts[0][1]. Therefore, you go beyond the boundaries of the array and get heap corruption.
I'm currently reading Jumping into C++ by Alex Allain and am stuck on Chapter 14's Practice Problem number 1.
Write a function that builds a two-dimensional multiplication table with arbitrary sizes for two dimensions.
I'm having trouble actually assigning the times table to the array but all these nested loops are giving me a headache! I'm getting an output of "999999999".
My Code:
#include <iostream>
using namespace std;
int main()
{
int **p_p_tictactoe;
p_p_tictactoe = new int*[3];
for (int i = 0; i < 3; i++)
p_p_tictactoe[i] = new int[3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
p_p_tictactoe[i][j] = 1;
for (int y = 0; y < 4; y++)
{
for (int t = 0; t < 4; t++)
{
p_p_tictactoe[i][j] = y * t;
}
}
cout << p_p_tictactoe[i][j];
}
}
cin.get();
for (int i = 0; i < 3; i++)
delete[] p_p_tictactoe[i];
delete[] p_p_tictactoe;
}
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;
}