+ Reply to Thread
Results 1 to 2 of 2

Thread: Is it safe to use pointers inside an union?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    46

    Is it safe to use pointers inside an union?

    Is it safe to use pointers inside an union?

    Thanks.

  2. #2
    It is not safe to use pointers in an union.

    Below example will crash!

    Code:
    void UseUnion()
    {
            union Example
    	{
    		double*	pData1 ;
    		int*	pData2 ;
    	} objExample ;
    
    	objExample.pData1 = new double ;
    	objExample.pData2 = new int ;
    
    	delete objExample.pData1 ;
    	delete objExample.pData2 ;
    
    }
    So avoid using pointers inside an union.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts