473,230 Members | 1,867 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,230 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 3309
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
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: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
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:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.