Invalid allocation size: 4294967295 byte - c++

I was coding about ullman algorithm and when I run my program I faced with :
"Invalid allocation size: 4294967295 byte" error. it could be about vector? or anything else? could any help me about this?
void ullman(Graph &graph,Pattern pattern,int **p,int k)
{
bool flg=true;
if(k>=pattern.vertexNum)
{
int **tmp;
tmp=new int *[pattern.vertexNum];
for(int i=0;i<pattern.vertexNum;i++)
tmp[i]=new int [graph.vertexNum];
for(int i=0;i<pattern.vertexNum;i++)
for(int j=0;j<graph.vertexNum;j++)
tmp[i][j]=p[i][j];
graph.permutation.push_back(tmp);
return;
}
for(int i=0;i<graph.vertexNum;i++)
{
for(int j=0;j<pattern.vertexNum;j++)
if(p[j][i])
flg=false;
if(!flg)
{
flg=true;
continue;
}
p[k][i]=1;
if(examin(graph,pattern,p,k))
ullman(graph,pattern,p,k+1);
p[k][i]=0;
}
return;}
bool examin(Graph &graph,Pattern pattern,int **p,int k)
{
bool flg=true;
int **pt;
pt=new int *[graph.vertexNum];
for(int i=0;i<graph.vertexNum;i++)
pt[i]=new int [pattern.vertexNum];
for(int i=0;i<pattern.vertexNum;i++)
for(int j=0;j<graph.vertexNum;j++)
pt[j][i]=p[i][j];
char **tmp; // P*graph
char **tmp2; // tmp*pt
tmp= new char *[pattern.vertexNum];
for(int i=0;i<pattern.vertexNum;i++)
tmp[i]=new char[graph.vertexNum];
for(int i=0;i<pattern.vertexNum;i++)
for(int j=0;j<graph.vertexNum;j++)
tmp[i][j]='-';
tmp2=new char *[pattern.vertexNum];
for(int i=0;i<pattern.vertexNum;i++)
tmp2[i]=new char[pattern.vertexNum];
for(int i=0;i<pattern.vertexNum;i++)
for(int j=0;j<pattern.vertexNum;j++)
tmp2[i][j]='-';
for(int j=0;j<pattern.vertexNum;j++)
for(int i=0;i<graph.vertexNum;i++)
if(p[j][i])
for(int m=0;m<graph.vertexNum;m++)
tmp[j][m]=graph.G[i][m];
for(int m=0;m<pattern.vertexNum;m++)
for(int i=0;i<graph.vertexNum;i++)
if(pt[i][m])
for(int j=0;j<pattern.vertexNum;j++)
tmp2[j][m]=tmp[j][i];
for(int i=0;i<pattern.vertexNum;i++)
{
for(int j=0;j<pattern.vertexNum;j++)
if(pattern.P[i][j]!='-' && tmp2[i][j]!='-')
if(pattern.P[i][j] != tmp2[i][j])
{
flg=false;
break;
}
if(!flg)
break;
}
if(flg)
return true;
else
return false;}

It looks like you are passing -1 for the size, because 4294967295 corresponds to 0xFFFFFFFF, i.e. the negative one in two's complement representation.
Since the only value that you pass to new [...] is vertexNum, that's the value that you need to check. Add a condition at the top of your functions to see if graph.vertexNum or pattern.vertexNum is negative, set a breakpoint inside the conditional, and see what part of your code is making the invalid call:
void ullman(Graph &graph,Pattern pattern,int **p,int k) {
if(pattern.vertexNum < 0) {
cerr << "pattern.vertexNum is negative" << endl; // Set brekpoint here
}
bool flg=true;
... // The rest of your code
}

Related

process exited due to signal 6/11 c++

