Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 16th, 2006, 04:55 PM
Subhransu Sahoo
Guest
 
Posts: n/a
Default 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

  #2  
Old October 16th, 2006, 05:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: A silly Question

Subhransu Sahoo wrote:
Quote:
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


  #3  
Old October 16th, 2006, 09:55 PM
Salt_Peter
Guest
 
Posts: n/a
Default Re: A silly Question

Subhransu Sahoo wrote:
Quote:
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?

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles