By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

New'ing an "array" -- or is it... ?

P: n/a

What are your thoughts on the following code?

typedef int Coords[3];

int main()
{
Coords *const p = new Coords;

delete p;
}

It doesn't compile with g++ on my system, saying:

cannot convert `int*' to `int (* const)[3]' in initialization

Half of me thinks that Coords is just an array in disguise and that the error
is justified, and the other half thinks that Coords is a new fully-fledged
type and that the code should be OK.

Thoughts?

--

Frederick Gotham
Sep 29 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Frederick Gotham wrote:
What are your thoughts on the following code?

typedef int Coords[3];

int main()
{
Coords *const p = new Coords;

delete p;
}

It doesn't compile with g++ on my system, saying:

cannot convert `int*' to `int (* const)[3]' in initialization

Half of me thinks that Coords is just an array in disguise and that the error
is justified, and the other half thinks that Coords is a new fully-fledged
type and that the code should be OK.

Thoughts?
This seems consistent with the general behavior of typedefs in C++ as
aliases for a type rather than new types unto themselves. Similar
reasoning explains why no conversion is necessary to do something like
the following:

typedef int MyInt;
int i = 1;
MyInt mi = i;

Mark
Sep 29 '06 #2

P: n/a
Frederick Gotham wrote:
What are your thoughts on the following code?

typedef int Coords[3];

int main()
{
Coords *const p = new Coords;

delete p;
}

It doesn't compile with g++ on my system, saying:

cannot convert `int*' to `int (* const)[3]' in initialization

Half of me thinks that Coords is just an array in disguise and that the error
is justified, and the other half thinks that Coords is a new fully-fledged
type and that the code should be OK.
Not sure, but Comeau online doesn't like it either, for pretty much the
same reason.
Sep 29 '06 #3

P: n/a
In article <t7***************@newssvr11.news.prodigy.com>,
red floyd <no*****@here.dudewrote:
>Frederick Gotham wrote:
>What are your thoughts on the following code?

typedef int Coords[3];

int main()
{
Coords *const p = new Coords;

delete p;
}

It doesn't compile with g++ on my system, saying:

cannot convert `int*' to `int (* const)[3]' in initialization

Half of me thinks that Coords is just an array in disguise and that the error
is justified, and the other half thinks that Coords is a new fully-fledged
type and that the code should be OK.

Not sure, but Comeau online doesn't like it either, for pretty much the
same reason.
Remember new does not return a pointer to the whole memory
just to the first element. IOWs, for new int, it returns
an int *, and for a new int[3] it still returns an int *,
but p is an int (*const)[3]
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Sep 30 '06 #4

P: n/a
Frederick Gotham wrote:
>
What are your thoughts on the following code?

typedef int Coords[3];

int main()
{
Coords *const p = new Coords;

delete p;
}

It doesn't compile with g++ on my system, saying:

cannot convert `int*' to `int (* const)[3]' in initialization

Half of me thinks that Coords is just an array in disguise and that the
error is justified, and the other half thinks that Coords is a new
fully-fledged type and that the code should be OK.

Thoughts?
Despite the name, typedef never defines a type. It just gives an alternative
name for an existing one. So your code is equivalent to:

int main()
{
int (*const p)[3] = new Coords[3];

delete p;
}
Sep 30 '06 #5

P: n/a
Rolf Magnus wrote:
....
>
Despite the name, typedef never defines a type. It just gives an alternative
name for an existing one. So your code is equivalent to:

int main()
{
int (*const p)[3] = new Coords[3];

delete p;
}
typo alert - I think you meant:

int main()
{
int (*const p)[3] = new int[3];

delete p;
}
The correct code obviously should look like:

int main()
{
int * const p = new int[3];

delete [] p;
}
Sep 30 '06 #6

P: n/a
Gianni Mariani wrote:
Rolf Magnus wrote:
...
>>
Despite the name, typedef never defines a type. It just gives an
alternative name for an existing one. So your code is equivalent to:

int main()
{
int (*const p)[3] = new Coords[3];

delete p;
}

typo alert - I think you meant:

int main()
{
int (*const p)[3] = new int[3];

delete p;
}
Yes. Thanks for the correction.

Sep 30 '06 #7

P: n/a
In article <ef*************@news.t-online.com>,
Rolf Magnus <ra******@t-online.dewrote:
>Frederick Gotham wrote:
>What are your thoughts on the following code?

typedef int Coords[3];

int main()
{
Coords *const p = new Coords;

delete p;
}

It doesn't compile with g++ on my system, saying:

cannot convert `int*' to `int (* const)[3]' in initialization

Half of me thinks that Coords is just an array in disguise and that the
error is justified, and the other half thinks that Coords is a new
fully-fledged type and that the code should be OK.

Thoughts?

Despite the name, typedef never defines a type. It just gives an alternative
name for an existing one. So your code is equivalent to:

int main()
{
int (*const p)[3] = new Coords[3];

delete p;
}

int (*const p)[3] = new int[3];

Unless you meant ]; as a smiley :)
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Sep 30 '06 #8

P: n/a
Frederick Gotham posted:
typedef int Coords[3];

int main()
{
Coords *const p = new Coords;

delete p;
}
Or how about this slightly more complicated one:

template<class Tstruct TypeProcurer {typedef T Type;};

typedef TypeProcurer<int[3]>::Type Coord;

int main()
{
Coord *const p = new Coord;

delete p;
}

The overall impression I'm getting from this is that it's Bad News to make
a typedef pointer or array, as it gives the false impression that you're
dealing with a normal object.

--

Frederick Gotham
Sep 30 '06 #9

P: n/a
In article <PB*******************@news.indigo.ie>,
Frederick Gotham <fg*******@SPAM.comwrote:
>Frederick Gotham posted:
> typedef int Coords[3];

int main()
{
Coords *const p = new Coords;

delete p;
}

Or how about this slightly more complicated one:

template<class Tstruct TypeProcurer {typedef T Type;};

typedef TypeProcurer<int[3]>::Type Coord;

int main()
{
Coord *const p = new Coord;

delete p;
}

The overall impression I'm getting from this is that it's Bad News to make
a typedef pointer or array, as it gives the false impression that you're
dealing with a normal object.
Even the non-typedef has that problem. This has existed in C
since day 1:

char c;
char array[99];
char *p;

p = &c;
p = array;
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Sep 30 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.