This is probably a question that I used to answer a lot of questions in csdn. My answer is generally in three ways: (1) using vector, (2) first assign a pointer array and then point each pointer to an array. The advantage of this method is that it is intuitive to access array elements, you can use the expression a [x] [Y]. The disadvantage is that it is equivalent to a sawtooth array in C #, and the memory space is not continuous. (3) directly allocate a one-dimensional array x * y to ensure that the space is continuous, but the access to array elements is not intuitive. For my "classic" answer, I was still very proud at that time. At least from the point of view, this answer is still very effective.
I saw a post on the chinaunix forum today, proving that I am not very familiar with C ++.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void **darray_new(int row, int col, int size)
{
void **arr;
arr = (void **) malloc(sizeof(void *) * row + size * row * col);
if (arr != NULL)
{
void *head;
head = (void *) arr + sizeof(void *) * row;
memset(arr, 0, sizeof(void *) * row + size * row * col);
while (row--)
arr[row] = head + size * row * col;
}
return arr;
}
void darray_free(void **arr)
{
if (arr != NULL)
free(arr);
}
Well, the memory is allocated continuously and can be accessed in a [x] [Y] mode! It is a wonderful way to dynamically allocate two-dimensional arrays! This program is C, and it seems that it is not difficult to change it to C ++ that supports object allocation (but it is estimated that placement new should be used. Well, you need to think about it again ......).
Supplement:
After the test, C ++ has been released: the key point is placement new and the displayed destructor call, which is used to ensure the normal Construction and Analysis of objects.
This implementation also has many disadvantages. For example, the size of the array must be remembered to guarantee the analysis of all objects. However, we can improve the allocation algorithm to save the array size in a bit of space.
Another drawback is that, in terms of syntax, it is easy to mistakenly think that the pointer returned by darray_new is the starting address of the Data zone, which may lead to some logical errors.
# Include <iostream>
# Include <cstdlib>
# Include <New>
Template <typename T>
T ** darray_new (INT row, int col)
{
Int size = sizeof (t );
Void ** arr = (void **) malloc (sizeof (void *) * row + size * row * col );
If (Arr! = NULL)
{
Unsigned char * head;
Head = (unsigned char *) Arr + sizeof (void *) * row;
For (INT I = 0; I <row; ++ I)
{
Arr [I] = head + size * I * Col;
For (Int J = 0; j <Col; ++ J)
New (Head + size * (I * Col + J) T;
}
}
Return (T **) Arr;
}
Template <typename T>
Void darray_free (T ** arr, int row, int col)
{
For (INT I = 0; I <row; ++ I)
For (Int J = 0; j <Col; ++ J)
Arr [I] [J]. ~ T ();
If (Arr! = NULL)
Free (void **) Arr );
}
Supplement
This article is only a technical discussion. In practice, a ready-made solution such as boost: multi_array may be more effective.
Reference: http://blog.csdn.net/lifanxi/archive/2007/06/13/1649840.aspx