arrays - How to allocate memory C -


i need int matrix[x][x] field allocated, x user input , want rows 1 continous block of memory.

lets user inputs x=5 , when

matrix [1][6] = 1  

i want

matrix [2][0] = 1 

anyone can me ?

may suggest different approach:

allocate 1-dimensional data-structure holds data in "line". write get-function linearizes these coordinates.

class linearmatrix{     int dim;     int* memory;  public:     linearmatrix(int d) : dim(d) {         memory = (int*) malloc(dim*dim*sizeof(int));         // insert code fill matrix     }      int get(int x, int y){         int index = x*dim + y;         return memory[index];     }      void set(int x, int y, int value){         int index = x*dim + y;         memory[index] = value;     }      ~linearmatrix(){         free(memory);     } }; 

the memory continous way , allows insert checks if coordinates ok. of course, have remove \n characters matrix string raw numbers in array.

the linearmatrix used somehow this:

linearmatrix matrix(5); // make sure matrix gets filled somehow // these 2 calls access same memory location matrix.set(1,6) = 1; matrix.set(2,1) = 1; 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -