Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can rawbytestring cleared from memory ?
#3
(09-11-2018, 04:25 PM)Madammar Wrote: how possibly i can zeromemory rawbytestring ?

The EXACT same way you zero out a String, per our previous discussion, but just using AnsiChar instead of Char to calculate the byte count.

As I mentioned earlier, instead of a manual loop, you can use the Win32 ZeroMemory() function (Windows only):

Code:
if S <> '' then
  ZeroMemory(@S[1], Length(S));

Alternatively:

Code:
ZeroMemory(PAnsiChar(S), Length(S));

Or, you can use the RTL's FillChar() function (all platforms):

Code:
if S <> '' then
  FillChar(S[1], Length(S), #0); // or [0] on ARC platforms, unless {$ZEROBASEDSTRINGS OFF} is used

Alternatively:

Code:
FillChar(PAnsiChar(S)^, Length(S), #0);

I generally prefer to use the typecast, because:

1. it does not require a manual check to see if the string is empty before dereferencing it. The typecast always returns a valid pointer, and passing a length of 0 is effectively a no-op.

2. it is not subject to 0-based or 1-based indexing based in platform.

On the other hand, if you want to avoid the overhead of a function call, then do check for an empty string first. But the overhead is pretty minimal.

Also, per our previous discussion, make sure that the RawByteString you are zeroing has a reference count of exactly 1, not -1 (read-only string literal) or > 1 (multiple string variables referencing the same physical data). Use the RTL's UniqueString() to ensure that, if needed.

Reply


Messages In This Thread
RE: can rawbytestring cleared from memory ? - by rlebeau - 09-13-2018, 08:21 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)