L'utilizzo del doppio puntatore è di gran lunga il miglior compromesso tra velocità di esecuzione/ottimizzazione e leggibilità. Usare un singolo array per memorizzare i contenuti della matrice è in realtà ciò che fa un doppio puntatore.
Ho usato con successo la seguente funzione di creazione di modelli (sì, so di usare il vecchio puntatore di stile C che fa riferimento, ma rende il codice più chiaro sul lato chiamante per quanto riguarda i parametri di modifica - qualcosa che mi piace dei puntatori che non è possibile con i riferimenti vedrete cosa intendo):.
///
/// Matrix Allocator Utility
/// @param pppArray Pointer to the double-pointer where the matrix should be allocated.
/// @param iRows Number of rows.
/// @param iColumns Number of columns.
/// @return Successful allocation returns true, else false.
template <typename T>
bool NewMatrix(T*** pppArray,
size_t iRows,
size_t iColumns)
{
bool l_bResult = false;
if (pppArray != 0) // Test if pointer holds a valid address.
{ // I prefer using the shorter 0 in stead of NULL.
if (!((*pppArray) != 0)) // Test if the first element is currently unassigned.
{ // The "double-not" evaluates a little quicker in general.
// Allocate and assign pointer array.
(*pppArray) = new T* [iRows];
if ((*pppArray) != 0) // Test if pointer-array allocation was successful.
{
// Allocate and assign common data storage array.
(*pppArray)[0] = new T [iRows * iColumns];
if ((*pppArray)[0] != 0) // Test if data array allocation was successful.
{
// Using pointer arithmetic requires the least overhead. There is no
// expensive repeated multiplication involved and very little additional
// memory is used for temporary variables.
T** l_ppRow = (*pppArray);
T* l_pRowFirstElement = l_ppRow[0];
for (size_t l_iRow = 1; l_iRow < iRows; l_iRow++)
{
l_ppRow++;
l_pRowFirstElement += iColumns;
l_ppRow[0] = l_pRowFirstElement;
}
l_bResult = true;
}
}
}
}
}
per de-allocare la memoria creata utilizzando l'utilità di cui sopra, si deve semplicemente a de-allocare in senso inverso.
///
/// Matrix De-Allocator Utility
/// @param pppArray Pointer to the double-pointer where the matrix should be de-allocated.
/// @return Successful de-allocation returns true, else false.
template <typename T>
bool DeleteMatrix(T*** pppArray)
{
bool l_bResult = false;
if (pppArray != 0) // Test if pointer holds a valid address.
{
if ((*pppArray) != 0) // Test if pointer array was assigned.
{
if ((*pppArray)[0] != 0) // Test if data array was assigned.
{
// De-allocate common storage array.
delete [] (*pppArray)[0];
}
}
// De-allocate pointer array.
delete [] (*pppArray);
(*pppArray) = 0;
l_bResult = true;
}
}
}
Per utilizzare queste funzioni modello di cui sopra è quindi molto semplice (ad esempio):
.
.
.
double l_ppMatrix = 0;
NewMatrix(&l_ppMatrix, 3, 3); // Create a 3 x 3 Matrix and store it in l_ppMatrix.
.
.
.
DeleteMatrix(&l_ppMatrix);
fonte
2013-04-17 14:10:48
stato fatto più e più volte: http://stackoverflow.com/questions/365782/how -do-i-best-handle-dynamic-multi-dimensional-array-in-cc http://stackoverflow.com/questions/527887/cc-optimize-data-structures-array-of-arrays-or-just- array e altri – dmckee