帮我翻译一下C++德尔福代码
我在 memset 和 memcpy 方面遇到困难。有人可以帮我翻译一下这个,或者关于这个东西如何工作的建议吗?
do{
memset(szSpeechBuf, 0x0, sizeof(char)*QSIZE);
if((nBufIter+1)*QSIZE > nRawBufLen)
{
diff = nRawBufLen - (nBufIter)*QSIZE;
if(diff < 0)
{
printf("DetectSpeech() error : timeout!!!");
exit(1);
}
memcpy(szSpeechBuf, szRawBuf+(QSIZE*nBufIter), diff);
}
else
memcpy(szSpeechBuf, szRawBuf+(QSIZE*nBufIter), sizeof(char)*QSIZE);
} while(1);
// where szSpeechBuf: PAnsiChar; nBufIter: Integer; Const QSIZE = 3200
im having hardtime in memset and memcpy. can somebody trasnlate this for me, or suggestion on how this thing work?
do{
memset(szSpeechBuf, 0x0, sizeof(char)*QSIZE);
if((nBufIter+1)*QSIZE > nRawBufLen)
{
diff = nRawBufLen - (nBufIter)*QSIZE;
if(diff < 0)
{
printf("DetectSpeech() error : timeout!!!");
exit(1);
}
memcpy(szSpeechBuf, szRawBuf+(QSIZE*nBufIter), diff);
}
else
memcpy(szSpeechBuf, szRawBuf+(QSIZE*nBufIter), sizeof(char)*QSIZE);
} while(1);
// where szSpeechBuf: PAnsiChar; nBufIter: Integer; Const QSIZE = 3200
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
memset
用指定的值填充多个字节。在 Delphi 中,我们使用FillChar
来实现此目的。但如果我们要填充的值为零,也可以使用ZeroMemory函数。memcpy
将字节块从一个位置复制到另一个位置(在 RAM 中)。在 Delphi 中,我们使用Move
来实现此目的。如果您想使用指针而不是 Delphi 变量,您还可以使用CopyMemory
(或identical函数MoveMemory
)。即
从名为a的数据中复制n个字节到b的位置,其中a和b是变量。这相当于
@a 和@b 分别是指向源和目标的指针。 (我个人认为,从某种意义上说,后一种语法更容易解释。它需要两个地址,毕竟,这就是我们使用内存的方式。)
因此,如果
p< /code> 和
q
是指针,您可以执行以下操作或者
您可能还想知道如何在 Delphi 中的堆上分配、重新分配和释放内存。您分别使用
GetMem
、ReallocMem
和FreeMem
过程。使用指针
当涉及到指针算术时,Deplhi 可能会受到相当大的限制。但在 32 位系统(例如运行 Delphi 应用程序的系统)上,指针实际上只是一个 32 位无符号整数,即基数。因此,如果您只是告诉编译器这样做,您就可以像基数一样使用指针。
因此,如果编译器不允许,
那么你可以这样做
memset
fills a number of bytes with the specified value. In Delphi, we useFillChar
for this. But if the value we want to fill with is zero, you can also use theZeroMemory
function.memcpy
copies a block of bytes from one location to another (in RAM). In Delphi, we useMove
for this. You can also useCopyMemory
(or the identical functionMoveMemory
) if you want to work with pointers instead of Delphi variables.That is,
copies n bytes from the data named a to the location of b, where a and b are variables. This is equivalent to
where @a and @b are the pointers to the source and destination, respectively. (Personally, I think the latter syntax is easier to explain, in some sense. It takes two addresses, and after all, that is how we work with memory.)
Hence, if
p
andq
are pointers, you can door
You might also want to know how to allocate, reallocate, and free memory on the heap in Delphi. You use the
GetMem
,ReallocMem
, andFreeMem
procedures, respectively.Working with pointers
Deplhi can be rather restrictive when it comes to pointer arithmetics. But on a 32-bit system (such as the ones running Delphi applications), a pointer is really just a 32-bit unsigned integer, that is, a
cardinal
. So you can work with pointers just like cardinals, if you just tell the compiler to do so.Hence, if the compiler doesn't allow
then you can do
经过几天的代码实验,我终于成功了!
@Merlyn Morgan-Graham 让我发布答案,然后这个问题就结束了!他们说这不是真的?因为我自己回答了?
after a days of experimenting the code, i finaly got it work!
@Merlyn Morgan-Graham ask me to post the answer, then this question got closed! they said its not real? because i answerd it myself?