LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-21-2004, 01:32 AM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
Unhappy 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
 
Old 03-21-2004, 01:45 AM   #2
tvn
Member
 
Registered: Mar 2004
Posts: 96

Rep: Reputation: 15
tried vector in stl (c++) ?
 
Old 03-21-2004, 02:35 AM   #3
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
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
 
Old 03-21-2004, 05:25 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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)
 
Old 03-22-2004, 02:10 AM   #5
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Unhappy

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)
 
Old 03-22-2004, 02:15 AM   #6
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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 )

Last edited by aluser; 03-22-2004 at 02:16 AM.
 
Old 03-22-2004, 02:19 AM   #7
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Unhappy

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 )
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Double pointers and two dimensional arrays in C kponenation Programming 23 03-16-2011 11:43 AM
Two Dimensional Character Problem Mistro116@yahoo.com Programming 8 11-26-2005 08:13 PM
Bash 2 dimensional array ldp Linux - General 5 07-29-2005 11:29 AM
3 dimensional array help leeach Programming 11 06-15-2005 10:46 AM
error code for pointers and 2-Dimensional arrays shams Programming 1 08-06-2004 10:00 PM


All times are GMT -5. The time now is 05:57 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration