Good afternoon. How can I proceed to free the memory allocated to tmp?
Code:/* * File: memswap.cpp * Author: thames * * Created on 7 de Janeiro de 2013, 14:46 */ #include <cstdlib> #include <iostream> #include <cstring> int swap(void* x, void* y, size_t size) { using std::memcpy; decltype(x) tmp; if( (tmp = new size_t) == nullptr) return -1; memcpy(tmp, x, size); memcpy(x, y, size); memcpy(y, tmp, size); // delete tmp; return 0; } int main() { using std::cout; using std::endl; int x = 10; int y = 20; size_t size = sizeof(int); if(swap(&x, &y, size) == 0) { cout << x << " " << y << endl; return EXIT_SUCCESS; } else { cout << "An error has occurred.\n"; return EXIT_FAILURE; } }



4Likes
LinkBack URL
About LinkBacks



. That cast made the trick!

