Home > Software engineering >  Questions about learning smart Pointers
Questions about learning smart Pointers

Time:09-21

I want to send this method returns a pointer (szResult) memory of automatic release, to do so? If not to do so, how should do? Thank you,,,, in the study,
Char * ReadString (char * szSection, char * szKey, const char * szDefaultValue)
{
Char * szResult=new char [MAXSTRINGLEN];
STD: : from & lt; Char> PResult (szResult);//add smart Pointers
Memset (szResult, 0 x00 MAXSTRINGLEN);
GetPrivateProfileString (LPCTSTR szSection, LPCTSTR szKey,
(LPCTSTR) szDefaultValue, szResult MAXSTRINGLEN, LPCTSTR m_szFileName);
Return szResult;
}

CodePudding user response:

 TCHAR * ReadString (TCHAR * szSection, TCHAR * szKey, const TCHAR * szDefaultValue) 
{
The static TCHAR szResult [MAXSTRINGLEN];
Memset (szResult, 0 x00, MAXSTRINGLEN * sizeof (TCHAR));
GetPrivateProfileString (LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szDefaultValue, szResult, MAXSTRINGLEN, LPCTSTR m_szFileName);
Return szResult;
}

CodePudding user response:

Don't return is empty? That you still have what meaning this function

CodePudding user response:

Static is automatically released?

CodePudding user response:

Must not, szResult is your return value, return after you release the pointer, the return value and purpose?
I think you should put your smart Pointers snippet to call this function

CodePudding user response:

I'm wrong and you are using a static array, thanks zhao four teachers

CodePudding user response:

Thank you, friends who

CodePudding user response:

Ask zhao four teachers, checked the, static array is automatically released after the function, isn't it? So, the return value is no problem?

CodePudding user response:

Life cycle is long, static char [],
Such use, single thread no problem, multithreading,,, out of the question,

CodePudding user response:

Smart Pointers can not exceed the scope to use, it is recommended that the passed in parameter way, who apply for destruction of

LPCTSTR ReadString (LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szDefaultValue, LPTSTR szResult, int iBuffSize)
{
GetPrivateProfileString (szSection szKey, szDefaultValue szResult, iBuffSize, m_szFileName);
Return szResult;
}




CodePudding user response:

refer to 7th floor junccc response:
ask zhao four teachers, checked the, static array is automatically released after the function, isn't it? So, the return value is no problem?


Static variable life cycle is the process of life cycle, so after the function call will not be released, but there are multiple threads to read and write when they go wrong,

CodePudding user response:

Multi-threaded version:
 TCHAR * ReadString (TCHAR * szSection, TCHAR * szKey, const TCHAR * szDefaultValue) 
{
The static TCHAR szResult [MAX_THREAD_NUM] [MAXSTRINGLEN];
Int dar;
Tid=GetCurrentThreadId () % MAX_THREAD_NUM;
Memset (szResult (dar), 0 x00, MAXSTRINGLEN * sizeof (TCHAR));
GetPrivateProfileString (LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szDefaultValue, szResult (dar), MAXSTRINGLEN, LPCTSTR m_szFileName);
Return szResult (dar);
}

CodePudding user response:


can

CodePudding user response:

Should not return szResult but pResult, then assigned to a smart pointer, function prototypes we need to make a corresponding change

CodePudding user response:

Smart Pointers obviously than static array,

CodePudding user response:

On the 14th floor zhao4zhong1
reference response:
smart Pointers obviously than static array,


A static array space is limited

CodePudding user response:

Smart Pointers space is limited,
In the real world, in addition to the time and space could be infinite, any other things are limited,
  • Related