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

F

Frederick Gotham

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?
 
M

Mark P

Frederick said:
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
 
R

red floyd

Frederick said:
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.
 
G

Greg Comeau

Frederick said:
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]
 
R

Rolf Magnus

Frederick said:
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;
}
 
G

Gianni Mariani

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;
}
 
R

Rolf Magnus

Gianni said:
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.
 
G

Greg Comeau

Frederick said:
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 :)
 
F

Frederick Gotham

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 T> struct 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.
 
G

Greg Comeau

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 T> struct 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;
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top