My program below compiles perfectly. But the result is not as expected. That is quite common. The program works fine when the data(&temp) is of type char, but problem arises when it is of type int. Pls advice what went wrong.
#include <iostream.h>
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <malloc.h>
using namespace std;
class data {
unsigned long int d_size;
unsigned short int d_type;
void* d_data;
public:
data();
void setData(void* data, unsigned long int size, unsigned short int type);
const void* getData();
unsigned long int getDataSize();
unsigned short int getDataType();
};
data::data() {
d_size = 0;
d_type = 0;
d_data = NULL;
}
void data::setData(void* data, unsigned long int size, unsigned short int type) {
d_data = malloc(size);
memcpy(d_data, data, size);
d_size = size;
d_type = type;
}
const void* data::getData() {
return d_data;
}
unsigned long int data::getDataSize() {
return d_size;
}
unsigned short int data::getDataType() {
return d_type;
}
const void* box1;
unsigned long int box2;
unsigned short int box3;
int main()
{
data data_member[50];
int temp = 23;
data_member[0].setData(&temp, sizeof(int), 1);
box1 = data_member[0].getData();
box2 = data_member[0].getDataSize();
box3 = data_member[0].getDataType();
cout << (int*)box1 << " " << box2 << " " << box3 << endl;
free((int*)box1);
system("PAUSE");
return 0;
}