When writing list management code, it is necessary to be able to swap data elements. This article looks at ways to use swapping techniques in C programming.
When creating projects that manage data, be it in a list, file, or any other form of data storage, it is often necessary to swap data elements. The most common reason for wanting to swap two data elements is when sorting the list.
When we talk about swapping and swapping techniques in C programming, we mean the ability to either:
In the first instance, we are taking a variable that contains an item of data, and swapping it with another variable containing the same type. The actual values are sapped, so that variable one contains the value of variable two, and variable two contains the value of variable one.
Reference swapping performs the same function, but it does not actually swap the value, but only a pointer to that value in memory. This has benefits, and drawbacks, which we shall discuss presently.
Consider an array of a known length, containing numerical data elements. We might have declared such an array thus:
This contains an array of 50 items, indexed 0 through 49. Each item, or element, is an integer; this being a built-in C data type. Since it is a built-in data type, we can directly assign values to each element.
The swapping process takes three stages:
The C code for this is actually simpler than the description:
Of course, the above makes more sense when placed into a function:
Please feel free to clip the code sample, and use it in your own projects. Of course, if you need to swap different data types, then the code will need to be adjusted.
If we have a user defined structure, the above solution will not work, as the data structure will not have a built-in copy operator. In other words, we can not assign the value of a data structure from one variable to another.
Instead, we need to apply the theory as follows:
It is important to note that each member of the structure must be copied, one at a time. If there are nested structures within the structure, these must be copied in the same way. This makes the process much more involved, and much more tedious.
The solution to the above problem is to swap references rather than actual objects. This requires that we have performed a few pre-requistite operations:
With these three items in place, we can perform the exact same process as for swapping items in an array consisting of built-in types. This is possible because the pointer is also a built-in type. The code for swapping pointers is akin to:
The 'void *' data type is used to indicate that the pointer points to data of a type that is not necessarily known at the time the code is written. It will, therefore, need to be cast when the program is compiled, to the required data type.
The most efficient way to swap to data elements is to swap a pair of pointers. However, the programming overhead required to do this might negate the performance increase. A simple array of built-in types might be better swapped using Array Swapping, while a full struct containing user defined data types might benefit more from Pointer Swapping.
The reason for this is simple - a user defined structure contains more data, and the act of copying each item from one place to another will be more computationally expensive than swapping two pointers. However, a pointer has the same size as many built-in data types, and so swapping an integer (for example) by reference will not save as much time, as it requires copying the same number of bytes as the pointer.