473,241 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 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 4431
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Nils | last post by:
Hi, I want to create a dynamic array with pointer, without allocation of the memory. I tried it so: objekt **ob= new objekt; It is not working, because he will parameter for the Konstructor...
14
by: Steven Taylor | last post by:
I'm learning C++ from C++ Primer Plus 5th edition. I'm struggling at the moment with declaring an array of three structures by using 'new' to allocate memory. (this is one of the programming...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
1
by: john townsley | last post by:
OK so with c++ when using pointers for dynamic allocation at runtime, like a database type program. I am talking about a user adding records of an unkown amount at runtime, so pointers would be the...
2
by: jccorreu | last post by:
I'm taking in data from multiple files that represents complex numbers for charateristics of different elements. I begin with arrays of doubles and interpolate to get standard values in real and...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
4
by: Steph | last post by:
Hello, I have filled a dynamic array of strings (realloc() + malloc()) char **sMyArray; At the end, I get correctly sMyArray = "STRING_0", sMyArray = "STRING_1", etc... But I'm unable to...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.