I didn't have any problems with compiling my program, but I have a big one when I try to use a bot to check output data. It says: "process exited due to signal 6" or "process exited due to signal 11". I thought it was a matter of dynamic arrays (i have two in this program) but both are deleted at the end.
I would be very grateful for any tip!
#include <iostream>
using namespace std;
void input_data (int p_n, int **p_arr);
void output_data (int p_n, int **p_arr);
void check_salary(int p_n, int **p_arr);
int main()
{
int n;
cin >> n;
int** arr = new int*[n];
for(int i = 0; i < n; ++i){
arr[i] = new int[2];}
input_data(n, arr);
check_salary(n, arr);
output_data(n, arr);
for(int i=0; i<n; i++) {
delete [] arr[i];
}
delete[] arr;
return 0;
}
void check_salary(int p_n, int **p_arr)
{
bool fit, flag, exist;
int *used = new int(p_n);
int boss, salary, i_temp, i_salarless, min_sal ,i_used = 0,boss_salary, id;
for (int i=0; i<p_n; i++)
{
if(p_arr[i][1] != 0)
{
used[i_used]=p_arr[i][1];
i_used++;
}
}
do{
flag = false;
exist=false;
for (int i=0; i<p_n; i++)
{
i_salarless = 0;
id = i+1;
boss = p_arr[i][0];
salary=p_arr[i][1];
boss_salary = p_arr[boss-1][1];
if(salary==0)
{
if(boss==id){
p_arr[i][1] = p_n;
flag = true;
}else if(boss_salary>0){
for (int j=0; j<p_n; j++)
{
if (p_arr[j][0]==boss && p_arr[j][1] == 0){i_salarless++;}
}
if (i_salarless == 1){
do{
--boss_salary;
exist = false;
for (int i=0; i< i_used; i++){
{
if(boss_salary == used[i]){exist = true;}
}
}
}while(exist);
p_arr[i][1]= boss_salary;
used[i_used]= p_arr[i][1];
i_used++;
flag = true;
}
}
}else{}
}
} while(flag);
delete []used;
}
void output_data (int p_n, int **p_arr)
{
for (int i=0; i<p_n; i++)
{
cout<< p_arr[i][1] << "\n";
}
}
void input_data (int p_n, int **p_arr)
{
for (int i=0; i<p_n; i++)
{
for (int j=0; j<2; j++)
{
cin>> p_arr[i][j];
}
}
}
I think the main issue is that you are doing int *used = new int(p_n);, this will create a pointer to an integer that has initial value equal to p_n. But, the problem comes here: used[i_used]=p_arr[i][1]; -- this may lead to a segmentation fault, as you may end up using memory outside the valid bounds.
I'm not sure about the algorithm, but based on the code, I think you should use this instead:
used = new int[p_n]; // assuming p_n > 0

generate a list of all possible combinations and randomly select a combination from the list

I found this code online on tutorials point. link https://www.tutorialspoint.com/cplusplus-program-to-generate-all-possible-combinations-out-of-a-b-c-d-e.
I tried to think of how to modify it so that it would randomly a single combination from the generated list, but I'm haven't figured it out yet.
#include<iostream>
using namespace std;
void Combi(char a[], int reqLen, int s, int currLen, bool check[], int l)
{
if(currLen > reqLen)
return;
else if (currLen == reqLen) {
cout<<"\t";
for (int i = 0; i < l; i++) {
if (check[i] == true) {
cout<<a[i]<<" ";
}
}
cout<<"\n";
return;
}
if (s == l) {
return;
}
check[s] = true;
Combi(a, reqLen, s + 1, currLen + 1, check, l);
check[s] = false;
Combi(a, reqLen, s + 1, currLen, check, l);
}
int main() {
int i,n;
bool check[n];
cout<<"Enter the number of element array have: ";
cin>>n;
char a[n];
cout<<"\n";
for(i = 0; i < n; i++) {
cout<<"Enter "<<i+1<<" element: ";
cin>>a[i];
check[i] = false;
}
for(i = 1; i <= n; i++) {
cout<<"\nThe all possible combination of length "<<i<<" for the given array set:\n";
Combi(a, i, 0, 0, check, n);
}
return 0;
}
im not a c++ specialist, but i think you should add a random number from -ArrayLenght to ArrayLenght, at least this works in python(which is written in c++)
i hope i understood your question right

finding elements in an array using a function c++

