Jump to content


Copying 2D array by pointers (C++)


7 replies to this topic

#1 lin-da

    New Member

  • Members
  • Pip
  • 4 posts

Posted 10 September 2004 - 06:19 PM

I had such code to copy three 2D arraya around by using pointers like below, but it seems all three pointers are pointing to the same memory address. How can I get it correctly?

float **U_past, **U_future, **V_past, **V_future, **U_pp, **V_pp;
In construct :
U_past = new float*[a]; //a=100
U_future = new float*[a];
U_pp = new float*[a];

for(int i=0; i<nsigma; i++)
{
  U_past[i] = new float[e]; //e=400
  U_future[i] = new float[e];
  U_pp[i] = new float[e];
}

and in main fuction I want to do

U_pp = U_past; //save U_past data in U_pp
U_past = U_future; //save U_future data in U_past
U_future = new_data; //get new data for U_future

in destruct
delete [] U_past;
delete [] U_future;
delete [] V_pp;

The expect data would be like
first aound : U_pp[0][0]=0.1, U_past[0][0] =0.2, U_future[0][0]=0.3;
second round : U_pp[0][0]=0.2, U_past [0][0]=0.3, U_future[0][0]=1.4;
thrid : U_pp[0][0]=0.3, U_past [0][0]=1.4, U_future[0][0]=2.3;

How should I make it? Thanks.

#2 anubis

    Senior Member

  • Members
  • PipPipPipPip
  • 2225 posts

Posted 10 September 2004 - 07:08 PM

i don't see where you do any copying at all... you allocate three 2d arrays and then set you set a few pointers. this does not copy data anywhere, it just changes which pointers point to the data. maybe i misunderstand what you are trying to do ? could you give a more detailed explanation ?
If Prolog is the answer, what is the question ?

#3 lin-da

    New Member

  • Members
  • Pip
  • 4 posts

Posted 10 September 2004 - 07:30 PM

Yes, I pointed U_pp to U_past to let U_pp have the data that U_past had. Then I want U_past had data from U_future. U_future will get some new data.
I knew I was wrong this part, my current result will be U_future, U_past, U_pp are all the same, because they are pointing to the same place.
But I don't want to for loop to copy values, one by one.
Basically, that's why I come here.
How can I copy them by pointers?
I hope I clear my problem this time. Thanks al ot.

#4 z80

    Valued Member

  • Members
  • PipPipPip
  • 104 posts

Posted 10 September 2004 - 07:50 PM

Copy them one by one in nested for loops... Nothing wrong with that for 400*100 numbers unless you do it VERY VERY often.

Your destructor code is buggy btw.. You probably want something like:

for(int i=0; i<nsigma; i++)
{
   delete[] U_past[i];
   delete[] U_future[i];
   delete[] V_pp[i];
}
delete[] U_past;
delete[] U_future;
delete[] V_pp;

EDIT:

In case you insist you can use memcpy (since you're newing the way you do, you will need at least one loop because you dont know if your memory blocks will end up next to each other in memory):

//save U_past data in U_pp
for(int i=0; i<nsigma; i++)
    memcpy(U_pp[i], U_past[i], 400 * sizeof(float));


#5 lin-da

    New Member

  • Members
  • Pip
  • 4 posts

Posted 10 September 2004 - 08:07 PM

Thanks z80.
But I am running a big program the actuall size of U_pp is 15*10000. And I need to run it at monthly data, so it will bw a lot of copying. That's why I want to fiure out a way to point them one to another.
And I had tried memcpy before, it didn't work. Because I still got the result like U_pp=U_past=U_future.
Any other idea?
I appreciated.

#6 z80

    Valued Member

  • Members
  • PipPipPip
  • 104 posts

Posted 10 September 2004 - 08:10 PM

Well, memcpy() is a good and efficient way to move data, so you can do that.

But you should probably tell us what problem your application is solving on a more abstract level, then we might be able to come up with some ideas.

#7 lin-da

    New Member

  • Members
  • Pip
  • 4 posts

Posted 10 September 2004 - 08:19 PM

Yes.
U arrays are velocities, I want to calculate a current velocity by using quadratic interpolation.
That's why I need to keep past past velociities, past velocities and possible future velocities.
I read U_future from files. After starting I pass U_futre to U_past, and U_past to U_pp to calculate U_now.
aU = (U_future+U_pp-2.*U_past)/2.;
bU = (U_future-U_pp)/2.;
cU = U_past;
U_now=aU*time_frac*time_frac + bU*time_frac + cU;
That's the popurse.

#8 z80

    Valued Member

  • Members
  • PipPipPip
  • 104 posts

Posted 10 September 2004 - 08:27 PM

Just copy them one by one right after you do the interpolation for individual array entries. That way you have the values handy in the cache.

for (int j = 0; j < height; ++j)
    for (int i = 0; i < width; ++i)
    {
        // Interpolate velocity
        U_now[j][i] = ...;
  
        // Update past & future velocities
        U_past[j][i] = U_now[j][i];
        U_future[j][i] = ...;
    }






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users