c++ - Allocating an array of pointers -


i'm hitting odd segmentation fault happening somewhere , wondering whether due way allocated matrix array of pointers.

it's declared such in .h file:

int **matrix; 

but when pass it, using in way int *matrix[], in order access individual rows matrix[i] (this made lot of tasks simpler).

so, when allocating matrix, should have done:

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

or third line, should use -> operator:

   matrix[i]-> new int[vertices];  // or this. 

and difference between two?

the first option suggested fine in case. second 1 -> operator not valid syntax. first line first suggestion creates array of int* of size vertices:

matrix = new int * [vertices]; 

each of elements of array initialised garbage values. can make sure initialised 0 using braces this: new int * [vertices](). in either case accessing of pointers matrix[i]-> meaningless. in fact arrow operator -> dereference operator used exclusively pointers objects have members. hope helps.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -