CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2008
    Posts
    8

    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.

  2. #2
    Join Date
    Jul 2008
    Posts
    8

    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;
    }

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  5. #5
    Join Date
    Jul 2008
    Posts
    8

    Re: Access violation reading void* assigned dynamically allocated value inside functi

    Quote Originally Posted by 2kaud View Post
    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.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Access violation reading void* assigned dynamically allocated value inside functi

    Quote Originally Posted by Tanaka View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured