By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,074 Members | 666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,074 IT Pros & Developers. It's quick & easy.

Copy Array of class pointer to another

P: 29
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.
  2.  
  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();
  16.  
  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();
  36.  
  37.            //cout << endl<< FirmArray[i];
  38.             if(FirmArray1[i]==NULL)  
  39.             {
  40.                 cout<<" Memory allocation fail.\n";
  41.                 exit(1);
  42.             }
  43.        }
  44.   }
  45. }
  46.  
  47.  
the copy function is
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<F;i++)
  2.    {
  3.       FirmArray[i]=FirmArray1[i];
  4.    }
  5.  
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
Share this Question
Share on Google+
3 Replies

RRick
Expert 100+
P: 463
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

P: 29
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. }
  16.  
  17.  
and then,
copy second array to first array as in first post
Dec 19 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
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.