C++ Access Violation - Possibly due to a table? - c++

Within the main function I have the following.
int numRows = rowSequence.length() + 1;
int numCols = columnSequence.length() + 1;
int** twoDimTable = new int* [numRows];
for (int rowIndex = 0; rowIndex < numRows; rowIndex++)
{
twoDimTable[rowIndex] = new int [numCols];
}
//updating table
for (int i = 0; i <= numRows; i++)
{
for (int j = 0; j <= numCols; j++)
{
if (i == 0 || j == 0)
twoDimTable[i][j] = 0;
// when I start running my code I receive an unhandled exception right at the 'if'
// statement: Access violation writing location. I looked at other similar
// situations, but cannot seem to understand the specific issue
else if (rowSequence[i - 1] == columnSequence[j - 1])
twoDimTable[i][j] = twoDimTable[i - 1][j - 1] + 1;
else
twoDimTable[i][j] = max(twoDimTable[i - 1][j], twoDimTable[i][j - 1]);
}
}

One problem is that your for loops are wrong; numRows and numCols are not valid indices, so they should not be included in your iterations.
That is, instead of this:
for (int i = 0; i <= numRows; i++)
{
for (int j = 0; j <= numCols; j++)
{
[...]
}
}
... you should have this:
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
[...]
}
}

Related

how to fill array with contents of another array that has different size using loops

I am trying to fill an array sized by [4344][20] with the contents of other array sized by [5430][20]. I wrote the following code and it has no errors. It filled the X_train correctly, but the Y_train didn't filled successfully. it remained zeros as its initialized.
my code:
void split(int fold, int array_X_set[5430][20], int array_Y_set[5430],
int X_train[4344][20], int Y_train[4344],
int X_test[1086][20], int Y_test[1086])
{
int rows = 5430;
int cols = 20;
int division = 1086;
int nTest = 0, nTrain = 0, nTest1 = 0, nTrain1 = 0;
switch (fold) {
case 1:
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (i < division) {
X_test[nTest][nTest1] = array_X_set[i][j];
Y_test[nTest] = array_Y_set[i];
nTest1++;
}
else {
X_train[nTrain][nTrain1] = array_X_set[i][j];
Y_train[nTrain] = array_Y_set[i];
nTrain1++;
}
}
}
break;
another condition :
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (i >= 1086 && i <= 2171) {
X_test[i][j] = array_X_set[i][j];
Y_test[i] = array_Y_set[i];
}
else {
X_train[i - division][j] = array_X_set[i][j];
Y_train[i - division] = array_Y_set[i];
}
}
}
you can try this code, so that you don't need extra variable like nTest, nTrain, nTest1, nTrain1.
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (i < division) {
X_test[i][j] = array_X_set[i][j];
Y_test[i] = array_Y_set[i];
} else {
X_train[i-division][j] = array_X_set[i][j];
Y_train[i-division] = array_Y_set[i];
}
}
}
updated:
Another condition:
if you have 10 elements in array
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
^ ^ ^ ^
you want to put index 3~6 to new array, in new array, its index would be 0~3
so for the part >=3 && <=6 would be newArr[i-3] = originalArr[i]
and if you want to put index 0~2 and 7~9 to new array, in new array, its index would be 0~2 and 3~5
so for the part <3 would be newArr[i] = originalArr[i]
the part >6 would be newArr[i-(6-3+1)] = originalArr[i], (6-3+1) means the element count between the part >=3 && <=6
Here is the code in your condition:
(>=1086 && <=2171 in test array, <1086 || >2172 in train array)
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (i >= 1086 && i <= 2171) {
X_test[i - 1086][j] = array_X_set[i][j];
Y_test[i - 1086] = array_Y_set[i];
}
else {
if(i<1086){
X_train[i][j] = array_X_set[i][j];
Y_train[i] = array_Y_set[i];
}else{
X_train[i - (2171-1086+1)][j] = array_X_set[i][j];
Y_train[i - (2171-1086+1)] = array_Y_set[i];
}
}
}
}

C++ free(): invalid next size (fast)

I've looked at similar questions (such as this, this, and this), but I still can't figure this out. On a few test cases, I'm getting the error and can't make sense of it. The error I'm getting is:
free(): invalid next size (fast)
My code uses the Robot Coin Collection algorithm. My implementation of it is below:
int collectTens( vector< vector<int> > grid ) {
vector<vector<int>> result(grid.size(), vector<int>(grid.size(), 0));
int rows = grid.size();
int cols = grid[0].size();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] % 10 != 0) {
grid[i][j] = 0;
}
}
}
result[0][0] = grid[0][0];
for (int k = 1; k < cols; k++) {
result[0][k] = result[0][k - 1] + grid[0][k];
}
for (int i = 1; i < rows; i++) {
result[i][0] = result[i - 1][0] + grid[i][0];
for (int j = 1; j < cols; j++) {
result[i][j] = max(result[i - 1][j], result[i][j - 1]) + grid[i][j];
}
}
cerr << result[rows - 1][cols - 1] << endl;
return result[rows - 1][cols - 1];
}
In the test cases that the error occurs, the result can be outputted using stderr but will not be able to return anything due to the error. Which is the part that doesn't really make sense to me, because it is the same position in the 2D vector.
The line
vector<vector<int>> result(grid.size(), vector<int>(grid.size(), 0));
creates square grid, not a rectangular grid. You probably meant to use:
vector<vector<int>> result(grid.size(), vector<int>(grid[0].size(), 0));
^^^^
I suggest using:
int rows = grid.size();
int cols = grid[0].size();
vector<vector<int>> result(rows, vector<int>(cols, 0));

Comparing pixels in alpha-trimmed filter

I have the following problem. I have written code for alpha-trimmed filter in opencv library. I think that it is properly constructed but I don't know how to compare two 3 channels pixels during sorting a 'window with pixels'. In my code it is done but comparing two but it is impossible for vectors. I assume that i should compare it one channel and after second and so on. Have you any hints for me, or could you propose some modifications in my code. This is my code.
int alphatrimmed(Mat img, int alpha)
{
Mat img9 = img.clone();
const int start = alpha;
const int end = 9 - alpha;
//going through whole image
for (int i = 1; i < img.rows - 1; i++)
for (int j = 1; j < img.cols-1; j++)
{
int k = 0;
Vec3b element[9];
//selecting elements
for (int m = i - 1; m < i + 2; m++)
for (int n = j - 1; n < j + 2; n++)
element[k++] = img.at<Vec3b>(m*img.cols + n);
for (int i = 0; i < end; i++)
{
int min = i;
for (int j = i + 1; j < 9; j++)
if (element[j] < element[min])
min = j;
Vec3b temp = element[i];
element[i] = element[min];
element[min] = temp;
}
const int result = (i - 1)*(img.cols - 2) + j - 1;
img9.at<Vec3b>(result) = element[start];
for (int j = start + 1; j < end; j++)
img9.at<Vec3b>(result) += element[j];
img9.at<Vec3b>(result) /= 9 - alpha;
}
namedWindow("AlphaTrimmed Filter", WINDOW_AUTOSIZE);
imshow("AlphaTrimmed Filter", img9);
return 0;
}
Thank you for your time spent on solving my problem.

how to optimize a 'for loop' for 3d complex<double> arrays to improve speed in C++

