I'm trying to assign data from one object to another and get me this error and choose an exception:
Unhandled exception at location 0x00007FFDCD79A839 in C ++ Project. Exe: Microsoft C ++ exception: std :: length_error in memory location 0x000000DDD04FA560.
Field.cpp
RectangleShape Field::getField() {
return field;
}
Board.h
#pragma once
#include "DEFINIONS.h"
#include "Field.h"
class Board
{
private:
int length{ 3 };
int width{ 3 };
int lengthBoard{ 0 };
int widthBoard{ 0 };
int a{ 0 };
int b{ 0 };
bool check{ true };
RectangleShape ramka;
Field** board;
public:
Board(int length = 3, int width = 3, int margin_X = 0, int margin_Y = 0);
void drawMap(RenderWindow& window);
};
Board.cpp
Board::Board(int length, int width, int margin_X, int margin_Y) {
this->length = length;
this->width = width;
ramka.setSize(Vector2f(800, 800));
ramka.setFillColor(Color::Green);
ramka.setOrigin(700, 700);
ramka.setPosition(730, 730);
board = new Field * [length];
for (int i = 0; i < length; i++) {
board[i] = new Field[width];
}
for (int i = 0; i < length; i++) {
*this->board[i] = Field();
for (int j = 0; j < width; j++) {
*this->board[j] = Field();
lengthBoard = length * board[0][0].getSize() + (length - 1) * board[0][0].getOdleglosc();
}
}
while (check) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++) {
if (lengthBoard > ramka.getSize().x&& lengthBoard > ramka.getSize().y) {
a = 0.95 * board[i][j].getSize();
b = board[i][j].getOdleglosc();
board[i][j].setSize(a);
board[i][j].setOdleglosc(b);
lengthBoard = length * board[0][j].getSize() + (length - 1) * board[0][j].getOdleglosc();
}
else
{
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++) {
if (lengthBoard < ramka.getSize().x && lengthBoard < ramka.getSize().y) {
a = 1.05 * board[i][j].getSize();
b = board[i][j].getOdleglosc();
board[i][j].setSize(a);
board[i][j].setOdleglosc(b);
lengthBoard = length * board[0][j].getSize() + (length - 1) * board[0][j].getOdleglosc();
}
else {
check = false;
break;
}
}
}
if (!check)
break;
}
}
if (!check)
break;
}
}
for (int i = 0; i < length; i++)
for (int j = 0; j < width; j++)
board[i][j] = Field(i, j, margin_X, margin_Y, a, b);
}
void Board::drawMap(RenderWindow& window) {
// window.draw(ramka);
scaleBoard(window);
for (int i = 0; i < length; i++)
for (int j = 0; j < width; j++) {
window.draw(board[i][j].getField());
std::cout << board[i][j].getSize() << "---" << board[i][j].getOdleglosc() << std::endl;
std::cout << a << "----------------" << b << std::endl;
}
}
GameState.h
Board tablica;
void doIt(RenderWindow& window,int numer); //metoda pozwalajaca zainicjowac całą tablice
int game(RenderWindow& window, Event& event, Vector2f mouse, int numer);
};
GameState.cpp
void GameState::gameload(RenderWindow& window, Event &event, int numer) {
Board board(numer, numer);
tablica.setA(board.getA());
tablica.setB(board.getB());
//tablica.setBoard(board.getBoard());
tablica.setCheck(board.getCheck());
tablica.setLength(board.getLength());
tablica.setLengthBoard(board.getLengthBoard());
tablica.setRamka(board.getRamka());
tablica.setWidth(board.getWidth());
tablica.setWidthBoard(board.getWidthBoard());
loadBackground();
window.draw(background);
board.drawMap(window);
drawPause(window);
setTextureKrzyzyk();
setTextureKolko();
setTextureKolkoWin();
setTextureKrzyzykWin();
}
void GameState::doIt(RenderWindow& window,int numer)
{
loadBackground();
window.draw(background);
tablica.drawMap(window);
drawPause(window);
setTextureKrzyzyk();
setTextureKolko();
setTextureKolkoWin();
setTextureKrzyzykWin();
}
int GameState::game(RenderWindow& window, Event& event, Vector2f mouse, int numer) {
if (checker)
{
gameload(window, event, numer);
checker = false;
}
doIt(window,numer);
return pauseButton(mouse, event, STATE_GAME);
}
Related
i have assigment to create c++ program which will multiple 2 uknown size matrix using fork and shared memory, I have write this code but at the end for multiplication result i get all zeros. Sorry for mess code im new in this.
`
`#include<iostream>
#include<math.h>
#include<vector>
#include<signal.h>
#include<unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<sys/wait.h>
using namespace std;
int l, m, n, job_numb;
key_t id_shared_mem;
int **B, **C, **A;
int **generateMatrix(int **matrix, int iterator, int vel){
for (int i = 0; i < iterator; i++)
{
matrix[i] = new int[vel];
for (int j = 0; j < vel; j++)
{
matrix[i][j] = (rand() % 9);
}
}
return matrix;
}
int **generateMatrixC(int **matrix, int iterator, int vel)
{
for (int i = 0; i < iterator; i++)
{
matrix[i] = new int[vel];
}
return matrix;
}
void *writeMatrixResult(int **matrix, int stupac, int red)
{
for (int i = 0; i < stupac; i++)
{
for (int j = 0; j < red; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
return NULL;
}
void write()
{
cout << "Mnozenik = " << endl;
writeMatrixResult(A, l, m);
cout << "Mnozitelj = " << endl;
writeMatrixResult(B, m, l);
cout << "Umnozak = " << endl;
writeMatrixResult(C, l, l);
}
void abort(int sig) {
if (sig == SIGINT) {
for (int i = 0; i < job_numb; i++) wait(NULL);
shmdt(A);
shmdt(B);
shmctl(id_shared_mem, IPC_RMID, NULL);
} else {
write();
shmdt(A);
shmdt(B);
shmctl(id_shared_mem, IPC_RMID, NULL);
}
exit(0);
}
void proces(int procesId)
{
while (job_numb > procesId)
{
int i = 0;
while (i < l)
{
int j = 0;
C[procesId][i] = 0;
while (m > j)
{
C[procesId][i] = C[procesId][i] + A[procesId][j] * B[j][i];
j++;
}
i++;
}
procesId++;
}
}
int main(int argc, char ** argv) {
if (argc != 4) {
cout << " unesite 3 argumenta!" << endl;
return 0;
}
l = atoi(argv[1]);
m = atoi(argv[2]);
n = atoi(argv[3]);
job_numb = l / n;
if (l % n) {
job_numb++;
}
id_shared_mem = shmget(IPC_PRIVATE, sizeof(int) * 10, 0600);
sigset(SIGINT, abort);
A = (int **)shmat(id_shared_mem, NULL, 0);
B = A + l;
C = B + l * m*m;
A = new int *[l];
B = new int *[m];
C = new int *[l];
srand(time(NULL));
A = generateMatrix(A,l, m );
B = generateMatrix(B,m,l);
C = generateMatrixC(C,l,l);
for (int i = 0; i < n; i++)
{
switch (fork()) {
case 0: { //dijete
proces(i);
exit(0);
}
case -1: { //greska
cout << "Fatalna pogreska!" << endl;
exit(1);
}
}
}
for (int i = 0; i < job_numb; i++) wait(NULL);
abort(0);
}`
`
i have try to assign only one value to matrix C like c[1][1] = 2 in proces function to see if it will write any, but still im getting all zeros on printl.
I have a problem with deallocating a 3 dimensional vector in c++, I get the error "CTR detected that the application wrote to memory after end of heap buffer".
Can somebody tell me what I am doing wrong? Thank you in advance.
Allocation:
count = new int**[w];
for (int i = 0; i < w; ++i)
{
count[i] = new int*[h];
for (int j = 0; j < h; ++j)
{
count[i][j] = new int[120];
for (int k = 20; k < 120; ++k)
{
count[i][j][k] = 0;
}
}
}
Deallocation:
for (int i = 0; i < w; ++i)
{
for (int j = 0; j < h; ++j)
{
delete [] count[i][j];
}
delete [] count[i];
}
delete [] count;
count = NULL;
This is my entire code:
#include <filesystem>
#include <stdexcept>
#include "pch.h"
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <math.h>
using namespace std;
using namespace cv;
class cerc {
int raza;
int a;
int b;
public:
cerc()
{
a = 0;
b = 0;
raza = 0;
}
inline cerc(int raza, int a, int b)
{
this->a = a;
this->b = b;
this->raza = raza;
}
inline cerc(cerc& c)
{
a = c.a;
b = c.b;
raza = c.raza;
}
void print()
{
std::cout << " (" << (int)this->a << ", " << (int)this->b << ", " << (int)this->raza << ") ";
}
inline bool operator==(const cerc& other) const
{
if (this->a == other.a && this->b == other.b && this->raza == other.raza)
return true;
else return false;
}
inline bool egal(int raza, int a, int b) const
{
if (this->a == a && this->b == b && this->raza == raza)
return true;
else return false;
}
inline void copy(cerc &c2)
{
this->raza = c2.raza;
this->a = c2.a;
this->b = c2.b;
}
inline void copy(int raza, int a, int b)
{
this->a = a;
this->raza = raza;
this->b = b;
}
const int getA()
{
return a;
}
const int getB()
{
return b;
}
const int getRaza()
{
return raza;
}
};
int min(int a, int b)
{
if (a < b)
return a;
return b;
}
void createVector(int*** count, Mat img, int w, int h)
{
int a = 0;
int b = 0;
int r = 0;
int wminus = 5 * (w / 6);
int hminus = 5 * (h / 6);
int wstart = w / 6;
int hstart = h / 6;
int x, y;
for (x = wstart; x < wminus; x++)
for (y = hstart; y < hminus; y++)
{
if (((Scalar)(img.at<uchar>(Point(x, y)))).val[0] == 255)
{
for (a = wstart; a < wminus; a += 1)
for (b = hstart; b < hminus; b += 1)
{
r = (int)sqrt((b - y)*(b - y) + (a - x)*(a - x));
if (r >= 20 && r <= 120)
count[a][b][r]++;
}
}
}
}
void cercFinal(cerc &cercMax, int***count, int w, int h)
{
int wminus = 5 * (w / 6);
int hminus = 5 * (h / 6);
int wstart = w / 6;
int hstart = h / 6;
int maxNr = count[0][0][0];
for (int i = wstart; i < wminus; i += 1)
for (int j = hstart; j < hminus; j += 1)
for (int k = 20; k < 60; k++)
if (maxNr < count[i][j][k])
{
maxNr = count[i][j][k];
cercMax.copy(k, i, j);
}
cout << maxNr;
}
void cercFinalSecund(cerc &SecondCircle, cerc &FirstCircle, int***count, int w, int h)
{
int wminus = 3 * (w / 4);
int hminus = 3 * (h / 4);
int wstart = w / 4;
int hstart = h / 4;
int minIrisRay = (int)(FirstCircle.getRaza() * 1.5);
int maxIrisRay = FirstCircle.getRaza() * 4;
int maxNr = count[0][0][0];
for (int i = wstart; i < wminus; i += 1)
for (int j = hstart; j < hminus; j += 1)
for (int k = 40; k < 120; k++)
{
if (k >= minIrisRay && k <= maxIrisRay)
{
if (maxNr < count[i][j][k] && !(cerc(k, i, j) == FirstCircle) && abs(j - FirstCircle.getB()) < 2 && abs(i - FirstCircle.getA()) < 2)
{
maxNr = count[i][j][k];
SecondCircle.copy(k, i, j);
}
}
}
}
int main()
{
Mat imgCanny;
Mat imgBlur;
int ***count;
for (int i = 10; i < 50; i++)
{
Mat_<uchar> img = imread("C://Users//Maria//Downloads//CASIA-IrisV2//CASIA-IrisV2//device1//00" + std::to_string(i) + "//00" + std::to_string(i) + "_000.bmp", IMREAD_GRAYSCALE);
//img.convertTo(img, -1, 4, 0);
//int t = img.at<uchar>(1, 2).val[0];
int w = img.size().width;
int h = img.size().height;
medianBlur(img, imgBlur, 15);
//GaussianBlur(img, imgBlur, Size(8, 8), 0);
Canny(imgBlur, imgCanny, 30, 40);
//imshow("canny", imgCanny);
int m = min(w, h);
count = new int**[w];
for (int i = 0; i < w; ++i)
{
count[i] = new int*[h];
for (int j = 0; j < h; ++j)
{
count[i][j] = new int[120];
for (int k = 20; k < 120; ++k)
{
count[i][j][k] = 0;
}
}
}
createVector(count, imgCanny, w, h);
cerc final(0, 0, 0);
cerc finalSecund(0, 0, 0);
cercFinal(final, count, w, h);
cercFinalSecund(finalSecund, final, count, w, h);
cout << endl << "cerc=";
final.print();
Point p(final.getA(), final.getB());
Mat imgColor = imread("C://Users//Maria//Downloads//CASIA-IrisV2//CASIA-IrisV2//device1//00" + std::to_string(i) + "//00" + std::to_string(i) + "_000.bmp", IMREAD_COLOR);
circle(imgColor, p, final.getRaza(), Scalar(255, 0, 0), 4, 8, 0);
Point ps(finalSecund.getA(), finalSecund.getB());
cout << endl << "cerc=";
finalSecund.print();
circle(imgColor, ps, finalSecund.getRaza(), Scalar(255, 0, 0), 4, 8, 0);
imshow("iris", imgColor);
for (int i = 0; i < w; ++i)
{
for (int j = 0; j < h; ++j)
{
delete [] count[i][j];
}
delete [] count[i];
}
delete [] count;
count = NULL;
waitKey(0);
}
return 0;
}
If I delete the line count[a][b][r]++; the deallocation works fine. Is anything wrong with this statement?
I am trying to create a simulation of conway's game of life.
I keep getting a "vector subscript out of range" error after about 300 generation and I don't understand the reason. From what I could gather it's caused by using an invalid index. The most likely section is the first part of the draw function where I find empty rows and replace them with "\n" to save time.
I've started learning to code not too long ago so I may be making baby mistakes.
Edit: visual studio point the error after the third for loop in the frame function, on if (emptyRows[m] == i)
Here's the full code :
#include <array>
#include <time.h>
#include <vector>
const int WIDTH = 150;
const int HEIGHT = 50;
bool table[WIDTH][HEIGHT];
bool tableNew[WIDTH][HEIGHT];
std::string buffer;
int total;
int counter = 0;
std::vector<int> emptyRows;
int numberOfNeighbours(int Y, int X) {
if (X == 0 || Y == 0 || X == WIDTH || Y == HEIGHT)
return 2;
total = 0;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (table[X + j][Y + i] == true)
total++;
}
}
total -= table[X][Y];
return total;
}
void draw() {
srand((int)time(0));
int m = 0;
bool check = 0;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (table[j][i] == 1)
check = 1;
}
if (check == 0)
emptyRows.push_back(i);
else
check = 0;
}
for (int i = 0; i < HEIGHT; i++) {
if (emptyRows.size() >= 1) {
if (emptyRows[m] == i) {
buffer.append("\n");
m++;
continue;
}
}
for (int j = 0; j < WIDTH; j++) {
if (table[j][i] == 1) buffer.push_back('#');
else buffer.push_back(' ');
}
buffer.append("\n");
}
std::cout << buffer;
std::cout << std::endl << "Generazione numero:" << counter;
emptyRows.erase(emptyRows.begin(), emptyRows.end());
buffer.erase(buffer.begin(), buffer.end());
m = 0;
}
void reset() {
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
table[j][i] = tableNew[j][i];
}
}
}
void logic() {
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
int k = numberOfNeighbours(i, j);
if (table[j][i] == 0 && k == 3)
tableNew[j][i] = 1;
else if (table[j][i] == 1 && k != 2 && k != 3)
tableNew[j][i] = 0;
else
tableNew[j][i] = table[j][i];
}
}
}
int main(){
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if ((rand() % 2) == 1)
table[j][i] = 1;
}
}
while (true) {
counter++;
draw();
logic();
reset();
system("cls");
}
return 0;
}
I have a function "mod_kadane" defined in class "MAX_SUB_MAT" which returns an object.
#define dx 101
class MAX_SUB_MAT
{
public:
int curr_sum, max_sum, left, right, up, down;
MAX_SUB_MAT mod_kadane(int mat[dx][dx], int row, int column)
{
MAX_SUB_MAT objx;
curr_sum = objx.max_sum = INT_MIN;
int sub_mat[row];
for(int L = 0; L < row; L++)
{
memset(sub_mat, 0, sizeof sub_mat);
for(int R = L; R < column; R++)
{
for(int i = 0; i < row; i++)
{
sub_mat[i] += i;//mat[i][R];
}
MAX_SUB_ARR obj;
obj = obj.kadane(sub_mat, row);
curr_sum = obj.maxima;
if(curr_sum > objx.max_sum)
{
objx.max_sum = curr_sum;
objx.left = L;
objx.right = R;
objx.up = obj.left;
objx.down = obj.right;
}
}
}
return objx;
}
};
But when I'm trying to call it from "main":
cin >> row >> column;
int mat[row][column];
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
cin >> mat[i][j];
}
}
MAX_SUB_MAT objx;
objx = objx.mod_kadane(mat, row, column);
then an error is shown:
error: no matching function for call to 'MAX_SUB_MAT::mod_kadane(int [row][column], int&, int&)'|
But if I remove the 2d array "mat" from both sides then the code works fine.
Here is my full code:
#include<bits/stdc++.h>
#define dx 101
using namespace std;
class MAX_SUB_ARR
{
public:
int max_curr, maxima, left, right;
MAX_SUB_ARR kadane(int arr[], int n)
{
MAX_SUB_ARR obj;
obj.maxima = max_curr = INT_MIN;
//cout << obj.maxima << endl;
/*for(int i = 0; i < n; i++)
cout << arr[i] << endl;*/
for(int i = 0; i < n; i++)
{
if(max_curr < 0)
{
max_curr = arr[i];
obj.left = i;
}
else
{
max_curr += arr[i];
}
//maxima = max(maxima, max_curr);
if(max_curr > obj.maxima)
{
obj.maxima = max_curr;
obj.right = i;
//cout << obj.maxima << endl;
}
}
return obj;
}
};
class MAX_SUB_MAT
{
public:
int curr_sum, max_sum, left, right, up, down;
/* MAX_SUB_MAT(int r, int c)
{
row = r;
column = c;
}*/
MAX_SUB_MAT mod_kadane(int mat[dx][dx], int row, int column)
{
MAX_SUB_MAT objx;
curr_sum = objx.max_sum = INT_MIN;
int sub_mat[row];
for(int L = 0; L < row; L++)
{
memset(sub_mat, 0, sizeof sub_mat);
for(int R = L; R < column; R++)
{
for(int i = 0; i < row; i++)
{
sub_mat[i] += i;//mat[i][R];
}
MAX_SUB_ARR obj;
obj = obj.kadane(sub_mat, row);
curr_sum = obj.maxima;
if(curr_sum > objx.max_sum)
{
objx.max_sum = curr_sum;
objx.left = L;
objx.right = R;
objx.up = obj.left;
objx.down = obj.right;
}
}
}
return objx;
}
};
int main()
{
int row, column;
printf("Number of rows you want to insert?:\n");
cin >> row;
printf("Number of columns you want to insert?:\n");
cin >> column;
int mat[row][column];
if(row && column)
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
cin >> mat[i][j];
}
}
//int curr_sum, max_sum, left, right, up, down;
//MAX_SUB_MAT objx = new MAX_SUB_MAT(row, column);
MAX_SUB_MAT objx;
objx = objx.mod_kadane(mat, row, column);
for(int i = objx.up; i <= objx.down; i++)
{
for(int j = objx.left; j <= objx.right; j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}
return 0;
}
It is not true that in C or C++ we cannot declare a function with two dimensional array parameter with both sizes specified.
See: http://c-faq.com/aryptr/pass2dary.html
The problem has been correctly explained by #AlanStokes in his comments!
I wrote a function that reads an Image (2-d array) from a file and returns it.
Calling it gave me a Block-Type error, which I could suppress by removing the destructor.
Where is my error? Do I even need a destructor?
#include <vector>
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <fstream>
using namespace std;
struct Pixel {
float p;
};
class Image {
int _x, _y;
Pixel *pix;
public:
Image(int x, int y) {
pix = new Pixel[x * y];
_x = x;
_y = y;
}
~Image() {
delete[] pix;
pix = NULL;
}
Image& operator= (const Image& param) {
_x = param._x;
_y = param._y;
pix = new Pixel[_x*_y];
pix = param.pix;
return *this;
}
float getPixel(int i, int j) {
return pix[(i-1) * _y + (j-1)].p;
}
void setPixel(int i,int j, Pixel val) {
pix[(i-1) * _y + j-1].p = val.p;
}
Pixel **GetData() {
Pixel **a=0;
a = new Pixel*[_x];
for (int i = 0; i < _x; i++) {
a[i] = new Pixel[_y];
for (int j = 0; j < _y; j++)
a[i][j] = pix[i*_y + j];
}
return a;
}
};
Image ReadFromFile(string a) {
FILE * pFile;
const char * c = a.c_str();
Pixel pix,**tval;
int n,m;
pFile=fopen(c,"r");
fscanf(pFile, "%i %i", &n, &m);
Image img(n, m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
fscanf(pFile, "%f", &pix.p);
img.setPixel(i, j, pix);
}
fclose(pFile);
return img;
}
int main(){
// Example Usage
Image img(10, 10);
Image img2(3, 3);
Pixel val[10][10], **tval;
for (int row = 1; row <= 10; row++)
for (int col = 1; col <= 10; col++) {
val[row-1][col-1].p = 2*row + col;
img.setPixel(row, col, val[row-1][col-1]);
}
for (int row = 1; row <= 10; row++) {
for (int col = 1; col <= 10; col++)
cout << img.getPixel(row,col) << ' ';
cout << '\n';
}
cout << '\n';
tval = img.GetData();
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 10; col++)
cout << tval[row][col].p << ' ';
cout << '\n';
}
string a;
a = "C:/programs/misha/out.txt";
img2 = ReadFromFile(a);
cout << '\n';
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++)
cout << img2.getPixel(row+1, col+1) << ' ';
cout << '\n';
}
}