Hi, I am creating a function that will take a void * and pass its value to another void *
Here is code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
bool test(void* datatoadd, unsignedlonglong size)
{
staticint i = 0;
void* heapdata = malloc(size);
//Here I want to pass value of datatoadd to heapdata but I keep getting an error
// *heapdata = *datatoadd; have failed
// How can I pass data/value of void pointer to void pointer ?
voidparray[i++] = heapdata;
returntrue;
}
You need to copy the data being pointed to by datatoadd to the memory being pointed to by heapdata.
memcpy(heapdata, datatoadd, size);
Don't forget that using malloc and free is a bad idea and they were replaced by new and delete, and also don't forget that using new and delete is also a bad idea and in modern C++ we really don't do manual memory management unless we really really have to.
You should work out why you're trying to write over memory that doesn't belong to you.
Very little about this function makes any sense.
How have you ended up with data of a completely unknown type?
Why do you need to copy that data? Is the original data going to vanish?
Why is the i value static?
What is creating voidparray? How do you know you're making it large enough? Why aren't you checking that the element i exists before trying to write to it?
The biggest problem with this code is the design. It all looks like a bad design that isn't working with the C++ language.