I am new to C++ and have just started learning functions. I have made a program to search an element in a 1-d array using a function search. But there is a logical error I can't comprehend! Is it because of the way the function is declared ?
int pos;
using namespace std;
int search(int *a, int size, int num);
int search(int *a, int size, int num)
{
int i;
for(i=0; i<size; i++)
{
if(a[i]==num)
{
pos=i; return 1;
}
else
return 0;
}
}
int main()
{
int a[5], size, num, i;
system("cls");
cout<<"Enter size(<5) \n";
cin>>size;
cout<<"Enter the elements of the array \n";
for(i=0; i<size; i++)
cin>>a[i];
cout<<"Enter the number to be searched \n";
cin>>num;
int b = search( a, size, num);
if(b==0)
{
cout<<"Element not found!"; exit(0);
}
else
cout<<"Element found at position "<<(pos+1);
system("pause");
return 0;
}
Output:
Enter size(<5)
4
Enter the elements of the array
4
3
2
1
Enter element to be searched
4
Element not found!
Your function always returns in the first loop iteration. If the first element is not the one to be searched, 0 is returned immediately. The loop never enters the second iteration.
you must return not found if you dont found any thing, with this code, you will always return zero, if the first element is not what you are searching for. something like this :
int search(int *a, int size, int num)
{
int i;
for(i=0; i<size; i++)
{
if(a[i]==num)
{
pos=i; return 1;
}
}
return 0;
}
It is in your logic
int search(int *a, int size, int num)
{
int i;
for(i=0; i<size; i++)
{
if(a[i]==num)
{
pos=i; return 1;
}
else
return 0;
}
}
Let's step through that. I'm going to give it [1, 2, 3, 4], 4, and 3.
i => 0
a[0] => 1
a[0] == 3 => false
return false
So, you check the first one, and if that doesn't work, it will immediately fail.
So try this:
int search(int *a, int size, int num)
{
for(int i = 0; i < size; ++i)
{
if(a[i]==num)
{
pos = i;
return 1;
}
}
return 0;
}
However, the better way would be to do something like this, and get rid of your global variable
int search(int *a, int size, int num)
{
for(int i = 0; i < size; ++i)
{
if(a[i]==num)
{
return i;
}
}
return -1;
}
Then if you get something != -1 you have found it.
Your search function is not doing what you think it is doing: it will return 0 as soon as a[i]!=num, thus not considering the rest of the elements of the array.
You'd better use someting like this, with a (non-global) variable returned:
#include <cstdlib>
// returns -1 if not found, else the found index
int search(int *a, int size, int num)
{
int pos = -1;
for(int i=0; i<size; i++)
{
if(a[i]==num)
{
pos = i;
break;
}
}
return pos;
}
// ... main() and parsing stuff goes here
if( (b = search( a, size, num)) == -1)
{
std::cerr<<"Element not found!";
return EXIT_FAILURE;
}
The problem is with your else statement. If the the element is not found straight away, it will automatically return 0. Furthermore, you use the integer 0 to indicate that the element is not found, but what if the element is found at position 0 (i.e it is the first element of the array)? Then you will still say that the element is not found, even though it clearly it exists in the array. Here is how I would do it.
bool search(int *a, int size, int num)
{
for (int i = 0; i < size; ++i)
{
if (a[i] == num)
{
cout << "Element found at position " << i << " of the array!" << endl;
return true;
}
}
cout << "Element not found!" << endl;
return false;
}
I hope you have learned about booleans (i.e true or false.) Since your function's main purpose is to search, it should return whether the element is found (true) or whether it is not found (false). Therefore, we loop through the array, and if we find it, we output the position of the element, and return true. Otherwise, if we exit the loop, this means the element has not been found, so we output that and return false. This gets rid of the global variable usage and the previous problems that I have mentioned.

Minesweeper Code error

