简体   繁体   English

C ++连续内存操作

[英]C++ contiguous memory operation

I have c++ program in which I am calculating determinant of a matrix using normal array which is as follows: 我有一个C ++程序,其中使用正态数组计算矩阵的行列式,如下所示:

/* rand example: guess the number */
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;

int** generateStandardMatrix(int dimension);
void ijMinor(int *matrix[], int *minorMatrix[], int size, int row, int column);
int determinant(int *matrix[], int size);

void ijMinor(int *matrix[], int *minorMatrix[], int size, int row, int column) {
  for (int i = 0; i < size; i++) {
    for (int j = 0; j < size; j++) {
      if (i < row) {
        if (j < column)minorMatrix[i][j] = matrix[i][j];
        else if (j == column)continue;
        else minorMatrix[i][j - 1] = matrix[i][j];
      }
      else if (i == row)continue;
      else {
        if (j < column)minorMatrix[i - 1][j] = matrix[i][j];
        else if (j == column)continue;
        else minorMatrix[i - 1][j - 1] = matrix[i][j];
      }
    }
  }
}

int determinant(int *matrix[], int size) {
  if (size == 1)return matrix[0][0];
  else {
    int result = 0, sign = -1;
    for (int j = 0; j < size; j++) {

      int **minorMatrix;
      minorMatrix = new int*[size - 1];
      for (int k = 0 ; k < size - 1 ; k++)
        minorMatrix[k] = new int[size - 1];

      ijMinor(matrix, minorMatrix, size, 0, j);

      sign *= -1;
      result += sign * matrix[0][j] * determinant(minorMatrix, size - 1);
      for (int i = 0; i < size - 1; i++) {
        delete minorMatrix[i];
      }
    }

    return result;
  }
}



int main (int argc, char* argv[])
{

  /* initialize random seed: */
  srand (time(NULL));
  // int iSecret, iGuess;
  int dimension = atoi(argv[1]);

  int rowCount = dimension , colCount = dimension;
  //2d array storing the integer values
  int** ary = new int*[dimension];

  //vector of vector storing the indices across the array for the threads to pick up from
  vector<vector<int> > vec;


  ary = generateStandardMatrix(dimension);


  printf("Array value : %d\n", ary[0][0]);
  int detVal = determinant(ary, dimension);
  printf("determinant value : %d\n", detVal);


  return 0;
}

int** generateStandardMatrix(int dimension) {
  int** ary = new int*[dimension];
  int counter = 0;
  for (int i = 0; i < dimension; ++i) {
    ary[i] = new int[dimension];
    counter = counter + 1;
    for (int j = 0; j < dimension; ++j)
    {
      ary[i][j] = counter;
      std::cout << ary[i][j] << "\t" << std::flush;
    }
    std::cout << std::endl;
  }

  return ary;
}

I want to replace it with code in which I allocate memory for the array before the start of the algorithm and then change the determinant and the ijMonor functions so that they don't create new array's but use the same array only. 我想用在程序开始之前为数组分配内存的代码代替它,然后更改行列式和ijMonor函数,以便它们不创建新数组,而仅使用同一数组。 The determinant will take parameter like: determinant(int *matrix, int *startOfMyWorkspace, int size) so that it knows where to start. 行列式将采用如下参数:determinant(int * matrix,int * startOfMyWorkspace,int size),以便知道从何处开始。

I am not good at c++ and so far I was not able to do it. 我不擅长C ++,到目前为止我还做不到。 Can someone please provide some sample code. 有人可以提供一些示例代码吗? I allocated some memory for array and created and array but was unable to change the ijMinor and determinant functions for that. 我为数组分配了一些内存,并创建了数组,但是无法为此更改ijMinor和行列式函数。

This is how I am allocating memory: 这就是我分配内存的方式:

int main (int argc, char* argv[])
{

  /* initialize random seed: */
  srand (time(NULL));
  // int iSecret, iGuess;
  int dimension = atoi(argv[1]);

  int *a;
  size_t const N_BYTES = dimension * dimension * sizeof(int);

  a   = (int*)malloc(N_BYTES);

  createData(dimension,a);

  return 0;
}

void createData(int const dimension, int* const a)
{
    int row, col;

    srand((unsigned)time(NULL));
    int counter;
    for(int row = 0; row < dimension; row++) {
      counter = counter + 1;
       for(int col = 0; col < dimension; col++) {
           int i = col + row * dimension;
           a[i] = counter;
          std::cout << a[i] << "\t" << std::flush;
        }
        std::cout << std::endl;
    }
}

Try this. 尝试这个。 Note if you use new to allocate an array, you need to use delete[] to free all of it. 注意,如果使用new分配数组,则需要使用delete[]释放所有数组。 You'll get away with delete (ie it won't crash) but this will only free the first element. 您将摆脱delete (即不会崩溃),但这只会释放第一个元素。 Your other functions are the same as you posted. 您的其他功能与您发布的功能相同。

You're dynamically allocating space for minorMatrix in determinant function, but it's hard to see how that could be preallocated. 您正在determinant函数中为minorMatrix动态分配空间,但是很难知道如何进行预分配。 I've modified determinant function to use allocate_arr and deallocate_arr . 我修改了determinant函数,以使用allocate_arrdeallocate_arr

int ** allocate_arr(int dimension)
{
    int** a = new int*[dimension];
    for (int i = 0; i < dimension; ++i)
        a[i] = new int[dimension];
    return a;
}

void deallocate_arr(int dimension, int **a)
{
    for (int i = 0; i < dimension; ++i)
        delete[] a[i];
    delete[] a;
}

int determinant(int *matrix[], int size) {
    if (size == 1)return matrix[0][0];
    else {
        int result = 0, sign = -1;
        for (int j = 0; j < size; j++) {

            int **minorMatrix = allocate_arr(size - 1);
            ijMinor(matrix, minorMatrix, size, 0, j);

            sign *= -1;
            result += sign * matrix[0][j] * determinant(minorMatrix, size - 1);
            deallocate_arr(size - 1, minorMatrix);
        }
        return result;
    }
}

void generateStandardMatrix(int dimension, int**ary) {
    int counter = 0;
    for (int i = 0; i < dimension; ++i) {
        counter = counter + 1;
        for (int j = 0; j < dimension; ++j)
        {
            ary[i][j] = counter;
            std::cout << ary[i][j] << "\t" << std::flush;
        }
        std::cout << std::endl;
    }
}

int main(int argc, char* argv[])
{
    srand(time(NULL));

    int dimension = atoi(argv[1]);
    int** a = allocate_arr(dimension);

    generateStandardMatrix(dimension, a);
    printf("Array value : %d\n", a[0][0]);
    int detVal = determinant(a, dimension);
    printf("determinant value : %d\n", detVal);

    // ... do more computations here, reusing `a` ...

    deallocate_arr(dimension, a);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM