2

I don't have too much experience managing pointers, and other advanced functions but I was thinking if it is possible to make an algorithm to exchange the memory addresses between two elements of an array using pointers, instead copying and moving the value each time ( example while sorting ). Let me explain:

Imagine this array It has 10 elements unsorted, and I pretend to use bubble-sort to sort it (in any order), the typical way is to use a secondary variable to copy the elements and use it as bypass each time i find a smaller or greater number (depends on descending or increasing sorting).

            *----*----*----*----*----*----*----*----*----*----*
array[10] = | 45 | 21 | 32 | 48 | 32 | 22 | 47 | 10 | 11 | 12 |
            *----*----*----*----*----*----*----*----*----*----*
            ^----- Imagine this is 10000

The program realizes that the array[ 1 ] is smaller than the array[ 0 ], so the bubble-sort will exchange their values (this is the typical way) using a secondary variable as bypass.

                   *--------------* 
                   |              ^
                   |              |
            *--------------*--------------*----*----*----*----*----*----*
array[10] = |      45      |      21      | 32 | 22 | 47 | 10 | 11 | 12 |
            *--------------*--------------*----*----*----*----*----*----*
                   |              ^
                   |   *------*   |
                   *-->|BYPASS|---*
                       *------*

So isn't possible to say something like exchange &array[0] and &array[1] addresses? So i avoid the secondary variable? In this case &array[1] = 10000; and &array[0] = 10004.

Thanks for your attention and all suggestions are allowed!

2
  • No, it's kind of the point of an array that indices correspond to addresses in a simple way. But why would you want to avoid the temp variable? If the things you want to sort are huge, you should sort pointers anyway. Nov 3, 2012 at 15:03
  • If you could somehow change the address of something (and not as in copying it to the new place, but changing the address in place - whatever that means), the world would stop making any kind of sense. Nice avatar though.
    – harold
    Nov 3, 2012 at 15:40

1 Answer 1

1

You can do this

 array[0] += array[1];
 array[1] = array[0] - array[1];
 array[0] -= array[1];

and not use a third variable.

An array is a contigious continuous memory space. You cannot eff up it's ordering. It is not a LinkedList, where you would just swap pointers like that.

3
  • Thanks, You illuminated me when you talked about linked lists Nov 3, 2012 at 15:27
  • I felt that mentioning lists would make you see why you can't do what you wanted to do :) But for mental gymnastics, think about the implications of what ELSE you could do if you could swap addresses in place. How would an array know where to look? How would you know if you hit the end of the array? Would all pointers instantly become pointers-to-pointers or ... ? what does a (char**) mean in that sense? And what about assembler? Linker? Compiler? How would they know what kind of code to generate if you could swap addresses? Stack Pointer? It all stops making sense by being non-deterministic.
    – Shark
    Nov 3, 2012 at 15:47
  • And when your head overloads from all of this, think about cross-compiling, compiling code on one architecture for another. Then think about generating such a cross-compiler. And suddenly you'll see that it just has to work this way - the way it currently does :) Then again, with proper hardware support your idea could be feasible; but it would have to be with a custom-built, hardware-specific compiler on your hardware whose MMU and baremetal allow for memory remapping/reassignment at runtime. It sounds... exotic :)
    – Shark
    Nov 3, 2012 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.