Access violation reading void* assigned dynamically allocated value inside function
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.
Re: Access violation reading void* assigned dynamically allocated value inside functi
I just noticed that the pointer is being cast to unsigned int instead of int in my example. I copy pasted from code in my program and forgot to change that. Anyway, since I can't seem to find a way to edit my original post, here's the fixed version (the result is the same)
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: %d\n", *static_cast<int*>(test)); // okay
}
int _tmain(int argc, _TCHAR* argv[])
{
void* testaptr = testa();
printf("Value of void*: %d\n", *static_cast<int*>(testaptr)); // okay
void* testbptr = nullptr;
testb(testbptr);
printf("Value of void*: %d\n", *static_cast<int*>(testbptr)); // access violation reading testbptr
getchar();
return 0;
}
Re: Access violation reading void* assigned dynamically allocated value inside functi
Because you're passing the pointer by value instead of by reference. The pointer in testb is not the same pointer as in _tmain. Change your function declaration to
void testb(void*& test)
Re: Access violation reading void* assigned dynamically allocated value inside functi
In your original code, test is passed by value in function testb. This means that any changes done to test within that function are 'lost' when the function returns and are not passed back to the calling function. In order for changes to a function argument to be 'passed back' to the calling function, they need to be passed by reference rather than by value.
See the marked change in the code below to pass by reference.
Code:
void testb(void* &test)
{
test = (void*) new int(2);
printf("Value of void* in testb: %u\n", *static_cast<unsigned int*>(test)); // okay
}
Re: Access violation reading void* assigned dynamically allocated value inside functi
Quote:
Originally Posted by
2kaud
In your original code, test is passed by value in function testb. This means that any changes done to test within that function are 'lost' when the function returns and are not passed back to the calling function. In order for changes to a function argument to be 'passed back' to the calling function, they need to be passed by reference rather than by value.
See the marked change in the code below to pass by reference.
Code:
void testb(void* &test)
{
test = (void*) new int(2);
printf("Value of void* in testb: %u\n", *static_cast<unsigned int*>(test)); // okay
}
Oh, that makes sense, thanks. I didn't realize you had to do that with pointers.
Re: Access violation reading void* assigned dynamically allocated value inside functi
Quote:
Originally Posted by
Tanaka
Oh, that makes sense, thanks. I didn't realize you had to do that with pointers.
You're not quite understanding the underlying concept.
A pointer is no different than an int, double, float, etc. It is just another variable type. Therefore to change the value of a variable in a function, what steps do you need to do? For example:
Code:
void foo(int x)
{
++x;
}
int main()
{
int z = 10;
foo(z);
}
What is the value of "z" after foo() is called? Is it 11? If not, why not? The code increments the value passed, so why is z still 10?
OK, if you understand that, then how do you fix the code so that when you pass "z", it indeed changes to 11? The answer is no different than the one given to you previously in the pointer case:
Code:
void foo(int & x)
{
++x;
}
int main()
{
int z = 10;
foo(z);
}
Now z is equal to 11 when foo() returns.
Regards,
Paul McKenzie