views:

141

answers:

2

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
+4  A: 
  • memset fills a number of bytes with the specified value. In Delphi, we use FillChar for this. But if the value we want to fill with is zero, you can also use the ZeroMemory function.

  • memcpy copies a block of bytes from one location to another (in RAM). In Delphi, we use Move for this. You can also use CopyMemory (or the identical function MoveMemory) if you want to work with pointers instead of Delphi variables.

That is,

Move(a, b, n)

copies n bytes from the data named a to the location of b, where a and b are variables. This is equivalent to

CopyMemory(@b, @a, n)

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 and q are pointers, you can do

CopyMemory(q, p, n)

or

Move(p^, q^, n).

You might also want to know how to allocate, reallocate, and free memory on the heap in Delphi. You use the GetMem, ReallocMem, and FreeMem 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

myPtr + 200

then you can do

cardinal(myPtr) + 200.
Andreas Rejbrand
Thanx for the reply, but how can i put the "szRawBuf+(QSIZE*nBufIter)" to this parameter? it said the second parameter was a pointer. how does the 'szRawBuf + (QSIZE*nBufIter)' posible?
XBasic3000
OK i got it. it is not posible to move(..) but the CopyMemory(..) does. thnx
XBasic3000
@XBasic3000: If `CopyMemory(a, b, n)` works, then so does `Move(b^, a^, n)`. (But maybe you need to cast it like `Move(PSomeType(b)^, PSomeType(a)^, n`.)
Andreas Rejbrand
yeh your the best!
XBasic3000
Why a downvote? Please elaborate.
Andreas Rejbrand
@XBasic3000: Glad you got the help you needed. Can you post the code you ended up with?
Merlyn Morgan-Graham
+2  A: 

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?

XBasic3000
Nah, that looks good. Glad you got it working :)
Merlyn Morgan-Graham
+1 and vote for re-open; I agree this is a real question about translating from C/C++ to Delphi.
Jeroen Pluimers
@Jeroen Pluimers, thanx. more power to you.
XBasic3000