473,396 Members | 1,865 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,396 software developers and data experts.

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


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
9 1997
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vic Spainhower | last post by:
Hello, I am new to php and MySQL and I'm attempting to run a select query on a MySQL database which is working fine except prior to displaying the table with the results from the select query it...
5
by: Eric A. Forgy | last post by:
Hello, I am just learning Java and am trying to write a method that does something like //=========================================== public Static List find(double array,double val,String...
3
by: asognoth | last post by:
Hi... Does anybody have a clue how to solve this problem? the task is to change the following method in that manner that it draws the (calculated) image form left to right instead of from top...
6
by: Jerry Spence1 | last post by:
I'm going mad here. I know about creating controls at runtime and creating a single event etc. In all the examples I have found they create a button and create an event for it. What I can't fathom...
0
by: Ismail Fatih Yýldýrým | last post by:
I modified the RSACSPSample from MSDN to try out a simple commutative encryption model using RSA encryption but when i run the progrem the first encryption command works but during the second...
8
by: Peter | last post by:
I will excute the code below to get MethodName array. These codes are edited under VS2003. Now I want to upgrade it to VS2005,but VS2005 gives me a varning message: The varible "sSourceMethodName"...
7
by: r_ahimsa_m | last post by:
Hello, I am learning JavaScript. I have a table on HTML page:                 <table id="announcement_fields" border="0">                 <tbody>                 <tr>...
11
by: moltendorf | last post by:
Hey everyone, as I have been by far pleased with some of the top helpers on this forum, I have decided to send another one your way. Even though I am personally having minimal issues with this...
4
by: Gilles Ganault | last post by:
Hello I'm puzzled as to why PHP (5) prints this (under FreeBSD 6.3): ========= #!/usr/local/bin/php <?php $dbh = new PDO("sqlite:test.sqlite"); $sql = "CREATE TABLE IF NOT EXISTS calls...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.