-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemorySwap.cpp
More file actions
32 lines (24 loc) · 1.32 KB
/
MemorySwap.cpp
File metadata and controls
32 lines (24 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//#include <stdio.h>
#include <iostream>
#include <tchar.h>
using std::cout;
int _tmain(int argc, _TCHAR* argv[])
{
int ARRAY_DATA[] = {100, 200}; //Array data
unsigned char *Mem_Loc = (unsigned char*)ARRAY_DATA; //Memory location of the array data
int sizeD = sizeof(ARRAY_DATA[0]); //data type size of the array data
cout << "Before swap: " << ARRAY_DATA[0]<<", "<<ARRAY_DATA[1]<<"\n";
//printf("Before swap: %d, %d \n", ARRAY_DATA[0],ARRAY_DATA[1]); //--> This is comand for C
unsigned char* temp = new unsigned char[sizeD]; //temporary variable to perform swap
//unsigned char* temp = (unsigned char*) malloc(sizeD); //--> This is comand for C
//Memory Swap------------------------------------------------------------------------------
memcpy(temp, Mem_Loc + 0 * sizeD, sizeD); //Commit test
memcpy(Mem_Loc + 0 * sizeD, Mem_Loc + 1 * sizeD, sizeD);
memcpy(Mem_Loc + 1 * sizeD, temp, sizeD);
//-----------------------------------------------------------------------------------------
delete [] temp; //memory clean up for the temporary variable (prevent memory leak)
//free(temp); //--> This is comand for C
cout << "After swap: " << ARRAY_DATA[0]<<", "<<ARRAY_DATA[1]<<"\n";
//printf("After swap: %d, %d \n", ARRAY_DATA[0],ARRAY_DATA[1]); //--> This is comand for C
return 0;
}