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 |
tried vector in stl (c++) ?
|
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 |
I use the following now and then:
Code:
|
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:
|
Code:
char** rowpointers = new_2d(ROWS, COLS); (Of course, you would initialize the array somewhere before reading from it ;) ) |
Thanks, aluser buddy!
It is a great idea! Best regards, George Quote:
|
All times are GMT -5. The time now is 07:44 AM. |