+ Reply to Thread
Results 1 to 2 of 2

Thread: What is wrong with below code?

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

    What is wrong with below code?

    I have below code and it does not work as expected:

    Code:
    void GetNewValue(char *pData)
    {
    	pData = new char[100] ;
    	strcpy(pData, "my data") ;
    }
    
    void AllocateData()
    {
    	char *pData = NULL ;
    	GetNewValue(pData) ;
    
            //here, in debugger, I can see that pData is NULL.
    	OutputDebugString(pData) ;
    }
    After GetNewValue() function executes, in debugger, I can see that pData is NULL. This is strange.

    What is the issue?

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    37
    Code:
    void GetNewValue(char **pData)
    {
    	*pData = new char[100] ;
    	strcpy(*pData, "my data") ;
    }
    
    void AllocateData()
    {
    	char *pData = NULL ;
    	GetNewValue(&pData) ;
    	OutputDebugString(pData) ;
    
    	delete []pData ;
    	pData = NULL ;
    }
    Above example works for me.

+ Reply to 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