dynamic array of pointers vs dynamic array of objects

L

lemonade

Hello!
Can someone explain to me the difference between dynamic
array of pointers vs dynamic array of objects by giving a
real life example. Following is the code that I am using
for dynamic array of objects. I am skipping initialization
and other member functions.

class Inventory {
Item *itemList;
int idx, count, size;
public:
void appendItem (char ttl[]);
void resizeArray();
};

void Inventory::appendItem (char ttl[])
{ if (count == size)
{resizeArray(); }
itemList[count].set(ttl)

}

void Inventory::resizeArray()
{Item *itm = new Item[size ++];
if (itm == 0) {cout << "Out of memory\n"; exit(1); }
for (int i = 0; i < count; i++)
{itm = itemList; }
delete [] itemList;
itemList = itm;
}

how this code will change to allocate an array of item
pointers of type Item*?????
 
B

Bart Jacobs

Lemonade,

This is your code, turned into a complete little program:

------------------------------------------------------------------------
#using <iostream>
#using <string>

using namespace std;

class Item {
char *title;
public:
void set(char *ttl) { title = ttl; }
char *get() { return title; }
};

ostream &operator <<(ostream &os, Item item) {
return os << item.get() << endl;
}

class Inventory {
Item *itemList;
int idx, count, size;
public:
Inventory() { count = 0; size = 0; itemList = new Item[0]; }
void appendItem (char ttl[]);
void resizeArray();
int getCount() { return count; }
Item getItem(int index) { return itemList[index]; }
};

ostream &operator <<(ostream &os, Inventory &inventory) {
for (int i = 0; i < inventory.getCount(); i++) {
os << inventory.getItem(i);
}
return os;
}

void Inventory::appendItem (char ttl[])
{
if (count == size)
{
resizeArray();
}
itemList[count++].set(ttl);
}

void Inventory::resizeArray()
{
Item *itm = new Item[++size]; // instead of size++
if (itm == 0) {
cout << "Out of memory\n";
exit(1);
}
for (int i = 0; i < count; i++)
{
itm = itemList;
}
delete [] itemList;
itemList = itm;
}

int _tmain(int argc, _TCHAR* argv[])
{
Inventory inventory;
inventory.appendItem("Hello");
inventory.appendItem("Bye");
cout << inventory;
string s;
cin >> s;
return 0;
}
------------------------------------------------------------------------

In the above code, Item objects have value semantics, in the sense that
their lifetime and their identity do not matter. In the above code, an
Item object is really just a container for a char *.

Now follows the version where the Item objects are allocated on the heap
and pointers to them are stored in itemList. Lifetime and identity are
important.

------------------------------------------------------------------------
#using <iostream>
#using <string>

using namespace std;

class Item {
char *title;
public:
Item(char *title) { this->title = title; }
char *get() { return title; }
};

ostream &operator <<(ostream &os, Item *item) {
return os << item->get() << endl;
}

class Inventory {
Item **itemList;
int idx, count, size;
public:
Inventory() { count = 0; size = 0; itemList = new Item*[0]; }
void appendItem (char ttl[]);
void resizeArray();
int getCount() { return count; }
Item *getItem(int index) { return itemList[index]; }
};

ostream &operator <<(ostream &os, Inventory &inventory) {
for (int i = 0; i < inventory.getCount(); i++) {
os << inventory.getItem(i);
}
return os;
}

void Inventory::appendItem (char ttl[])
{
if (count == size)
{
resizeArray();
}
itemList[count++] = new Item(ttl);
}

void Inventory::resizeArray()
{
Item **itm = new Item*[++size]; // instead of size++
if (itm == 0) {
cout << "Out of memory\n";
exit(1);
}
for (int i = 0; i < count; i++)
{
itm = itemList;
}
delete [] itemList;
itemList = itm;
}

int _tmain(int argc, _TCHAR* argv[])
{
Inventory inventory;
inventory.appendItem("Hello");
inventory.appendItem("Bye");
cout << inventory;
string s;
cin >> s;
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top