Hi guys,
I'm hoping someone can explain the behavior of this code for me. I don't understand why the void pointer passed to testb doesn't continue pointing to the allocated integer after the function call returns.
Code:
#include <stdio.h>
#include <iostream>
void* testa()
{
return new int(1);
}
void testb(void* test)
{
test = (void*) new int(2);
printf("Value of void* in testb: %u\n", *static_cast<unsigned int*>(test)); // okay
}
int _tmain(int argc, _TCHAR* argv[])
{
void* testaptr = testa();
printf("Value of void*: %u\n", *static_cast<unsigned int*>(testaptr)); // okay
void* testbptr = nullptr;
testb(testbptr);
printf("Value of void*: %u\n", *static_cast<unsigned int*>(testbptr)); // access violation reading testbptr
getchar();
return 0;
}
Thanks a lot.