Home > Software engineering >  Memory allocation and release of question
Memory allocation and release of question

Time:09-27

String AesEncryptor: : EncryptString (string strInfor) {
Int nLength=strInfor. Length ();
Int spaceLength=16 - (16) nLength %;
Unsigned char * pBuffer=new unsigned char [nLength + spaceLength];
Memset (pBuffer, '\ 0', nLength + spaceLength);
Memcpy_s (pBuffer, nLength + spaceLength, strInfor c_str (), nLength);
M_pEncryptor - & gt; Cipher (pBuffer, nLength + spaceLength);

//here need to get an array of characters into a hexadecimal string
Char * pOut=new char * (nLength + spaceLength) [2];
Memset (pOut, '\ 0', 2 * (nLength + spaceLength));
Byte2Hex (pBuffer, nLength + spaceLength, pOut);

String retValue (pOut);
The delete [] pBuffer;
The delete [] pOut;
Return retValue;
}

In the delete [] pOut times wrong, the pop-up dialog: Windows have been * * *. Exe triggered a breakpoint, the reason may be that the heap is damaged

CodePudding user response:

Without experts to guide?

CodePudding user response:

Int nLength=strInfor. Length ();
If the length is not big, not new delete
Char Out [4096].

CodePudding user response:

People think is associated with the "copy-on-write" feature of string
 string retValue (pOut); 

Based on the characteristics of "copy-on-write retValue pOut and point to the same block of memory, at the end of the program, the string class destructor has released the memory, and then call
 delete [] pOut. 

Release the memory has been released, there will be an error!

CodePudding user response:

Byte2Hex is how to write? An estimated cross-border issues

CodePudding user response:

Your transformation is beyond pOut are length,
You set the pOut long point, or who wrote it function asked how long the length of the definition, right under the

Remember to points,

CodePudding user response:

Byte2Hex (pBuffer, nLength + spaceLength, pOut);//the other possible conversion of cross-border,

You can try to put this article commented out, and see if there is no problem, if there is a problem, it is the other errors,

CodePudding user response:


Either in c method, buff and buffsize call transfer, if the saved data is over bufsize is return a negative error code, if the data is large enough to hold, then return 1

Either string retValue (pOut); Change
String retValue;
retValue=https://bbs.csdn.net/topics/pOut;

This is copy within shallow and deep copy those things, are not ripe is avoided

CodePudding user response:

Post code, remember to choose the format of C/C + +,
  • Related