0

I'm hitting an odd segmentation fault that is happening somewhere and I was wondering whether it could be due to the way I allocated the matrix array of pointers.

It's declared as such in the .h file:

int **matrix;

But when I pass it, I am using it in this way int *matrix[], in order to access individual rows with matrix[i] (this made a lot of my tasks simpler).

So, when I am allocating the matrix, should I have done:

matrix = new int * [vertices];
    for (int i = 0; i < vertices; i++)
         matrix[i] = new int[vertices];

Or for the third line, should I use the -> operator:

   matrix[i]-> new int[vertices];  // Or something like this.

And what is the difference between the two?

Rudolf Real
  • 1,948
  • 23
  • 27
Ashley
  • 27
  • 5
  • Can you post more code, like the function that receives the matrix argument? And maybe post a small, compilable example. –  Aug 19 '14 at 01:00
  • 3
    One compiles and the other doesn't? – David G Aug 19 '14 at 01:01
  • The file is huge, this is just sections of it that I am worried may be causing the fault. @0x499602D2 well, yes, hence the comment next to it... – Ashley Aug 19 '14 at 01:03
  • I think you're looking for the operator `*` rather than `->` in the latter example. – OMGtechy Aug 19 '14 at 01:04
  • `matrix[i]= new int[vertices];` is fine. I suggest compiling and running it. – Code-Apprentice Aug 19 '14 at 01:19
  • Just to clarify C++: the operator -> means (*a).b On the other hand matrix[i] is the position i, you don't need anything else there – Rudolf Real Aug 19 '14 at 01:24
  • @FabricioPH: When `matrix` is (or decays to) a pointer, then `matrix[i]` is the same as `*(matrix + i)`. – Bill Lynch Aug 19 '14 at 01:27
  • 4
    I'd approach the job task differently. http://stackoverflow.com/a/6465254/179910 – Jerry Coffin Aug 19 '14 at 01:33
  • As an option to an array of pointers for a matrix, you could do a single allocation for the matrix and set a pointer to the first row of the matrix: int (*pmatrix)[vertices] = (int( * )[vertices]) new int[vertices*vertices]; ... delete [] pmatrix; This makes pmatrix[0] a pointer to the first row of the patrix, pmatrix[1] a pointer to the second row, without using an array of pointers. – rcgldr Aug 19 '14 at 03:13
  • is that a cast operation @rcgldr? – Ashley Aug 19 '14 at 05:50
  • Separating the statements: int (*pmatrix)[vertices]; - this declares a pointer to an array of (vertices) integers. Then pmatrix = (int ( * ) [vertices]) new int[vertices*vertices] ; allocates vertices^2 (squared) integers (to be used as a matrix of vertices rows by vertices columns), and casts the pointer returned by new to be compatible with pmatrix. – rcgldr Aug 19 '14 at 06:01

1 Answers1

0

The first option you suggested is completely fine in this case. But the second one with the -> operator is not even a valid syntax. The first line from the first suggestion actually creates an array of int* of size vertices:

matrix = new int * [vertices];

Each of the elements of this array is initialised with some garbage values. You can make sure it is initialised with zero by using braces like this: new int * [vertices](). But in either case accessing any of the pointers by matrix[i]-> would be meaningless. In fact the arrow operator -> is a dereference operator that is used exclusively with pointers to objects that have members. Hope that helps.