-1

I need int matrix[x][x] field to be allocated, x is user input and I want the rows to be one continous block of memory.

Lets say user inputs x=5 and when I do this

matrix [1][6] = 1 

I want it to do this

matrix [2][0] = 1

Anyone can help me with this ?

francis
  • 9,525
  • 2
  • 25
  • 41
  • What is your final goal? – lodo Mar 28 '15 at 14:28
  • i have a text file in which is matrix like 00101\n01011\n01011\n01011\n01010\n and i need it to be loaded into matrix [5][5] user first writes the matrix into text file then run program and input number of rows – Daniel Glos Mar 28 '15 at 14:29
  • Do you want to allocate with "0"'s or just dynamically allocate an array? You kind of lost me with the matrix[2][0]=1 part.. – Ross Bush Mar 28 '15 at 14:30
  • Do you need help in allocating `matrix`? Do you really need the kind of indexing you're describing? – lodo Mar 28 '15 at 14:32
  • Is it just that you want it to wrap (since the 6 would otherwise be beyond the end of that array)? – TravisJ Mar 28 '15 at 14:34
  • If you use C++ you can create specific classes and operators for this. Otherwise, you can create a simple C function to transform "wrong" indexes into correct ones. – lodo Mar 28 '15 at 14:34
  • well no i dont i have solved it that the matrix in text file is just one continous line but whne i want it to be formated im not able to load it properly into matrix in program for calculations – Daniel Glos Mar 28 '15 at 14:36
  • yes exactli i otherwise it will be out of range but i want it to be in continous block of memory so it will wrap to next line – Daniel Glos Mar 28 '15 at 14:37
  • "when I do `matrix [1][6] = 1` I want it to do `matrix [2][0] = 1`??? Do you even understand yourself what it is that you want to do? – barak manos Mar 28 '15 at 15:11
  • shouldn't it be that `matrix [1][6]` is equal to `matrix[2][1]`? If you think of a linearized access, now both point to the 11th element – Slizzered Mar 28 '15 at 15:40

2 Answers2

2

May I suggest a slightly different approach:

Allocate a 1-dimensional data-structure that holds the data in a "line". Then write a get-function that linearizes these coordinates.

class linearMatrix{
    int dim;
    int* memory;

public:
    linearMatrix(int d) : dim(d) {
        memory = (int*) malloc(dim*dim*sizeof(int));
        // insert code to fill the 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 will be always continous this way and it allows to insert some checks if coordinates are really OK. Of course, you have to remove all the \n characters from your matrix string so that only the raw numbers are in the array.

The linearMatrix would be used somehow like this:

linearMatrix matrix(5);
// make sure the matrix gets filled somehow
// these 2 calls access the same memory location
matrix.set(1,6) = 1;
matrix.set(2,1) = 1;
Slizzered
  • 869
  • 2
  • 9
  • 23
0

I suggest the following way to allocate the array, so as to have all elements contiguous in memory :

int n;
char** array2d=malloc(n*sizeof(char*));
if(array2d==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
array2d[0]=malloc(n*n*sizeof(char));
if(array2d[0]==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
int i;
for (i = 1 ; i < n ; i++)
{
    array2d[i] =& array2d[0][i*n];
}

The elements can be acceded by array2d[i][j].

A 2D array is an array of pointers to starts of rows, all items being allocated by a single call to malloc().

This way to allocate memory is useful if the data is to by treated by libraries such as fftw or lapack. The pointer to the data is array[0].

Indeed, writing array2d[0][n]=42 or array2d[1][0]=42 performs the same thing !

See :

In a function :

int alloc2d(int n,char ***array2d){
    *array2d=malloc(n*sizeof(char*));
    if(*array2d==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
    (*array2d)[0]=malloc(n*n*sizeof(char));
    if((*array2d)[0]==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
    int i;
    for (i = 1 ; i < n ; i++)
    {
        (*array2d)[i] =& (*array2d)[0][i*n];
    }
    return 0;
}

called from main by char** array2d;alloc2d(42,&array2d);

Test code :

#include <stdio.h>
#include <stdlib.h>

int alloc2d(int n,char ***array2d){
    *array2d=malloc(n*sizeof(char*));
    if(*array2d==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
    (*array2d)[0]=malloc(n*n*sizeof(char));
    if((*array2d)[0]==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
    int i;
    for (i = 1 ; i < n ; i++)
    {
        (*array2d)[i] =& (*array2d)[0][i*n];
    }
}

int free2d(char **array2d){
    free(array2d[0]);
    free(array2d);
    return 0;
}

int main()
{
    int n=42;
    char** array2d;
    alloc2d(n,&array2d);

    array2d[0][n]=13;
    printf("%d\n",array2d[1][0]);
    free2d(array2d);
    return 0;
}

Compiled by gcc main.c -o main. As expected, output is 13. A function to free the memory was added.

Community
  • 1
  • 1
francis
  • 9,525
  • 2
  • 25
  • 41