new operator

  • Thread starter Thread starter Hai Ly Hoang
  • Start date Start date
H

Hai Ly Hoang

Hi,
int *(*arr)[5];
a is a pointer to an element (e1). e1 is an array of 5 integer pointer.
Now i want to to allocate for arr. How to do that ?

int *(**arr2)[3];
and arr2 = new (????).
arr2 is now an array of pointer p1. Each pointer p1 point to an object e.
Each e1 is an array of pointers which type is integer pointer. Is it true ?
What is the syntax of "new " operator ?

Thanks alot !
 
Hai said:
Hi,
int *(*arr)[5];
a is a pointer to an element (e1). e1 is an array of 5 integer pointer.
Now i want to to allocate for arr. How to do that ?

int *(**arr2)[3];
and arr2 = new (????).
arr2 is now an array of pointer p1. Each pointer p1 point to an object e.
Each e1 is an array of pointers which type is integer pointer. Is it true ?
What is the syntax of "new " operator ?

See John's post. C++ is a low level language. It only supports simple
constructs while high level languages support abstractions like what you
are trying to do. Abstractions are created with classes in C++, they
have to be written. That's why he suggested the MultiArray class.

You could:
int ar( int *ar, int a, int b ) { return ar[ a * b ]; }
#define ax( a, b ) au[ a * b ]

then:
int i= 3, j= 5;
int *au= new int[i*j];
and:
int h= ar( au, 2, 3 );
or:
int w= ax( 3, 4 );
or even:
int x= au[ 3 * 2 ]; //back to where you started!

But this is a very dangerous practice as well as very limited.
There is no bounds checking.

Start using classes in C++ or you will not be happy with the language.

It also means you can use a class that is applicable to your needs. If
you plan to do linear algebra with your arrays, you can use a class with
all the matrix math built in!
Thanks alot !

Best, Dan.
 
Hai Ly Hoang said:
Hi,
int *(*arr)[5];
a is a pointer to an element (e1). e1 is an array of 5 integer pointer.
Now i want to to allocate for arr. How to do that ?
The following won't work:
arr = new int*[5];
because it will allocate an array of five elements of type int*,
not an element of type array of five pointers to int. Yuk!

You can try with parentheses:
arr = new (int*[5]);
And it'll be the same.

It is probably easier with typedef:
typedef int *arr_t[5];
arr_t *arr;
arr = new arr_t;

But it won't work either.

Firstly I thought it was a compiler bug, but then, seeking in the C++
standard I found the following quote:

5.3.4.6: "When the allocated object is an array [...], the
newexpression
yields a pointer to the initial element (if any) of the
array. [Note: both new int and new int[10] have type int* and the type
of new int[10] is int (*)[10]. "

You can workaround this using
arr = new arr_t[1];

But you must remember the [] at the delete
delete[] arr;

Or maybe better:
struct foo { arr_t x; };
foo *new foo;
int *(**arr2)[3];
and arr2 = new (????).
arr2 is now an array of pointer p1. Each pointer p1 point to an object e.
Each e1 is an array of pointers which type is integer pointer. Is it true ?
Not exactly. arr2 is a pointer to pointers to type p1. p1 is an array
of 3 pointers to int.
What is the syntax of "new " operator ?
arr2 = new (int *(*)[3]);

This compiles fine because the type is not an array, but a pointer to
an array.

HTH.
Bobo.
 
Back
Top