[英]Storing a char* value into a unsigned char** array
I'm creating a C++ wrapper for a third-party .dll in order to use it with C# P/Invoke. 我正在为第三方.dll创建C ++包装程序,以便将其与C#P / Invoke一起使用。 To do so, I have access to a demo application that uses this .dll and as I'm no C expert, I'm heavily dependent on this code. 为此,我可以访问使用此.dll的演示应用程序,并且由于我不是C专家,所以我在很大程度上依赖此代码。 Everything works fine, but a couple of weeks ago, I came across a bit of code that I found rather odd: 一切正常,但是几周前,我遇到了一些我觉得很奇怪的代码:
unsigned char** ppucValues = new unsigned char*[iSize];
for(int i = 0; i < iSize; i++){
ppucValues[i] = (unsigned char *)new int;
*(int *)ppucValues[i] = piValues[i]; //(piValues is int*)
}
Those lines inside the for-loop puzzled me for a while but looking closely at the first line, I figured "hey, that must mean that an unsigned char is pointing to an int... right?¹" for循环内的那些行使我有些困惑,但仔细观察第一行,我想到“嘿,那必须表示一个未签名的char指向一个整数...对吗?”
Yeah, I'm still not so sure about that, but I've used the same concept for several different types and it works. 是的,我仍然不太确定,但是我对几种不同的类型使用了相同的概念,但它确实有效。 In case anyone is wondering the reason for that, I have to store differently typed values into this unsigned char** and pass it ahead to a method in the third-party .dll that takes, obviously, this unsigned char**. 万一有人想知道原因,我必须将不同类型的值存储到该无符号char **中,并将其传递给采用该无符号char **的第三方.dll中的方法。 That's how the manufacturer did in their demo app, so I'm just going with it. 这就是制造商在其演示应用程序中所做的工作,因此我将继续进行下去。
With that out of the way, on to my problem: I have to do this same operation but now I'm taking a char** (array of strings) instead of an int* or double* or whatever. 解决了这个问题:我必须执行相同的操作,但是现在我使用的是char **(字符串数组),而不是int *或double *或其他任何东西。 Based on what I just said, this is what I came up with: 根据我刚才所说的,这是我想到的:
for(int i = 0; i < iSize ; i++){
ppucValues[i] = (unsigned char *) new char*; //type of ppucValues as unsigned char** and type of ppucValues[i] as char*???
*(char **)ppucValues[i] = ppcValues[i]; //Hmmm... what?!
}
But it doesn't work quite as I expected (not to mention that I was just 50% confident on it). 但这并不能达到我的预期(更不用说我对此只有50%的信心)。 For instance, if my input (ppcValues[i]) is "test", my output (ppucValues[i]) turns out as "¨Ô4". 例如,如果我的输入(ppcValues [i])是“ test”,那么我的输出(ppucValues [i])则显示为“?4”。 I could risk suggesting that this is an encoding issue that has nothing to do with C++ or its pointers, but I'll leave suggestions to the kind people here at StackOverflow. 我可能会冒险建议这是一个与C ++或其指针无关的编码问题,但是我会在StackOverflow上给那些友好的人一些建议。 Any help would be appreciated. 任何帮助,将不胜感激。
¹In case I'm wrong, please enlighten me! ¹万一我错了,请赐教!
Without more context it's hard to give a definitive solution, but if the lifetime of ppcValues
is at least as long (or longer, or course) than ppucValues
it is a simple as just copying the pointers , like 没有更多上下文,很难给出确定的解决方案,但是如果ppcValues
的生存期至少与ppcValues
一样长(或更长ppucValues
,则就像复制指针一样简单,例如
for(int i = 0; i < iSize ; i++){
ppucValues[i] = reinterpret_cast<unsigned char*>(ppcValues[i]);
}
If the lifetime of ppcValues
is shorter than the lifetime of ppucValues
, then you need to allocate enough memory to fit all the data in ppcValues[i]
and copy it: 如果寿命ppcValues
比的寿命较短 ppucValues
,那么你就需要分配足够的内存,以适应所有的数据ppcValues[i]
,并复制它:
for(int i = 0; i < iSize ; i++){
ppucValues[i] = new unsigned char[length_of(ppcValues[i])];
std::copy_n(reinterpret_cast<unsigned char*>(ppcValues[i]), ppucValues[i], length_of(ppcValues[i]));
}
The length_of
function depends on the contents of ppcValues[i]
. 所述length_of
功能取决于内容ppcValues[i]
If it's a C-style string then you should be using strlen(ppcValues[i]) + 1
. 如果它是C风格的字符串,则应使用strlen(ppcValues[i]) + 1
。
Also note that if you allocate using new[]
then you need to be freeing with delete[]
. 还要注意,如果使用new[]
进行分配,则需要使用delete[]
进行释放。
But it doesn't work quite as I expected 但这并不完全符合我的预期
That's because strings, ie null terminated char*
s, are not the same as the fundamental types. 这是因为字符串(即以null结尾的char*
与基本类型不同。 You are passing a pointer. 您正在传递一个指针。 If the memory the pointer points to gets deallocated or overwritten by some code before it is processed by the users of ppucValues
, then you are looking at undefined behavior. 如果指针指向的内存在被ppucValues
的用户处理之前被某些代码释放或覆盖,则您正在查看未定义的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.