开发者

How do you delete (or fill with specific values) a static n-dimension array?

const int RO开发者_开发技巧WS = 3;
const int COLUMNS = 4;

void fillArray(double a[ROWS][COLUMNS], double value);
void deleteArray(double a[ROWS][COLUMNS]);

int main () {
    double a[ROWS][COLUMNS];
    fillArray(a, 0);
    deleteArray(a);
}


In C++, how do you delete (or fill with specific values) a static n-dimension array?

In C++ we generally do not use arrays. We use std::vector. You can use memset or std::fill to fill the array with specific values. BTW you can use delete on dynamically allocated arrays not on static ones.

 memset( a, 0 ,ROWS * COLUMNS * sizeof( double ));
 or
 std::fill(&a[0][0], &a[0][0]+sizeof(a)/sizeof(double), 0);


You can delete only an object created by new (and that object will be allocated in the heap). What do you mean by "deleting a static POD variable"? It has no sense:

1) It doesn't have any destructor to perform additional tasks before freeing the memory,

2) The stack memory will be "freed" as you exit the current block.

And to set it: either loop, either simple memset(a, 0, sizeof(a)); .

Also, the array in your example is not static.


std::vector is what is generally used for C++ arrays (especially when you're new at it). One of vector's constructors will fill it for you to:

std::vector<type> myVector(initialSize, defaultValue);

If you want multidimensional, you could do a vector of vectors, or boost::multi_array:

boost::multi_array<type, numberOfDimensions> myArray(boost::extents[firstSize][secondSize][thirdSize]);

In that case, you'll need to use the multiple-for-loops approach, because it doesn't seem to have a constructor that does that.

EDIT: Actually you can use std::vector to make a multidimensional array with default values:

std::vector<std::vector<double> > a(3, std::vector<double>(4, 0));

Where 3 is the number of rows, 4 is the number of columns and 0 is the default value.

What it's doing is create a vector of vectors with 3 rows, where the default value for each row is a vector with 4 zeroes.


Filling arrays in C++ is the same as filling them using C, namely nested for loops

int i, j;
for (i = 0; i < ROWS; i++)
    for (j = 0; j < COLS; j++)
        a[i][j] = 0

Arrays aren't "deleted" but they can use free if they've been allocated on the heap (if they've been allocated on the stack within a function, this is unnecessary).

int i;
for (i = 0; i < ROWS; i++)
    free(a[i]);
free(a);


Firstly, the code you posted seems confused. What is it that you think "deleteArray" is supposed to do? 'a' is an auto variable and therefore cannot be deleted or freed.

Secondly, wrap your array in a class. There is a nice one in the FAQ that you can start with, but it can be improved. The first improvement is to use a vector rather than newing a block of memory. Then std::fill can be used to fill the array.


Use std::fill

#include <algorithm>

And then your implementation is simply:

std::fill(&a[0][0], &a[0][0]+sizeof(a)/sizeof(a[0][0], value);

You don't delete the array since it is stack allocated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