-
September 16th, 2013, 09:10 AM
#1
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.
-
September 16th, 2013, 09:16 AM
#2
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;
}
-
September 16th, 2013, 09:57 AM
#3
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)
-
September 16th, 2013, 09:59 AM
#4
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
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 16th, 2013, 10:27 AM
#5
Re: Access violation reading void* assigned dynamically allocated value inside functi
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.
-
September 16th, 2013, 11:25 AM
#6
Re: Access violation reading void* assigned dynamically allocated value inside functi
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|