LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Looking for a safe and efficient way to use two dimensional arrays. (https://www.linuxquestions.org/questions/programming-9/looking-for-a-safe-and-efficient-way-to-use-two-dimensional-arrays-160455/)

George2 03-21-2004 01:32 AM

Looking for a safe and efficient way to use two dimensional arrays.
 
Hello, everyone!


I am looking for a safe and efficient way to use two dimensional arrays, better a library that I can use in C/C++.

Here is the way that I used before, allocate a pointer array, and then allocate memory pointed by each pointer in the array (size of each memory block pointed by each pointer may be vary and I always use an additional one dimensional array to record each of their size). But I find it is hard to manage (for example, release/free memory), because all the space the two dimensional array using is allocated on the heap and not on the stack, and it is easy to result in memory leak if the management mechanism is not sound. And I want to supply an unified interface to client applications using the library, who uses the memory block as a two dimentional array, for example, A[i][j], and not using it as pointers, for example, *(A + i + j). Maybe to implement that function, I need a wrapper or a middle layer.

What I am looking for is a safe and efficient way to define, allocate space, manipulate and free (free part or all of the two dimensional array). Can anyone help? Are there some source codes or tutorials that I can make a reference?


Thanks in advance,
George

tvn 03-21-2004 01:45 AM

tried vector in stl (c++) ?

George2 03-21-2004 02:35 AM

Thanks, tvn buddy!


STL vector is useful in C++. But sometimes I need to use the same function in a pure C environment. What can I use in a pure C environment?


Have a nice weekend,
George

aluser 03-21-2004 05:25 PM

I use the following now and then:

Code:


char** new_2d(int rows, int cols)
{
    char* data = malloc(rows * cols);
    char** rowpointers = malloc(rows * sizeof(*rowpointers));
    int i;
    if (data == NULL || rowpointers == NULL)
        barf();
    rowpointers[0] = data;
    for (i = 1; i < rows; ++i) {
        rowpointers[i] = rowpointers[i-1] + cols;
    }
    return rowpointers;
}

void free_2d(char** rowpointers)
{
    if (rowpointers == NULL)
        return;
    free(rowpointers[0]);
    free(rowpointers);
}

This way you don't have to malloc each row individually, and you can access your data linearly if that is useful. (by using rowpointers[0] as an array of rows*cols bytes)

George2 03-22-2004 02:10 AM

Thanks, aluser buddy!


Your sample code is helpful. I have another question, how to use "rowpointers" looking as a two dimensional array? I mean, in your sample, you use it as one dimentional array, for example, "rowpointers [i]". I want to know how to use it looking as a two dimentional array, for example, rowpointers [i][j]? Need to add additional wrappers or interface?


Best regards,
George


Quote:

Originally posted by aluser
I use the following now and then:

Code:


char** new_2d(int rows, int cols)
{
    char* data = malloc(rows * cols);
    char** rowpointers = malloc(rows * sizeof(*rowpointers));
    int i;
    if (data == NULL || rowpointers == NULL)
        barf();
    rowpointers[0] = data;
    for (i = 1; i < rows; ++i) {
        rowpointers[i] = rowpointers[i-1] + cols;
    }
    return rowpointers;
}

void free_2d(char** rowpointers)
{
    if (rowpointers == NULL)
        return;
    free(rowpointers[0]);
    free(rowpointers);
}

This way you don't have to malloc each row individually, and you can access your data linearly if that is useful. (by using rowpointers[0] as an array of rows*cols bytes)


aluser 03-22-2004 02:15 AM

Code:

char** rowpointers = new_2d(ROWS, COLS);
char foo = rowpointers[row][col];

That's it : ) It's all set up already.

(Of course, you would initialize the array somewhere before reading from it ;) )

George2 03-22-2004 02:19 AM

Thanks, aluser buddy!


It is a great idea!


Best regards,
George
Quote:

Originally posted by aluser
Code:

char** rowpointers = new_2d(ROWS, COLS);
char foo = rowpointers[row][col];

That's it : ) It's all set up already.

(Of course, you would initialize the array somewhere before reading from it ;) )



All times are GMT -5. The time now is 07:44 AM.