0

I have this main function and i want to get an multidimensional array out of this to generate a graph instead of cout

int main(){
   zoro z;
   std:ifstream k("ggg.grf");
   z.getfromstream(k);
   for(int i =0 ; i < z.nnodes; i++){
     edge_iteratore s = z.begin(i);
     while(s != z.end(i)){
       std:cout << "(" << (*s).height << "," << (*s).weight << ")" << std::endl;
       ++s;
     }
   }

   return 0;
}

I' trying to get std::out to a function to generate a multidimensional array so i have implemented this function to get an array,

int createarr(height,width){

     int** ary = new int*[height];
for(int i = 0; i < height; ++i)
    ary[i] = new int[weight];

}

but nothing works, how can i return an multidimensional array to use it in another function call instead of outputting it to the screen.

  • What are: `zoro`, `getfromstream`,`edge_iteratore`, `nnodes`, `height`, `weight`? Please provide a [MCVE](https://stackoverflow.com/help/mcve). –  May 25 '17 at 03:49
  • @RawN this is an implementation of dijsktra, zoro means class where reading that ggg.grf file part goes . I want to know how to return a multidimensional array instead of cout statement. – Mahmudul Islam May 25 '17 at 03:55
  • A good place to start: [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). –  May 25 '17 at 03:58
  • @RawN Thanks for the references but, i need small help :) how can i return an multidimensional array to use it in another function call instead of outputting it to the screen – Mahmudul Islam May 25 '17 at 04:16

2 Answers2

1

If height and weight are constexpr values you know at compile time, declare std::array<<std::array<int>, weight>, height>. This gets you locality of reference. If they are values you compute at runtime or that could vary, use vector<vector<int>>(height) and initialize each row. Then the compiler takes care of freeing the memory for you. If only one is fixed, you can also do a vector of arrays or an array of vectors.

It’s unfortunate that, because of the legacy char** argv interface of main(), every beginning C and C++ programmer thinks that’s how you do a two-dimensional array. A ragged array like that is almost never what you really want. But if you do, use std::vector to manage the memory for you.

The problem with your createarr() as written is that it doesn’t return any array pointer, but RAII is more likely what you want. And if you do have a sparse matrix that would benefit from raggedness, you can use a format like Compressed Sparse Row.

Davislor
  • 14,674
  • 2
  • 34
  • 49
  • sorry i didn't get it :( how can i return an multidimensional array to use it in another function call instead of outputting it to the screen – Mahmudul Islam May 25 '17 at 04:19
  • With this approach, you don’t need to. But you can initialize a temporary `std::vector` inside a function and return that efficiently, due to the move constructor. You would want to create a `std::array` inside the scope that uses it and pass a reference to it to the function that fills it. To dynamically allocate a `std::array` and still get RAII, you can return a `std::unique_ptr>`.` – Davislor May 25 '17 at 04:24
  • Basically, your function that creates the array needs to return some kind of object (or pointer, if you must) that references it. – Davislor May 25 '17 at 04:26
  • I think you're talking over his head. The reason you and I like std vector is because we don't like memory leaks, but you still need to understand pointers, which he clearly doesn't. – Ron Thompson May 25 '17 at 04:27
1

You need to return a pointer to the created array.

But you'll need to somehow deal with the fact that this is dynamically allocated memory, ie, you need to release it when you're done.

And you'll need to somehow encapsulate the dimensions too, which then means it needs to be a struct.

When you finally get tired of dealing with memory leaks, and understand how pointers work, use std::vector like the other guy said.

In the meantime, don't try to implement complicated algorithms in C without understanding the basics first. Brush up hard on how pointers and arrays actually work, first.

Ron Thompson
  • 1,086
  • 6
  • 12