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

From the freestore


The following works as expected:
unsigned* p_number = new unsigned;

*p_number = 5;

delete p_number;

However, the following won't compile:
unsigned (*p_numbers)[5] = new unsigned[5];

(*p_numbers)[0] = 5;

(*p_numbers)[1] = 6;

delete [] p_numbers;

My compiler gives me the following error:
cannot convert `unsigned int*' to `unsigned int (*)[5]'
"new" is returning an expression of type "int *" rather than "int (*)[5]".
My first question is, Why did they make it so? I don't see the rationale.

Secondly, does the Standard explicitly specify that "new" returns a pointer
to the first element?
Thirdly, how do you somehow turn it into a pointer to an array? Is the
following valid (I've made it verbose for clarity):
unsigned* p_first_number = new unsigned[5];

void* p_temp = p_first_number;

unsigned (*p_numbers)[5] = static_cast<unsigned (*)[5]>( p_temp );

(*p_numbers)[0] = 5;

(*p_numbers)[1] = 6;

delete [] p_first_number;
I've used a "void*" in the above rather than using "reinterpret_cast".

So is there a pefectly legal way to get a pointer/reference to an array (for
something allocated using "new")?
-Tomás
Mar 24 '06 #1
3 1525
Tomás wrote:
[snip]
However, the following won't compile:

unsigned (*p_numbers)[5] = new unsigned[5];

(*p_numbers)[0] = 5;

(*p_numbers)[1] = 6;

delete [] p_numbers;

My compiler gives me the following error:

cannot convert `unsigned int*' to `unsigned int (*)[5]'

[snippers]

Sure. What does int (*) suggest to you? Hint, look up
"pointer to function" and see what you find. Putting the
( ) round (*p_numbers) changes the type.

You need to read up on the syntax of what's going on here.
It looks like you've just guessed what it should be. Get
something like _The C++ Language_ and read the first
100 pages or so very carefully. Also, read through this
group and find the C++ FAQ and read it carefully.
Socks

Mar 24 '06 #2
Tomás wrote:

The following works as expected:
unsigned* p_number = new unsigned;

*p_number = 5;

delete p_number;

However, the following won't compile:
unsigned (*p_numbers)[5] = new unsigned[5];

(*p_numbers)[0] = 5;

(*p_numbers)[1] = 6;
new[] returns a pointer to the first element of the array, not a pointer to
the array itself. So make that:

unsigned* p_numbers = new unsigned[5];
p_numbers[0] = 5;
p_numbers[1] = 6;
delete [] p_numbers;

My compiler gives me the following error:
cannot convert `unsigned int*' to `unsigned int (*)[5]'

"new" is returning an expression of type "int *" rather than "int (*)[5]".
No. new[] is. It's another beast.
My first question is, Why did they make it so? I don't see the rationale.
Because it's normal to use an array through a pointer to its first element,
and the syntax is simpler.
Secondly, does the Standard explicitly specify that "new" returns a
pointer to the first element?
Yes.
Thirdly, how do you somehow turn it into a pointer to an array?
Why would you want to?
Is the following valid (I've made it verbose for clarity):
unsigned* p_first_number = new unsigned[5];

void* p_temp = p_first_number;

unsigned (*p_numbers)[5] = static_cast<unsigned (*)[5]>( p_temp );

(*p_numbers)[0] = 5;

(*p_numbers)[1] = 6;

delete [] p_first_number;

I've used a "void*" in the above rather than using "reinterpret_cast".

So is there a pefectly legal way to get a pointer/reference to an array
(for something allocated using "new")?


If you really want a pointer to the array, you can allocate an array of
arrays (or rather of one array), like:

unsigned (*p_first_number)[5] = new unsigned[5][1];

Mar 24 '06 #3
unsigned (*p_first_number)[5] = new unsigned[5][1];

Exactly what I was looking for! Although it should be: unsigned[1][5]

Now we can do this:

void Array()
{
unsigned (*p_numbers)[5] = new unsigned[1][5];

(*p_numbers)[0] = 5;

(*p_numbers)[1] = 6;

delete [] p_numbers;
}
-Tomás
Mar 24 '06 #4

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

Similar topics

0
by: |-|erc | last post by:
Hi! Small challenge for you. The index.php uses this file and calls layout(). Take a look at www.chatty.net this file draws the chat login box on the right. I traced the CHAT button it submits...
16
by: Fuzzyman | last post by:
Hello, To create a classic (old style) class, I write : class foo: pass To do the equivalent as a new style class, I write : class foo(object):
2
by: nitin | last post by:
g++ -c $INCLUDES ActualPEResult.cpp In file included from /usr/include/c++/3.2.2/backward/iostream.h:31, from /home/pradeepks/Linux_Porting/dcpfrontier/dcpdev/dcp_components/trap...
4
by: susmita_ganguly | last post by:
Hi I am trying to upgrade from oracle 8i to oracle 9i on the same server ..I don't know much abt migration . Can anyone help me out. Thanks. Susmita
2
by: cs168 | last post by:
Hi I am new in ASP programming so I do use the very basic and simple way to do all my stuff. Now I do really got stuck at how can I loop thru the calculation for all my selection.. My full code is as...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
15
by: Dilip | last post by:
I am aware that the C++ standard in its present form does not say anything about threads, however I do have a relevant question. I am working on Windows XP/VC++ 8.0. Is there a problem new'ing...
18
by: Philipp | last post by:
Hello, in my main() I have code which looks like this MyClass* myObject; if ( param.isValid() ) myObject = new MyClass(otherParam, param); else myObject = new MyClass(otherParam); -----...
3
by: yonil | last post by:
Over the years of using C++ I've begun noticing that freestore management functions (malloc/free) become performance bottlenecks in complex object-oriented libraries. This is usually because these...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...
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...

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.