471,604 Members | 1,543 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Copy Array of class pointer to another

Hi All,
Now, I am trying to build a project, I need to expand an array of pointer to classes. Moreover, this array includes some elements I want to delete them. So, I create another array, copy the elements which i want to keep and copy this array to the original one as follows
Expand|Select|Wrap|Line Numbers
  1.  This function to create two arrays it depends on the parameter x.
  3. void Econ::CreateFirms(int array,int x) 
  4. {
  5. if(x==1)
  6.   {
  7.      FirmArray= new Firm*[array];
  8.      if(FirmArray==NULL)
  9.        {
  10.              cout<<" Memory allocation fail.\n";
  11.             exit(1);
  12.        }
  13.      for(int i=0;i<array;i++)
  14.        {
  15.             FirmArray[i]=new Firm();
  17.            //cout << endl<< FirmArray[i];
  18.             if(FirmArray[i]==NULL)  
  19.             {
  20.                 cout<<" Memory allocation fail.\n";
  21.                 exit(1);
  22.             }
  23.        }
  24.   }
  25.   else if(x==2)
  26.   {
  27.      FirmArray1= new Firm*[array];
  28.      if(FirmArray1==NULL)
  29.        {
  30.              cout<<" Memory allocation fail.\n";
  31.             exit(1);
  32.        }
  33.      for(int i=0;i<array;i++)
  34.        {
  35.             FirmArray1[i]=new Firm();
  37.            //cout << endl<< FirmArray[i];
  38.             if(FirmArray1[i]==NULL)  
  39.             {
  40.                 cout<<" Memory allocation fail.\n";
  41.                 exit(1);
  42.             }
  43.        }
  44.   }
  45. }
the copy function is
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<F;i++)
  2.    {
  3.       FirmArray[i]=FirmArray1[i];
  4.    }
The problem is FirmArray has an pointer to pointer to float, this give me a segmentation fault
when deleting the FirmArray1
Dec 18 '07 #1
3 3177
463 Expert 256MB
FirmArray has only pointers to Firm's. When you talk about a pointer to floats, I assume you mean the floats are in the object Firm. From your post, it sounds like you delete FirmArray1, which in turn deletes the Firms which in turn delete the float array.

This sounds like a problem of deleting the float array more than once. Don't do that. In C++, you must make sure the array is deleted only once. A reference counter is one way to fix this type of problem (can someone help with a link to theScripts how-to article). Another possibility is do a deep copy between FirmArrays.
Dec 18 '07 #2
Thanks for your explanation. I think, I did not give a good vision of my problem.
I can explain my problem as follows:

I create one array of Firms (ArrayFirm). And each object (ArrayFirm[i]) has an array of float. After, finishing the first round, my objects (ArrayFirm[i]) I do not need anymore and I would like to delete them, in the maintime, new object i would like to add, some will exit and others will entry, the problem is number of exit objects is the equal the number of entry objects. Therefore, my idea is to create another array of firms with a new dimension(old-exit+entry), Then, starting to copy objects I need according to the next function
Expand|Select|Wrap|Line Numbers
  1. void Econ::CopyFirmsPointer()
  2. {
  3.        int i=0;
  4.          {     
  5.             for(int j=0;j<numfirm;j++)
  6.               {    
  7.                   if(FirmArray[j]->GetCapital()>0)
  8.                      {
  9.                         FirmArray1[i]=FirmArray[j]; 
  10.                         if(i!=j) ResetHousehold(j,i);
  11.                         i++;  
  12.                      }                    
  13.               }
  14.          }
  15. }
and then,
copy second array to first array as in first post
Dec 19 '07 #3
9,208 Expert Mod 8TB
Apparently, your Firm class has a float* in it. Most likely you are copying the pointer rather than copying the float array in your Firm copy constructor and assignment operator.

You cannot copy pointers because the first object that goes out of scope deletes the array in the destrcutor thereby trashing all the other objects.

If all objects are to share the saem array, you use a handle. (Read this: http://www.thescripts.com/forum/thread651599.html.

If all objects are to be copies, then in the copy constrcutor and assignment operator you must make a copy of the array and not just a copy of the pointer.
Dec 19 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

30 posts views Thread by franky.backeljauw | last post: by
2 posts views Thread by Birt | last post: by
4 posts views Thread by William Payne | last post: by
4 posts views Thread by xuatla | last post: by
4 posts views Thread by Yudan Yi | last post: by
7 posts views Thread by simkn | last post: by
35 posts views Thread by dragoncoder | last post: by
3 posts views Thread by Immortal_Nephi | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | 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.