1

This is within C++ code

YInterface ** Y_list;

int numberofYInterfaces;

then

numberofYInterfaces=1;

Y_list = new YInterface *[numberofYInterfaces];        //What ??????

I have a very hard time picturing the structure of Y_list? And then imagine my confusion when and if the numberofYInterfaces becomes more than one, even 2?

OOPS sorry and I forgot to write this below statement:

Y_list[i]= new AnotherClass();

//later addition to my orginal question . Thanks for the headsup Eric Fortin. Note: AnotherClass is inherited from YInterface.

Any help is appreciated

user553514
  • 217
  • 2
  • 4
  • 14

4 Answers4

12

When you declare Y_list, you get a single variable with an unspecified value in it:

Y_list
+-------+
|       |
+-------+

When you assign it with new[], it points to whatever new[] allocated, which in this case is an array of pointers:

Y_list         array
+-------+     +-------+
|   o---+---->|       |
+-------+     +-------+
              |       |
              +-------+
              |       |
              +-------+
              |       |
              +-------+
              |       |
              +-------+

At that point, you don't yet have any YInterface objects. You just have space allocated to hold pointers to them, should they ever exist.

Finally, you assign a value to one of the elements Y_list[i] = new AnotherClass. That's when a YInterface descendant exists, but you only have one so far. Run that code in a loop and you'll get multiple instances. But you still just have one array, and one pointer to that array.

Y_list         array         AnotherClass
+-------+     +-------+     +-------+
|   o---+---->|   o---+---->|       |
+-------+     +-------+     |       |
              |       |     |       |
              +-------+     |       |
              |       |     |       |
              +-------+     +-------+
              |       |
              +-------+
              |       |
              +-------+

To free all this, remember to have one delete statement for each new statement you had, and one delete[] for each new[]:

delete Y_list[i];

delete[] Y_list;
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • 1
    +1 for nice graphics :) It always helps when dealing with pointers and dynamically allocated memory. – Xeo Feb 07 '11 at 17:34
  • @Rob Kennedy : When you say "When you assign it with new[], it points to whatever new[] allocated, which in this case is an array of pointers: – user553514 Feb 07 '11 at 17:35
  • " in this case it does not say new[] it says new *[]. The astrik throws me?? Can you please explain that too. Thanks – user553514 Feb 07 '11 at 17:36
  • 2
    @User, when you use `new`, the thing following `new` is the name of the type you're allocating. If you're allocating an array of that type, then the type is followed by brackets. The type in this case is *pointer-to-YInterface*, or `YInterface*`. The space between the base type name and the asterisk doesn't matter to the compiler. If I were writing this code, I'd write `new YInterface* [...]`. There's no such thing as `*[]`. The asterisk goes with the type name before it, not the brackets after it. – Rob Kennedy Feb 07 '11 at 17:40
  • @Rob Kennedy: So, in your graphics am I correct that you have assumed that numberofYInterfaces is actually 5? I would highly appreciate your reply. You have been extremely helpful. Thank you. – user553514 Feb 07 '11 at 17:54
  • I didn't really count as I was drawing it, but yes, the array's length is five in my example. I didn't notice that your code used just one. The length doesn't really matter. When you draw your own diagrams, make them as long or as short as you need. – Rob Kennedy Feb 07 '11 at 18:02
  • @Rob Kennedy Well, thank you very much. Overall an awesome explanation. You have a gift for it. Thanks. – user553514 Feb 07 '11 at 18:13
  • @Rob Kennedy, If everything else stayed the same, what would have happened if Y_list was only declared as YInterface * Y_list? – user553514 Feb 07 '11 at 19:38
  • 1
    @User, if `Y_list` were declared as a `YInterface*, then your code would no longer compile. The expression `new YInterface*[...]` has type `YInterface**`, and you're not allowed to assign that to something of type `YInterface*`. – Rob Kennedy Feb 07 '11 at 20:01
2

It means it's a dynamically allocated array of pointers on YInterface. Then suppose method blah is part of YInterface, you could access it through the array like:

Y_list[0]->blah();

Concerning your edit. Every item in the array is a pointer on YInterface so to actually have something pointed to, you need to allocate it which you do with new operator. So doing Y_list[i] = new AnotherClass() does allocate an AnotherClass object and store a pointer on this object in the array at index i.

Keep in mind though that AnotherClass needs to inherit YInterface for this to work.

Eric Fortin
  • 7,533
  • 2
  • 25
  • 33
2

Y_list points to a chunk of memory which is an array of pointers to YInterface objects -- each pointer is sizeof (YInterface *), so this chunk of memory (after the call to new) is of size numberOfYInterfaces * sizeof (YInterface *).

So Y_list is just a normal dynamically allocated array, with numberOfYInterfaces elements, each of which is itself a pointer to a YInterface object.

metamatt
  • 13,809
  • 7
  • 46
  • 56
0

in c++ array are pointers , that is the memory address of first element of array is the name of array , so now what you have is array of pointers , hence **

Mr Coder
  • 8,169
  • 5
  • 45
  • 74