我的功能如下:
void Dump(uint32_t sig, const void *buf, int bytes_to_send) {
}
需要从UT代码调用。
我尝试使用以下代码段来调用它:
方法一:
// string to char* - but thats not what I want
string *pStr = new string("Where is my place in Memory?");
uint32_t sig = 4;
Dump(sig,pStr,100);
方法2:
// char to string
char tab[4];
tab[0] = 'c';
tab[1] = 'a';
tab[2] = 't';
tab[3] = '\0';
Dump(4,&tab,sizeof(tab));
方法3:
int i=0;
char aPointer[] = "I love earth and all its creatures.";
char *pCarrier[sizeof(aPointer)];
for (i = 0; i < sizeof(aPointer); i++)
pCarrier[i] = &aPointer[i];
Dump(4,pCarrier ,sizeof(aPointer));
Everything Compiles但RunTime行为是错误,如
Invalid read of size 2
==16492== at 0x4A08DAC
==16492== Invalid read of size 8
Memcheck:Addr2
其他错误:
==24108== Use of uninitialised value of size 8
请分享一些朋友的建议!!
答案 0 :(得分:1)
Dump()
不知道string*
是什么
或者怎么做tab
而非tab
的地址而无法正常工作。aPointer
的地址填充数组。尝试:
char message[] = "My message";
const size_t len = strlen(message);
Dump(4, message, len);