473,399 Members | 4,254 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,399 software developers and data experts.

A silly Question

Hi All,

Can anyone explain me why the return type of new MyClass[10] is
MyClass*. I think that it should be MyClass (*ptr)[] or MyClass**. Is
it dependent on the compiler implementation of new[] ?

Regards,
Sahoo

Oct 16 '06 #1
2 1373
Subhransu Sahoo wrote:
Can anyone explain me why the return type of new MyClass[10] is
MyClass*. I think that it should be MyClass (*ptr)[] or MyClass**. Is
it dependent on the compiler implementation of new[] ?
That's just how it is in the language. The expression 'new T [ n ]'
where "T" is a type-id and "n" is an integral expression has the type
"pointer to T". If you want a pointer to an array, you need to create
a single object whose type is "array of MyClass". If you want a pointer
to a pointer, you need to create a single object of type "pointer to
MyClass". I am not sure why you'd want substitute creation of many
objects of type FOO with creation of a single object of type BAR, no
matter how FOO and BAR are related.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 16 '06 #2
Subhransu Sahoo wrote:
Hi All,

Can anyone explain me why the return type of new MyClass[10] is
MyClass*. I think that it should be MyClass (*ptr)[] or MyClass**. Is
it dependent on the compiler implementation of new[] ?
Since you need
Regards,
Sahoo
No, if the return type was pointer_to_pointer then new would only be
allocating one element. That would be bad news.
Consider:
MyClass p_array[10];

MyClass represents the element type involved. The pointer's type is
therefore absolutely critical.
p_array, which decays to a pointer, is actually "storing":

a) a const count of elements <- thats hidden from you
b) the type of all elements (by typing the pointer itself) <- thats the
key
c) the address of the first element

Thats how the program knows how_many_times to invoke the _appropriate_
ctor and d~tor starting from where. Which also explains why you can't
pass an array by value or reference (you would loose the count).

Alternatively:
int main()
{
MyClass* p_array = new MyClass[10];

// do stuff

delete [] p_array; // ???
}
Question ???: How does the program know how many d~tors to invoke at
delete []? How does the program know _which_ ctor and d~tor to invoke?
Imagine what would happen if p_array - a pointer(* p_array) - had
MyClass* and not MyClass as a type? Have you ever seen the d~tor for a
pointer? Do you see how critical type MyClass is?

Is this a lousy way of doing things? Yes. But its not worth breaking
old code. Should the language change the way an array works? It has -
you shouldn't be using an array - they are evil.
http://www.parashift.com/c++-faq-lite/containers.html
Use a std::vector instead.

std::vector< MyClass v(10);

Unlike arrays, i can pass a vector around by value or reference and its
dynamic.

void foo( std::vector< MyClass >& r_vector)
{
// do whatever
}

Why deal with dumb, buggy pointers anyways? Why hastle with primitive,
fixed-size, brain-dead arrays?

Oct 16 '06 #3

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

Similar topics

5
by: Pjotr Wedersteers | last post by:
This may be a silly question, but I was wondering. If 10 clients fill out a form on my page simultaneaously and hit submit, how does my (Apache2.0.50/PHP4.3.8) server exactly ensure each gets...
4
by: Jenny | last post by:
Hi you al I have two very silly question The first one is In vb 6.0 you can add a procedure, sub or function, by clicking add procedure from the tools item on the menuba I cannot seem to find...
4
by: musosdev | last post by:
I think I'm having a dim day and should just go back to bed, but alas, I cant do that.... I'm writing a peice of code to create XHTML compliant documents using System.IO, there's probably an...
6
by: Adam Honek | last post by:
Hi, I've looked over and over and in MSDN 2005 and just can't find what is a really simple answer. How do I select the first value in a combo box to be shown? I tried the .selectedindex -1...
7
by: Matt | last post by:
I've asked this question to some developers that are much more experienced than I, and gotten different answers --- and I can't find anything about it in the documentation. Dim vs. Private as a...
2
by: Willem Voncken | last post by:
Hi guys, might be a silly question, but i'm wondering whether it is even possible to generate access violations when using C#? I'm new at c# programming, but i have some experience with c++....
1
by: Pontifex | last post by:
Hi all, Has anyone devised a simple method for passing a string to an external script? For instance, suppose I want to produce a title bar that is very fancy and I don't want my main HTML...
2
by: toton | last post by:
Hi, This is a silly question related to syntax only. I have a template class with template member function, How to write it separately in a file (not translation unit, just want to separate...
4
by: Marcin Kasprzak | last post by:
Hello Guys, Silly question - what is the most elegant way of compiling a code similar to this one? <code> typedef struct a { b_t *b; } a_t; typedef struct b {
5
by: Ben Bacarisse | last post by:
Newbie <newbie@gmail.comwrites: <snip> The version you posted in comp.programming has the correct loop. Why change it? -- Ben.
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: 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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.