UtilityTest.cpp 和 utility.c 是两个不同的项目,但只有一个解决方案。
在UtilityTest.cpp中,在调用'BytesToString(pbArray,5)'后,它无法免费。为什么呢?
我们不能在c中分配内存而在c ++中是免费的吗?
UtilityTest.cpp
[Owner("SHRI")]
[TestMethod]
[TestCategory("Automatic")]
[Description("Convert byte to string")]
void ConvertByteToString()
{
unsigned char pbArray[5] = { 0x41, 0x42, 0x43, 0x44, 0x45 };
char* pbExpArray = NULL;
unsigned int Flag = 1;
int len = 0;
pbExpArray = BytesToString(pbArray,5);
len=strlen(pbExpArray);
Flag=strcmp("41 42 43 44 45 ",pbExpArray);
free(pbExpArray);
}
utility.c
char* BytesToString( const unsigned char* PpcbBytes, const unsigned int PuiNoOfBytes)
{
unsigned char bRetVal = 0;
char tempStr[5];
unsigned int ctr = 0;
char* PpszString = NULL;
do
{
PpszString=(char*)calloc(PuiNoOfBytes*3+1,sizeof(char));
if(NULL==PpszString)
break;
for(;ctr<PuiNoOfBytes; ctr++)
{
sprintf(tempStr, "%02X ", PpcbBytes[ctr]);
strcat(PpszString, tempStr);
}
} while(0);
return PpszString;
}