473,327 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

new operator

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 !


Nov 17 '05 #1
2 1364


Hai Ly Hoang wrote:
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.

Nov 17 '05 #2
"Hai Ly Hoang" <No*****@Mail.com> wrote in message news:<#O**************@TK2MSFTNGP12.phx.gbl>...
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[i][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.
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.