473,320 Members | 2,024 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,320 software developers and data experts.

Copy Array of class pointer to another

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
3 3315
RRick
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
aeo3
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
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

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

Similar topics

30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
2
by: Birt | last post by:
My understanding about defining your own copy and assignment constructors is whenever there is a member of pointer type. Is this true or is there any exception to this"rule"? How about when you...
4
by: William Payne | last post by:
Hello, I was under the impression that if I made a class Foo and if I didn't specify a copy constructor I would get one anyway that simply assigns the member variables (and that won't work for...
4
by: xuatla | last post by:
Hi, How to copy a pointer to another pointer? Can I do in the following way: // START double *copyfrom = new double; double *copyto = new double;
4
by: Yudan Yi | last post by:
I have a problem to copy (assign) a matrix to another matrix. Curreny, I know copy the number using loops, while it will take some time, I wonder if there have faster method. The following code...
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
2
by: Jeremy Smith | last post by:
I am writing a program where speed is crucial, and one optimisation is that blocks of memory are re-used instead of being copied around and re- allocate. I have written my own vector-style class...
35
by: dragoncoder | last post by:
Just a simple theoritical question to the experts. What was the rationale behind making STL containers follow copy semantics rather than reference semantics. References almost always make things...
3
by: Immortal_Nephi | last post by:
Sometimes, unsigned char array is located in the file scope. You define A Object and B Object. A Object and B Object need to share unsigned char array. They are very different object. They are...
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...
1
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.