Gow i can free allocated by new [1][100]

G

Grizlyk

Hello.

Consider:

enum{ items=100 };
typedef int * (*my_type)[items];


How to delete the array allocated with size == 1?

my_type=new[1][items];
delete[] my_type;
delete my_type;

How to delete the array with size =!1 ?

my_type=new[items][items];
delete[] my_type;
delete my_type;


--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
 
G

Grizlyk

Sorry, i was hurry to post code combined from two different examples.
Consider:

enum{ items=100 };
typedef int * my_type[items];
How to delete the array allocated with size == 1?

my_type *ptr1=new int* [1][items];
my_type *ptr2=new my_type[1];

delete[] ptr1;
delete[] ptr2;

How to delete the array with size =!1 ?

my_type *ptr1=new int* [items][items];
my_type *ptr2=new my_type[items];
delete[] ptr1;
delete[] ptr2;

What does it mean:

my_type *ptr=new int* [0][items];

--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
 
O

Old Wolf

Sorry, i was hurry to post code combined from two different examples.
Consider:
enum{ items=100 };
typedef int * my_type[items];

Note, "my_type" is a typedef for an array type. The array has
100 elements, each of which is a pointer to int.
How to delete the array allocated with size == 1?

my_type *ptr1=new int* [1][items];
my_type *ptr2=new my_type[1];

Both of these lines have the same effect. ptr1 is a pointer
to the first and only element of an array of length 1. The
element of this array is itself an array, having 100 elements
each of which are of type (int *).
delete[] ptr1;
delete[] ptr2;

That is correct

I guess you mean, size != 1
my_type *ptr1=new int* [items][items];
my_type *ptr2=new my_type[items];
delete[] ptr1;
delete[] ptr2;

That is also correct. delete[] deletes an array of any dimension,
there's nothing special about an array size of 1, nor that the
elements of the array are themselves arrays.
What does it mean:

my_type *ptr=new int* [0][items];

Ah, here is the array of size = !1 :)

Either throws a bad_alloc exception, or returns a non-null
pointer (which it would be illegal to dereference). If you
haven't overloaded operator new() then the returned pointer
is different to any other pointer to valid memory.

NB. The standard doesn't clearly say that this can throw,
but I assume that was the intent.
 
G

Grizlyk

Old Wolf write:
Sorry, i was hurry to post code combined from two different examples.
Consider:
enum{ items=100 };
typedef int * my_type[items];

Note, "my_type" is a typedef for an array type. The array has
100 elements, each of which is a pointer to int.

Yes, I need array of pointers.
How to delete the array allocated with size == 1?

my_type *ptr1=new int* [1][items];
my_type *ptr2=new my_type[1];

Both of these lines have the same effect. ptr1 is a pointer
to the first and only element of an array of length 1. The
element of this array is itself an array, having 100 elements
each of which are of type (int *).
delete[] ptr1;
delete[] ptr2;

That is correct

The question was is array of size==1 the same as not array? As i can
understand, the following expression:

new int* [items][items][items]

will call single "operator new[]" with required total solid area of memory
of size: "items*items*items" of "sizeof(int*)". The data order in array as
[z][y][x] is just software convention. So in my case of "1*items" it looks
like just "items".

But now i see the "[1]" can be runtime value and i can not do like this:

// formally it is unexpected error
// if my_type is kind of template parameter
my_type *ptr3=new my_type;

instead of

int **ptr4=new my_type;

so i need call delete[] here, not delete or make structure and place my_type
into structure.
I guess you mean, size != 1

Sorry, it was deep evening for me and i was asking "with last forces".
my_type *ptr1=new int* [items][items];
my_type *ptr2=new my_type[items];
delete[] ptr1;
delete[] ptr2;

That is also correct. delete[] deletes an array of any dimension,
there's nothing special about an array size of 1, nor that the
elements of the array are themselves arrays.
What does it mean:

my_type *ptr=new int* [0][items];

Ah, here is the array of size = !1 :)

Either throws a bad_alloc exception, or returns a non-null
pointer (which it would be illegal to dereference). If you
haven't overloaded operator new() then the returned pointer
is different to any other pointer to valid memory.

NB. The standard doesn't clearly say that this can throw,
but I assume that was the intent.

Thanks.

--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top