all the arrays in this code are complex type in this code and the running time for this for loop is about 1 min. Ktemp is an array with size 141*1202*141. could anyone help me to optimize this code and save the running time?
complex<double> ***P1;
P1 = new complex<double>**[141];
for (i = 0; i < num_y; i++)
{
P1[i] = new complex<double> *[1202];
for (j = 0; j < tsize; j++)
{
P1[i][j] = new complex<double>[141];
}
}
for (int zz = 1; zz < 20; zz++)//in z direction
{
for (i = 0; i < 141; i++)
{
for (j = 0; j < 1202; j++)
{
for (k = 0; k < 141; k++)
{
if (Ktemp[i][j][k].real() <= 0)
{
P1[i][j][k] = 0;
}
else
{
P1[i][j][k] = excit_pfft[i][j][k] * expn[i][j][k];
}
}
}
}
excit_pfft = P1;
}
my second question is about rewriting matlab function 'fftshift' with C++. I have finished the code, but it seems not that efficient. could anyone help me rewrite this code? my code is attached below:
complex<double> ***fftw_shift(complex<double> ***te, int a, int b, int c)
{
complex<double> ***tempa;
tempa = new complex<double> **[a];
for (i = 0; i < a; i++)
{
tempa[i] = new complex<double> *[b];
for (j = 0; j < b; j++)
{
tempa[i][j] = new complex<double>[c];
}
}
/*for the row*/
if (c % 2 == 1)
{
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c / 2; k++)
{
tempa[i][j][k] = te[i][j][k + c / 2 + 1];
tempa[i][j][k + c / 2] = te[i][j][k];
tempa[i][j][c - 1] = te[i][j][c / 2];
}
}
}
}
else
{
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c / 2; k++)
{
tempa[i][j][k] = te[i][j][k + c / 2];
tempa[i][j][k + c / 2] = te[i][j][k];
}
}
}
}
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c; k++)
{
te[i][j][k] = tempa[i][j][k];
}
}
}
/*for the column*/
if (b % 2 == 1)
{
for (i = 0; i < a; i++)
{
for (j = 0; j < b / 2; j++)
{
for (k = 0; k < c; k++)
{
tempa[i][j][k] = te[i][j + b / 2 + 1][k];
tempa[i][j + b / 2][k] = te[i][j][k];
tempa[i][b - 1][k] = te[i][b / 2][k];
}
}
}
}
else
{
for (i = 0; i < a; i++)
{
for (j = 0; j < b / 2; j++)
{
for (k = 0; k < c; k++)
{
tempa[i][j][k] = te[i][j + b / 2][k];
tempa[i][j + b / 2][k] = te[i][j][k];
}
}
}
}
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c; k++)
{
te[i][j][k] = tempa[i][j][k];
}
}
}
/*for the third dimension*/
if (a % 2 == 1)
{
for (i = 0; i < a / 2; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c; k++)
{
tempa[i][j][k] = te[i + a / 2 + 1][j][k];
tempa[i + a / 2][j][k] = te[i][j][k];
tempa[a - 1][j][k] = te[a / 2][j][k];
}
}
}
}
else
{
for (i = 0; i < a / 2; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c; k++)
{
tempa[i][j][k] = te[i + a / 2][j][k];
tempa[i + a / 2][j][k] = te[i][j][k];
}
}
}
}
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c; k++)
{
te[i][j][k] = tempa[i][j][k];
}
}
}
return (te);
}
Since you are repeatedly multiplying by the values in expn (i.e. calculating an exponent) you can do this more efficiently using the pow function and get rid of the zz loop:
for (i = 0; i < 141; i++)
{
for (j = 0; j < 1202; j++)
{
for (k = 0; k < 141; k++)
{
if (Ktemp[i][j][k].real() <= 0)
{
excit_pfft[i][j][k] = 0;
}
else
{
excit_pfft[i][j][k] = excit_pfft[i][j][k] * pow(expn[i][j][k], 20);
}
}
}
}
Your code also seems to have a memory leak because you assign P1 to excit_pfft, but never free the previous contents of excit_pfft. You don't need to have the P1 temporary array in any case once you get rid of the outer loop.
I'm not sure of the internals of the complex pow() function, but you can calculate the (scalar) exponent of a complex number geometrically by converting it to polar co-ordinates (angle + distance scalar), then multiplying the angle by the power and raising the distance to the power, then converting back. So it's a lot faster than repeated multiplication.
First (will probably give you a big performance boost), get rid of the pointer arrays if you know beforehand the size of your arrays and simply allocate them in the stack:
complex<double> P1[141][1202][141];
Instead of :
complex<double> ***P1;
P1 = new complex<double>**[141];
for (i = 0; i < num_y; i++)
{
P1[i] = new complex<double> *[1202];
for (j = 0; j < tsize; j++)
{
P1[i][j] = new complex<double>[141];
}
}
And since I don't know exactly what this does, I'm assuming this:
for (int zz = 1; zz < 20; zz++)//in z direction
{
for (i = 0; i < 141; i++)
{
for (j = 0; j < 1202; j++)
{
for (k = 0; k < 141; k++)
{
if (Ktemp[i][j][k].real() <= 0)
{
P1[i][j][k] = 0;
}
else
{
P1[i][j][k] = excit_pfft[i][j][k] * expn[i][j][k];
}
}
}
}
excit_pfft = P1;
}
Could become this:
for (int zz = 1; zz < 20; zz++)//in z direction
{
for (i = 0; i < 141; i++)
{
for (j = 0; j < 1202; j++)
{
for (k = 0; k < 141; k++)
{
if (Ktemp[i][j][k].real() <= 0)
{
P1[i][j][k] = 0;
}
else
{
P1[i][j][k] = P1[i][j][k] * expn[i][j][k];
}
}
}
}
}
If this cannot be done than I'll need a more broad chunk of this code to analyze excit_pfft, etc.
A huge performance boost you could have is to use Worker Threads and run this last code multithreaded.
The same goes for our second question, Worker Threads should do it.
EDIT:
On second though, the stack won't be able to handle that much variables.
I'd recommend using vector<vector<vector<complex<double> > > > instead.

