Solved

Help me... please... object array problems...

Posted on 2004-09-13
6
259 Views
Last Modified: 2010-04-01

Hi, I need to make an array of object pointers which is probably one of the easiest things to implement but I can't get my brain to wrap around this...

I create an object like this:

CMyObject * myObj = new CMyObject;

then I allocate some space in an array which is defined as a "void *", cast it to an array of pointers (CMyObject*)? and then assign the pointer to the new object to the end of the list:

declared as --> void * myList = NULL;

myNewList = realloc (myList, (numItems + 1) * sizeof (CMyObject *));
if (myNewList != NULL)
{
     myList = myNewList;
     CMyObject * objectPtrList = (CMyObject *)myList;
     objectPtrList[numItems] = myObj;
     numItems ++;
}

Now, I'm kind of constrained as this code needs to adhere to the "void *" for storing the array and the memory must be allocated with malloc/realloc except for objects such as CMyObject...

The compiler I use complains at the assignment of objectPtrList[numItems]....

-- Bubba
0
Comment
Question by:bganoush
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12045782
You should bettr use a 'std::list', e.g. like

#include <list>
using namespace std;

list<CMyObject*> myList;

CMyObject * myObj = new CMyObject;

myList.push_back (myObj);

//...

list<CMyObject*>::iterator i = myList.begin();

CMyObject * object = *i;

0
 
LVL 1

Accepted Solution

by:
zekard earned 500 total points
ID: 12045836
How about this?

/// tested in VC++ 6.0

#include <iostream>

using namespace std;

class CMyObject
{
public:
      int x;
      int y;
      CMyObject (); // constructor
};

CMyObject::CMyObject ()
{
}

int main ()
{
      CMyObject *myObj = new CMyObject ();
      CMyObject ** objectPtrList = NULL;

      void * myList = NULL;
      void * myNewList = NULL;
      int numItems = 0;

      myNewList = realloc (myList, (numItems + 1) * sizeof (CMyObject *));
      if (myNewList != NULL)
      {      
            myList = myNewList;
            objectPtrList = (CMyObject **)myList;
            objectPtrList[numItems] = myObj;
            numItems ++;
      }

      return 0;

}
0
 

Expert Comment

by:kyleleber
ID: 12045852
It looks like your assignment:

objectPtrList[numItems] = myObj;

is trying to assign a pointer to an object.

myObj is type (CMyObject *)
objectPtrList expects type (CMyObject).

Try dereferencing myObj in the assignment:

objectPtrList[numItems] = *myObj;  //If this is what you are trying to do
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bganoush
ID: 12046190
Zekard,

>>           objectPtrList = (CMyObject **)myList;
>>          objectPtrList[numItems] = myObj;
 
Oops!  I knew that!

What I tried before was something like this:

          CMyObject ** objectPtrList = (CMyObject *)myList;
          objectPtrList[numItems] = myObj;
 
I forgot to cast "myList" to a pointer to pointers...

Thanks!
0
 
LVL 86

Expert Comment

by:jkr
ID: 12046240
>>I forgot to cast "myList" to a pointer to pointers...

1st of all, your list is not a list, but an array of pointers. 2nd, why would ypu want to reinvent the wheel?
0
 

Author Comment

by:bganoush
ID: 12047467

1st:

English is not my first language, "list" sounds much closer to what it is called in my language. Thank you for correcting my unsuitably bad translation...

2nd:

It is what is expected of me... The management I am working with requires me to send an array of pointers to their precious engine. ie: red tape

But thank you so very much for caring!

-- Bubba
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question