471,305 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

dynamic array of pointers vs dynamic array of objects

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[i] = itemList[i]; }
delete [] itemList;
itemList = itm;
}

how this code will change to allocate an array of item
pointers of type Item*?????
Nov 16 '05 #1
1 4305
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[i] = itemList[i];
}
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[i] = itemList[i];
}
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;
}
------------------------------------------------------------------------

Bart Jacobs

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Nils | last post: by
14 posts views Thread by Steven Taylor | last post: by
8 posts views Thread by Peter B. Steiger | last post: by
60 posts views Thread by Peter Olcott | last post: by
1 post views Thread by john townsley | last post: by
13 posts views Thread by Krivenok Dmitry | last post: by
4 posts views Thread by Steph | last post: by
reply views Thread by rosydwin | last post: by

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.