1

I have two arrays of type Region, both of size 1000, and at every iteration of a loop I want to swap the two of them (by swapping their memory addresses). I was hoping this would work:

        Region *swap = (Region*)myRegions;
        myRegionsLast = myRegions;
        myRegions = (Region[1000])swap;

Line one seems fine. The second and third lines are invalid assignments, as you apparently can't re-assign that type. The third line is also invalid because you can't cast to the type (Region [1000]). Obviously I don't want to allocate whole new Region[1000] if I can help it. Can someone help me accomplish what I want?

eds
  • 449
  • 2
  • 10
  • 1
    No idea, but no need to allocate a third array at all -- you can just use a single temporary Region variable since you are doing it index by index. – Jeremy Apr 26 '12 at 01:42
  • 1
    Why aren't both pointers instead of having a pointer and an array? – slartibartfast Apr 26 '12 at 01:43
  • 1
    @Jeremy, or use `std::swap()`. – Wyzard Apr 26 '12 at 01:44
  • @myrkos myRegions and myRegionsLast are arrays of type Region and size 1000. swap is just a pointer to a region, just to swap the two. – eds Apr 26 '12 at 01:44
  • 1
    If the array is fixed size and you're in C++ land, `std::array::swap` might be a good alternative (similar to Wyzard's comment). – mavam Apr 26 '12 at 01:45
  • @myrkos I guess I see what you mean, just make them both dynamically allocated instead of both fixed size. – eds Apr 26 '12 at 01:47
  • 1
    @Wyzard: You can make your comment an answer. (I'd upvote it.) – thb Apr 26 '12 at 01:47
  • This sort of thing is not only covered by standard textbooks and tutorials, but it's also an SO duplicate: http://stackoverflow.com/q/3393518/777186 – jogojapan Apr 26 '12 at 04:34

3 Answers3

6

This happens because you cannot assign a whole array at once. You can assign a pointer, however:

Region reg1[1000], reg2[1000];
Region *myRegionsLast = reg1;
Region *myRegions = reg2;

Now your swap routine is going to work without further modifications.

You could also swap arrays one element at a time, but it is going to involve a lot more data copying.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

How about creating just a variable of the content of Region type and iterating through it...

for(int i=0; i<1000; i++)
{
    tempRegionTypeVariable = myRegionsLast[i];
    myRegionsLast[i] = myRegions[i];
    myRegions[i] = tempRegionTypeVariable;
}

Hope this helps.. Here "tempRegionTypeVariable" is just a temporary variable or an Object. Not an Array..

LearningDeveloper
  • 638
  • 2
  • 11
  • 23
  • I'd prefer to just switch the addresses instead of swapping every variable, though, as speed is very crucial for this application! – eds Apr 26 '12 at 01:52
  • It's a good thought, but there is a much faster way, as Wyzard and Dasblinkenlight have noted. – thb Apr 26 '12 at 01:53
  • Yup.. If speed is a critical component then my method is not the best. It's the simplest and easiest though.. :) – LearningDeveloper Apr 26 '12 at 04:10
0

A couple other valid answers were posted in the comments:

  1. Use std::swap() / std::array::swap
  2. Make them dynamically allocated arrays instead of static
eds
  • 449
  • 2
  • 10