Home > other >  Scope of dynamic (multidimm) array when initialized with pointer and new
Scope of dynamic (multidimm) array when initialized with pointer and new

Time:06-18

I'm royally confused right now. I have seen similar questions asked, and my implementation seems to be along the lines of these solutions, but I just can't get it to work.

I need to have a UtilClass that can initialize and dump a multi-dimensional dynamic array. I just want to pass the pointer the the array in BaseClass that I want initialized along with the dims. I chose to use the new keyword instead of malloc(), but I'm worried that the scope of the pointers is limited to the init function and not to the lifetime of the BaseClass (or the array pointer for that matter) and that's why the dump function produces a SEGFAULT.

Here is some code to explain:

// BaseClass.h
#pragma once
include "UtilClass.h"

class BaseClass
{
public:
  UtilClass* util{nullptr};
  double** array{nullptr};
};


// BaseClass.cpp
#include "BaseClass.h"

BaseClass::BaseClass() {
  util = new UtilClass();
  util->init(array, 4, 6);
  util->dump(array, 4, 6);
}
// UtilClass.h
#pragma once

class UtilClass {
  void init(double** array, int rows, int cols);
  void dump(double** array, int rows, int cols);
};

// UtilClass.cpp
#include "UtilClass.h"

void UtilClass::init(double **darray, int rows, int cols) {
  int i,j;
  array = new double*[rows];
  for (i = 0; i < rows; i  )
      array[i] = new double[cols];

  for (i = 0; s < rows; i  )
      for (j = 0; a < cols; j  ) {
          data[i][j] = 1;
          std::cout << "Data in array " << data[i][j] << std::endl; // This obviously works
      }
}

void dump(double** array, int rows, int cols) {
  int i, j;
  for (i = 0; i < rows; i  )
      for (j = 0; j < cols; j  )
          std::cout << array[i][j] << " "; // But this produces a SEGFAULT
      std::cout << std::endl;
}

CodePudding user response:

Let me start off with a piece of advice: it's best to reuse existing tools. Consider using std::vector for dynamically-allocated array. This is well tested, optimized and easy to use. Otherwise, you'll need to deal with conundrums of memory management (e.g. deallocate the allocated chunks of memory in the dtor of BaseClass).

Regarding your question: when you call init, you pass the pointer by value. This means that the init method allocates some memory and stores the pointer to it in its local copy of darray. At the end of init this local variable is gone (you lose access to it, leaking whole allocated memory). Change the method to:

  void UtilClass::init(double ***array, int rows, int cols) {
  int i,j;
  *array = new double*[rows];
  for (i = 0; i < rows; i  )
      (*array)[i] = new double[cols];

  for (i = 0; i < rows; i  )
      for (j = 0; j < cols; j  ) {
          (*array)[i][j] = i*j;
          std::cout << "Data in array " << (*array)[i][j] << std::endl; // This obviously works
      }
  }

When calling dump, you need to pass 4 and 6 (and not 5 and 6).

  • Related