Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

swap function, heap and void * example

Got an idea for a practice project / debug snippet / short-code contest / etc? Tell us about it here!

swap function, heap and void * example

Postby TimNatale on Mon Mar 23, 2009 10:17 pm

Below is a function for swapping the contents of 2 variables that are of the same data type. The function can handle any type but please note that variables vp1 and vp2 must be the same type, it uses void * in the first 2 parameters of the function, which yield the starting address only, the size parameter is vital to swapping the contents properly from one variable to the other. If you send the function two variables of different types you will get unpredictable results.

After going thru the last few video lessons you have there antiRTFM (awesome by the way!), I stumbled upon a situation where I had to use the heap, the code I have commented out shows what the compiler doesn't like, see the comments in the swap function below:

Code: Select all
#include "stdafx.h"
#include <iostream>
#define _CRT_SECURE_DEPRECATE_MEMORY
#include <memory.h>

using namespace std;

void swap(void *vp1, void *vp2, int size);

int main()
{   
   int z = 8;
   int a = 1929;
      
   cout << "before" << endl;
   cout << "z " << z << endl;
   cout << "a " << a << endl;

   swap(&z, &a, sizeof(int));  //swap z with a
   
   cout << "after" << endl;
   cout << "z " << z << endl;
   cout << "a " << a << endl;
   
   cin.get();
   return 0;
}

void swap(void *vp1, void *vp2, int size)
{
    // the compiler will display errors saying that it cannot create an array of size zero:
    // char temp[size]
    // so I replace that with this:
   char * temp = new char[size];  // using the heap in this manner allows for the dynamic creation
                       // of the temp buffer based on size, it will compile with no errors

   memcpy(temp, vp1, size);   // the memcpy function from memory.h is used to swap the variables
   memcpy(vp1, vp2, size);
   memcpy(vp2, temp, size);

   delete [] temp; temp = 0;   // clean up the heap
}
TimNatale
 
Posts: 1
Joined: Mon Mar 23, 2009 9:54 pm

Re: swap function, heap and void * example

Postby antiRTFM on Mon Mar 23, 2009 10:53 pm

gracious holy lord TimNatale, you are exposing some deep dark unholy secrets of C++ which the innocent and young generation must not know of, lest they will stray from the path and excercise such dangerous practices, and they shall forever be damned from the face of the earth...

Thou shalt not make use of the profane "memcpy" in this day and age, rather use std::copy
Thou shalt not make use of headers ending in ".h", rather find their standard c++ replacement and use it

and most of all, thou shalt forever seperate thyself from the impure forces of the void*
User avatar
antiRTFM
Administrator
 
Posts: 462
Joined: Sun Apr 13, 2008 9:10 am


Return to -- Code Practice Submissions --

Who is online

Users browsing this forum: No registered users and 0 guests