A silly Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Subhransu Sahoo

    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

  • Victor Bazarov

    #2
    Re: A silly Question

    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


    Comment

    • Salt_Peter

      #3
      Re: A silly Question

      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_poin ter 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.

      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?

      Comment

      Working...