Putting String into a 2D Matrix in Objective C++

So I'm using Objective C++ and I want to put a string into a 4 by X (X = length of string/4) int array by using the ASCII code. The first quarter of the string (which is formatted to fit completely into a 4 by X array) is supposed to go in [0][col], the second quarter into [1][col], the third quarter into [2][col] and the fourth quarter into [3][col]. So I tried the following with 4 for loops, but it doesnt work at all, and I just can't seem to get it to work somehow. Any suggestions would be greatly appreciated.
textMatrix is the matrix in which I want to put the NSString/ASCII number, and inputFinal is the NSString itself. Length * (1/4) or whatever is also always going to be an integer.
for(int i = 0; i < length*(1/4); i++)
{
textMatrix[0][i] = (int)[inputFinal characterAtIndex: i];
}
for(int j = length*(1/4); j < length*(2/4); j++)
{
textMatrix[1][j] = (int)[inputFinal characterAtIndex: j];
}
for(int k = length*(2/4); k < length*(3/4); k++)
{
textMatrix[2][k] = (int)[inputFinal characterAtIndex: k];
}
for(int l = length*(3/4); l < length; l++)
{
textMatrix[3][l] = (int)[inputFinal characterAtIndex: l];
}
You can rewrite your 4 loops in 1 loop:
for(int i = 0; i < length; i++)
{
textMatrix[i/4][i%4] = (int)[inputFinal characterAtIndex:i];
}
I don't think I understand what you're trying to do..
Given a string: "Here";
do you want:
Matrix[0][0] = 'H';
Matrix[1][1] = 'e';
Matrix[2][2] = 'r';
Matrix[3][3] = 'e';
If so then this works:
#import <objc/objc.h>
#import <objc/Object.h>
#import <Foundation/Foundation.h>
#implementation TestObj
int main()
{
NSString* str = #"Here";
int matrix[4][4] = {0};
for (int i = 0, j = 0; j < 4; ++j)
{
matrix[i][i++] = (int) [str characterAtIndex: j];
}
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
printf("%c", (char)matrix[i][j]);
}
}
return 0;
}
#end
The above prints Here.
actually a double loop like so ended up working best for me:
int index = 0;
for(int row = 0; row < 4; row++)
{
for(int col = 0; col < length/4; col++)
{
textMatrix[row][col] = (int)[inputFinal characterAtIndex:index];
index++;
}
}