#include <iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int n,m,counter=0;;
cin>>n>>m;
char x[n][m];
int y[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>x[i][j]; //array input
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
counter=0;
if(x[i][j]=='.') // searching for mines(*)
{
if (x[i][j-1]=='*')
counter++;
if (x[i][j+1]=='*')
counter++;
if (x[i-1][j]=='*')
counter++;
if (x[i+1][j]=='*')
counter++;
if (x[i+1][j-1]=='*')
counter++;
if (x[i+1][j+1]=='*')
counter++;
if (x[i-1][j-1]=='*')
counter++;
if (x[i-1][j+1]=='*')
counter++;
}
if(x[i][j]!='*')
y[i][j]=counter; // assign values
else
y[i][j]='*';
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(y[i][j]==42)
cout<<'*';
else
cout<<y[i][j]; // output numeric array
}
cout<<endl;
}
return 0;
}
This is Minesweeper code which has input like
4 4
*...
....
.*..
....
and output
*100
2210
1*10
1110
But the code's output is
*101
2210
1*10
1110
the zero on the top right turn into 1 What's making it do that ?
and is there an more easy way to search for the mines(*) without all the if conditions ?
You are reading beyond array's memory. While you're on lines 0..n-2, you're reading next line's chars, at line n-1 you're reading inaccessible memory.
You have to check that memory you access belongs to proper lines and are within array bounds.
I.e.
if (x[i][j+1]=='*')
What if j == m - 1?
if (x[i-1][j]=='*')
What if i == 0?
There are basically two fixes to this problem.
First, you can each time check that i, j are in bounds, say
if (j < m - 1 && x[i][j+1]=='*')
That will solve the problem, but is bad from code reuse perspective. I'd write a function, say
char get_at(char ** array, int i, int j, int n, int m)
{
if (i >= 0 && i < n && j >= 0 && j < m)
return array[i][j];
else
return '!';
}
This function returns '!' if indices are out of bounds, so it will not be interpreted as a mine.
I am trying to point something out in your code:
int main()
{
int n,m,counter=0;;
cin>>n>>m;
char x[n][m];
int y[n][m];
/**
* Minesweeper solving has always worked (if followed step-wise) is
* scanning the neihbouring 8 cells i.e.
* top(left,middle,right),side(left,right), and
* bottom(left,middle,right). You probably should try and follow this
* in your code so that any indexing issues can be avoided
*
*
* BTW - don't forget the corners/edges
*
*/
for(int i=0;i<n;i++) // This is a bit dangerous what if the index is -1?
{
for(int j=0;j<m;j++) // Same indexing issue here.
{
cin>>x[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
counter=0;
if(x[i][j]=='.') // If current pos is '.'
{
if (x[i][j-1]=='*')
counter++;
if (x[i][j+1]=='*')
counter++;
if (x[i-1][j]=='*')
counter++;
if (x[i+1][j]=='*')
counter++;
if (x[i+1][j-1]=='*')
counter++;
if (x[i+1][j+1]=='*')
counter++;
if (x[i-1][j-1]=='*')
counter++;
if (x[i-1][j+1]=='*')
counter++;
}
if(x[i][j]!='*') // If current pos is ANYTHING but '*', may be you want ELSE_IF condition?
y[i][j]=counter;
else
y[i][j]='*';
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(y[i][j]==42)
cout<<'*';
else
cout<<y[i][j];
}
cout<<endl;
}
return 0;
}
Is that what you are actually thinking?

N queen using c++ and backtracking using a dynamic 2D array

I've been trying to solve the N queen problem using backtracking. Most of the approaches that I found online, involved vectors, making it difficult for me to visualize the solutions as some applets on the Internet do.
The solution I came up with, is giving me many problems(which i have a feeling are related to indexing of the dynamic 2D array used) and I'm not able to figure it out using Dev-C++ debugger.Any help and/or constructive criticism is highly appreciated. Many thanks in advance.
Here is the solution that i came up with:
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
void display(char** b, int len);
void initialize(char** &b, int k);
void consider1strow(char ** b, int len);
void markunsafe(char** board, int rowno, int colno);
void marksafe(char** board, int rowno, int colno);
void considerrow(char** board, int rowno);
void backtrack(char** board, int rowno);
bool checksafety(char** board, int rowno, int colno);
void place(char** board, int rowno, int colno);
void solve(char** board, int len);
int state[20] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
int len;
void display(char** board, int len)
{
int i, j;
cout << endl << "The current state of the board:" << endl;
for (i = 0; i < len; i++)
{
for (j = 0; j < len; j++)
{
cout << board[i][j];
}
cout << endl;
}
}
void initialize(char** &b, int k)
{
int i, j;
//create dynamic board
b = new char*[k];
for (i = 0; i < k; i++)
{
b[i] = new char[k];
}
//initialize array
for (i = 0; i < k; i++)
{
for (j = 0; j < k; j++)
{
b[i][j] = '-';
}
}
}
void consider1strow(char ** board, int len)
{
int col;
cout << "Enter the column to try for the first row!";
cin >> col;
board[0][col - 1] = 'Q';
state[0] = col - 1;
markunsafe(board, 0, col - 1);
display(board, len);
}
void markunsafe(char** board, int rowno, int colno)
{
int i, j;
//mark row as unsafe
for (i = 0; i < len; i++)
{
board[rowno][i] = 'x';
}
//mark column as unsafe
for (i = 0; i < len; i++)
{
board[i][colno] = 'x';
}
//mark unsafe diagonals
for (i = 0; i < len; i++)
{
for (j = 0; j < len; j++)
{
if ((rowno + colno) == (i + j))
{
board[i][j] = 'x'; //check if index gives a problem of +/- 1
}
if ((rowno - colno) == (i - j))
{
board[i][j] = 'x'; //check if index gives a problem of +/- 1
}
}
}
board[rowno][colno] = 'Q';
}
void marksafe(char** board, int rowno, int colno)
{
int i, j;
//mark row as safe
for (i = 0; i < len; i++)
{
board[rowno][i] = '-';
}
//mark column as unsafe
for (i = 0; i < len; i++)
{
board[i][colno] = '-';
}
//mark unsafe diagonals
for (i = 0; i < len; i++)
{
for (j = 0; j < len; j++)
{
if ((rowno + colno) == (i + j))
{
board[i][j] = '-'; //check if index gives a problem of +/- 1
}
if ((rowno - colno) == (i - j))
{
board[i][j] = '-'; //check if index gives a problem of +/- 1
}
}
}
}
void considerrow(char** board, int rowno)
{
bool safe = 0;
int i;
for (i = 0; i < len; i++)
{
safe = checksafety(board, rowno, i);
if (safe && (i >= state[rowno]))
{
break;
}
}
if (safe && (i >= state[rowno]))
{
place(board, rowno, i);
}
else if (!safe)
{
backtrack(board, rowno);
}
}
void backtrack(char** board, int rowno)
{
marksafe(board, rowno - 2, state[rowno - 2]);
considerrow(board, rowno);
}
bool checksafety(char** board, int rowno, int colno)
{
if (rowno == 0)
{
return 1;
}
else if (board[rowno][colno] == 'x')
{
return 0;
}
else if (board[rowno][colno] == '-')
{
return 1;
}
}
void place(char** board, int rowno, int colno)
{
board[rowno][colno] = 'Q';
state[rowno] = colno;
markunsafe(board, rowno, colno);
}
void solve(char** board, int len)
{
int i = 0;
if (i == len)
{
display(board, len);
}
else
{
consider1strow(board, len);
for (i = 1; i < len; i++)
{
considerrow(board, i);
}
}
}
int main()
{
char** board;
cout << "Enter the size of the board!";
cin >> len;
initialize(board, len);
solve(board, len);
getch();
}
It is running after the initial configuration, but you're not printing it. Change this (inside solve):
for(i=1;i<len;i++)
{considerrow(board,i);}
for this:
for(i=1; i<len; i++) {
considerrow(board,i);
display(board,len);
}
Besides that, there is a problem with the way you are doing backtracking. If no options are available, you are removing the queen from the previous row (that's ok) and then you are marking every cell it was attacking as safe (not ok). The problem is that some of these cells may be under attack by a different queen, so you cannot mark them as safe. Furthermore, you do not place a different queen on that row. I propose some solutions:
First, make it recursive: considerrow would call itself with the following row, and return true (1) if it succeeds or false (0) if it fails. If it fails with the next row, you can use the next queen in the current row and call considerrow again, until you succeed or run out of columns, in which case you return false.
To consider a different queen on a certain row, you can do two things: create a copy of the board which you would pass to considerrow for the next row (and thus keeping a 'before' copy to try a different queen), or mark every cell as safe, and then check all the existing queens to mark cells unsafe.
Edit:
To make it recursive, we are going to make considerrow call itself with the next value.
bool considerrow(char** board,int rowno) {
//Print the board
display(board,len);
bool safe=0;
int i;
for(i=0; i<len; i++) {
safe=checksafety(board,rowno,i);
if(safe) {
place(board,rowno,i);
//Is this the last row? If so, we suceeded
if (rowno==len-1) return 1;
//Call itself with next row, check if suceeded
if (considerrow(board,rowno+1))
return 1;
else //Failed, try a different row
backtrack(board,rowno);
}
}
return 0; //If we got here, then we ran out of colums. Return failure
}
The backtrack function can be modified to revert the current row like this:
void backtrack(char** board, int rowno) {
//Clear the current row
marksafe(board,rowno,state[rowno]);
//Check that every cell attacked by another queen is marked unsafe
for(int i=0; i<rowno; i++) markunsafe(board,i,state[i]);
}
Doing that, solve will only need to call the first row:
void solve(char** board,int len) {
considerrow(board,0);
display(board,len);
}