Buffer overrun while writing to 'costMatrix' - c++

I am getting a warning when writing to a dynamically allocated array that I can't seem to figure out. Visual Studio says that there is a "buffer overrun." However, I don't see what would cause that error. For reference, here is my code:
//Initialize the cost matrix
int numMembers = 10;
int** costMatrix = new int* [numMembers];
for (int i = 0; i < numMembers; i++)
{
costMatrix[i] = new int[5];
}
for (int i = 0; i < numMembers; i++)
{
for (int j = 1; j < 3+1; j++)
{
int index = 3;
costMatrix[i][index] = j; //Set cost matrix
}
}
Any help would be greatly appreciated. Thanks!

Related

multiplying two arrays of different dimensions in c++

for my assignment I have to multiply two matrices together to create a new one. I will then sort the new one from an inherited class. My question is what is the format to multiply two arrays of different dimensions my first one a1d is 5 integers. My other one a2d is a 5x10 array. What is the correct way to multiply these together being that they are different sizes and dimensions. Do I multiply a1d by every row of a2d? I am going to output the products to a 1 dimensional array so that sorting is easier. I have drawn out the two arrays as tables to help me visualize it. I will attach the short code I have and my illustration. This is in C++.
#pragma once
#include<ctime>
#include<iostream>
#include<cmath>
using namespace std;
class matrices
{
private:
int* a1d[5]; // Old Code:int* a1d = new int[5];
int** a2d = new int* [5];
public:
int* matrix;
matrices();
~matrices();
int* filla1d();
int* filla2d();
int* multiply();
};
int* matrices::filla1d() {
for (int i = 0; i < 5; i++) {
a1d[i] = new int;
}
for (int i = 0; i < 5; i++) {
*a1d[i] = rand() % 10 + 1;
}
return *a1d;
}
int* matrices::filla2d() {
for (int i = 0; i < 5; i++) {
a2d[i] = new int[10];
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++) {
a2d[i][j] = rand() % 10 + 1;
cout << a2d[i][j] << endl;
}
}
return *a2d;
}
int* matrices::multiply() {
}
it is required that I only use pointer type variables and pointer returning functions, though that doesn't change too much. I don't know how they should be multiplied, and because of that, I am not sure how many values will be generated from the multiplication for the size of the new array. Thanks in advance!
Here is what I have designed to multiply them together. I have changed how my pointer arrays are allocated. My problem now is that it tells me that "expression must have arithmetic or unscoped enum type". Where I have matrix[i] =(a1d[index1] * a2d[index1][index2]); I thought maybe a1d needed to be a pointer type but it gives me the error where it can't convert from int* to int.
Also, when I debug, my a1d and matrix arrays allocate perfectly and show the correct number of data slots when moused over. However, a2d only shows one pointer which points to 5 in this case. I followed the syntax I have seen online for an array of pointers to create a 2d array.
int* matrices::filla1d() {
for (int i = 0; i < 5; i++) {
a1d[i] = new int;
}
for (int i = 0; i < 5; i++) {
*a1d[i] = rand() % 10 + 1;
}
return *a1d;
}
int* matrices::filla2d() {
for (int i = 0; i < 5; i++) {
a2d[i] = new int[10];
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++) {
a2d[i][j] = rand() % 10 + 1;
}
}
return *a2d;
}
int* matrices::multiply() {
for (int i = 0; i < 50; i++) {
matrix[i] = new int;
}
int index1 = 0;
int index2 = 0;
for (int i = 0; i < 50; i++) {
matrix[i] = (a1d[index1] * a2d[index1][index2]);
index1++;
index2++;
}
return *matrix;
}
class matrices
{
private:
int* a1d[5];
int** a2d = new int*[5];
public:
int* matrix[50];
matrices();
~matrices();
int* filla1d();
int* filla2d();
int* multiply();
};
Edit 2:
I changed the line to fill up the new matrix to say
*matrix[i] = a2d[index1][index2] * *a1d[index1];
Now I get an access violation error on this line. I have matrix allocated the same way I have a1d allocated, what can cause my access violation?

c++ heap corruption dectected

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.

Assinging a pointer-to-pointer dynamic array with values

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;
}

C++ 2D dynamic array allocation

I have a float** array that contains num_rows rows and num_cols columns. I'd like to determine the number of occurrences of every number between 0-9 columnwise. To do this, I thought of using another 2D array of size [10][num_cols], so that for each column the number corresponding to an element is the number of occurrences of that number in the original table.
Example: if the original table contains 1 2 3 1 1 in the fifth column, then in the second column, the values should be like: 1-> 3, 2 -> 1, 3 -> 1.
I tried using the function as follows, but it gives me a pointer error. I tried using vectors but that too brings no luck.
int ** attribute_count(float * * input, int row_num, int col_num) {
int ** arr_2 = new int * [10];
int * arr = new int[10 * col_num];
int counter = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < col_num; j++) {
arr_2[i][j] = 0;
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < col_num; j++) {
int temp = input[i][j];
arr_2[temp][j]++;
}
}
return arr_2;
}
EDIT:
I tried your suggestions. The new code is:
int** attribute_count(float** input, int row_num, int col_num) {
int** arr_2 = new int* [10];
int* arr = new int[10 * col_num];
int counter = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < col_num; j++) {
arr_2[i] = new int[col_num];
}
}
for (int i = 0; i < 11; i++) {
for (int j = 0; j < col_num; j++) {
int temp = input[i][j];
arr_2[temp][j]++;
}
}
return arr_2;
}
This still gives me memory errors. The function is being called in the .cpp like this:
int** attr = attribute_count(training_data, 10, num_cols_train);
cout<<attr[5][1];
Any idea what I'm doing wrong even now?
I think your problem is in incorrect allocation of the 2D array. Try
int ** arr_2 = new int* [row_num];
for (int i = 0; i < row_num; i++)
arr_2[i] = new int[col_num];
You've only allocated one dimension of arr_2. You need to loop through and allocate an array of ints on each one to get the 2nd dimension.
EDIT: Also, what's up with arr? You allocate it, never use it, don't return it, and don't deallocate it. That's how we spell memory leak.
arr_2 is defined and allocated as an array of pointers to int, but you don't actually assign/allocate those pointers.
Here's a stab at correcting your code - however I'm not convinced you have rows and columns the right way around...
int ** attribute_count(float ** input, int row_num, int col_num)
{
int ** arr_2 = new int * [10];
for (int i = 0; i < 10; i++)
{
arr_2[i] = new int[col_num];
for(int j = 0 ; j < col_num ; j++)
{
arr_2[i][j] = 0;
}
}
for (int i = 0; i < row_num; i++)
{
for (int j = 0; j < col_num; j++)
{
int temp = input[i][j];
arr_2[temp][j]++;
}
}
return arr_2;
}

Dealocating 2d arrays pointers

What am I doing wrong? I allocated using new but when i try to delete[] it gives me an error.
mycode:
int** ma;
int n;
int m;
m = nr_col_lin;
n = nr_col_lin;
ma = new int*[m];
for(i = 0; i < m; i++)
ma[i] = new int[n];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
ma[i][j] = 0;
}
}
and the dealocation:
for(int i = 0; i < m; ++i)
delete[] ma[i];
delete [] ma;
EDIT:
I found the problem... it wasn't in the code... I was deleting something I didn't allocate.
There is absolutely nothing wrong in your code. It is perfect. The probem must be somewhere else, assuming nr_col_lin is initialized with some valid value, i.e make sure it is not 0 or negative. If it is size_t or some unsigned integral value, then make sure m and n are greater than 0.