开发者

allocation of a pointers to fixed size arrays

开发者 https://www.devze.com 2023-04-12 06:33 出处:网络
I have 2 doubts regarding basics of pointers usage. With the following code int (*p_b)[10]; p_b = new int[3][10];

I have 2 doubts regarding basics of pointers usage.

With the following code

 int (*p_b)[10];
 p_b = new int[3][10];
 // ..do my stuff
 delete [] p_b

p_b is pointing to an array of 3 elements, each having fixed-size length of 10 int.

Q1:

How to declare p_b if I want that each element be a pointer to a fixed array size? Basically I want the following

  p_b[0] = pointer to a fixed-array s开发者_开发知识库ize of 10
  p_b[1] = pointer to a fixed-array size of 10
  // ... and so on

I was thinking to int (** p_b)[10] but then I don't know how to use new to allocate it? I would like to avoid falling back to more general int** p_b

Q2:

Is per my original code sample above, how to call new so that p_b points to a unique fixed-size array of 10 int other than calling p_b = new int[1][10] ? To free memory I have to call delete[] while I cannot find an expression where I can call only simply delete.


p_b is pointing to an array of 3 elements, each having fixed-size length of 10 int.

How to declare p_b if I want that each element be a pointer to a fixed array size?

Does your first sentence not completely cover that question?

Is per my original code sample above, how to call new so that p_b points to a unique fixed-size array of 10 int other than calling p_b = new int[1][10]? To free memory I have to call delete[] while I cannot find an expression where I can call only simply delete.

I completely do not understand why this is a problem, but you could do it by wrapping your array inside another type... say std::array, boost::array or std::vector.


First of all, if your new expression has square brackets (new somtype[somesize]), your delete has to have square brackets as well (delete [] your_pointer).

Second, right now you've defined p_b to be a single pointer to some data. If what you really want is an array of pointers, then you need to define it as an array. Since you apparently want three independent arrays, you'll have to allocate each of them separately. It's probably easiest if you start with a typedef:

typedef int *p_int;
p_int p_b[3];

Then you'll allocate your three arrays:

for (int i=0; i<3; i++)
    p_b[i] = new int[10];

To delete those, you'll need to delete each one separately:

for (int i=0; i<3; i++)
    delete [] p_b[i];

I definitely agree with @Tomalak that you should almost never mess with things like this yourself though. It's not clear what you really want to accomplish, but it's still pretty easy to guess that chances are quite good that a standard container is likely to be a simpler, cleaner way to do it anyway.


Here's an example of how to implement Q1:

int main()
{
    typedef int foo[10];

    foo* f = new foo[3];

    f[0][5] = 5;
    f[2][7] = 10;

    delete [] f;
}

As for Q2, the only way to delete memory allocated with new[] is with delete[]. If you personally don't want to write delete [], you can use a vector or another STL container. Really, unless this is some hardcore uber-optimisation, you should be using vectors anyway. Never manage memory manually unless you are absolutely forced to.


To use a raw pointer to manage a 2-d array you must first create a pointer to a pointer to array element type that will point to each row of the array. Next, each row pointer must be assigned to the actual array elements for that row.

int main()
{
  int **p;

  // declare an array of 3 pointers
  p = new int*[3];

  // declare an array of 10 ints pointed to by each pointer
  for( int i = 0; i < 3; ++i ) {
    p[i] = new int[10];
  }

  // use array as p[i][j]

  // delete each array of ints
  for( int i = 0; i < 3; ++i ) {
    delete[] p[i];
  }

  // delete array of pointers
  delete[] p;
}

A far easier solution is to use std::array. If your compiler does not provide that class you can use std::vector also.

std::array<std::array<int,10>,3> myArr; 
myArr[0][0] = 1;


For Q1, I think you want

int (*p[3])[10];

Try cdecl when you're unsure.

Your other question seems to be well answered by other answers.

regards, Yati Sagade


Actually, nobody posted an answer to your exact question, yet.

Instead of

int (*p_arr)[10] = new int[3][10];
// use, then don't forget to delete[]
delete[] p_arr;

I suggest using

std::vector<std::array<int, 10>> vec_of_arr(3);

or if you don't need to move it around and don't need runtime length:

std::array<std::array<int, 10>, 3> arr_of_arr;

Q1

How to declare p_b if I want that each element be a pointer to a fixed array size?

int(**pp_arr)[10] = new std::add_pointer_t<int[10]>[3];
for (int i = 0; i < 3; ++i)
    pp_arr[i] = new int[1][10];
// use, then don't forget to delete[]
for (int i = 0; i < 3; ++i)
    delete[] pp_arr[i];
delete[] pp_arr;

The modern variant of that code is

std::vector<std::unique_ptr<std::array<int, 10>>> vec_of_p_arr(3);
for (auto& p_arr : vec_of_p_arr)
    p_arr = std::make_unique<std::array<int, 10>>();

or if you don't need to move it around and don't need runtime length:

std::array<std::unique_ptr<std::array<int, 10>>, 3> arr_of_p_arr;
for (auto& p_arr : arr_of_p_arr)
    p_arr = std::make_unique<std::array<int, 10>>();

Q2

Is per my original code sample above, how to call new so that p_b points to a unique fixed-size array of 10 int other than calling p_b = new int[1][10]?

Not without wrapping the array into another type.

std::array<int, 10>* p_arr = new std::array<int, 10>;
// use, then don't forget to delete
delete p_arr;

You can replace std::array<int, 10> with your favourite array-wrapping type, but you cannot replace it with a fixed-size array alias. The modern variant of that code is:

auto p_arr = std::make_unique<std::array<int, 10>>();
0

精彩评论

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

关注公众号