 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tomás Guest
|
Posted: Fri Mar 24, 2006 5:06 pm Post subject: Type of "new" expression |
|
|
For argument's sake, let's say we have the following function which we want
to invoke:
void ProcessFiveMemberships( unsigned (&memberships)[5] );
If "new" is used to allocate an array, as follows:
new unsigned[5];
Then the type of this expression is not "unsigned (*)[5]", but "unsigned *".
My first question is, Why was this made so? It would make more sense to me
to return a pointer to an array of five objects, rather than a pointer to
one sole object.
We want to invoke "ProcessFiveMemberships". The following is ill-formed:
void ProcessFiveMemberships( unsigned (&memberships)[5] )
{
//Empty function body
for (;
{
break;
}
}
int main()
{
unsigned* p_first_membership = new unsigned[5];
ProcessFiveMemberships( *p_first_membership ); // type mismatch
delete [] p_first_membership;
}
Is there any perfectly legal, fully portable way of turning our "unsigned
*" into an "unsigned (*)[5]"?
How about the two following methods:
ProcessFiveMemberships(
*reinterpret_cast<unsigned (*)[5]>(p_first_membership)
);
or:
ProcessFiveMemberships(
*static_cast< unsigned(*)[5] > (
static_cast<void*>(p_first_membership)
)
);
-Tomás
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Tomás Guest
|
Posted: Sat Mar 25, 2006 1:56 am Post subject: Re: Type of "new" expression |
|
|
| Quote: | Then the type of this expression is not "unsigned (*)[5]", but
"unsigned *".
|
I found a solution. Use:
new unsigned[1][5]
instead of:
new unsigned[5]
Now the following is okay:
void Array()
{
unsigned (*p_numbers)[5] = new unsigned[1][5];
(*p_numbers)[0] = 5;
(*p_numbers)[1] = 6;
delete [] p_numbers;
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sat Mar 25, 2006 2:06 am Post subject: Re: Type of "new" expression |
|
|
* NULL (AT) NULL (DOT) NULL:
| Quote: | For argument's sake, let's say we have the following function which we want
to invoke:
void ProcessFiveMemberships( unsigned (&memberships)[5] );
If "new" is used to allocate an array, as follows:
new unsigned[5];
Then the type of this expression is not "unsigned (*)[5]", but "unsigned *".
My first question is, Why was this made so?
|
Most practical; usually the array size is determined at run-time.
| Quote: | It would make more sense to me
to return a pointer to an array of five objects, rather than a pointer to
one sole object.
|
Then J&W Pascal is the language for you. ;-)
| Quote: | We want to invoke "ProcessFiveMemberships". The following is ill-formed:
void ProcessFiveMemberships( unsigned (&memberships)[5] )
{
//Empty function body
for (;
{
break;
}
}
int main()
{
unsigned* p_first_membership = new unsigned[5];
ProcessFiveMemberships( *p_first_membership ); // type mismatch
delete [] p_first_membership;
}
Is there any perfectly legal, fully portable way of turning our "unsigned
*" into an "unsigned (*)[5]"?
|
void ProcessFiveMemberships( unsigned (&memberships)[5] )
{
for (; { memberships; break; }
}
int main()
{
typedef unsigned UnsignedArray5[5];
UnsignedArray5* ua5 = new UnsignedArray5[1];
ProcessFiveMemberships( *ua5 );
delete[] ua5;
}
| Quote: | How about the two following methods:
ProcessFiveMemberships(
*reinterpret_cast<unsigned (*)[5]>(p_first_membership)
);
|
Formally UB.
| Quote: | or:
ProcessFiveMemberships(
*static_cast< unsigned(*)[5] > (
static_cast<void*>(p_first_membership)
)
);
|
Not sure whether it's formally valid or not, but while it's in-practice
technically OK (except perhaps on AS400 or original ENIAC...) it's
ungood as software engineering practice.
If you want a fixed-size array, just wrap it in a struct.
You might also find it beneficial to check out the Boost array facilities.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|