[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua 5.4.2 (lua_newstate)
- From: Francisco Olarte <folarte@...>
- Date: Tue, 10 Nov 2020 11:10:59 +0100
On Tue, Nov 10, 2020 at 9:53 AM Ranier Vilela <firstname.lastname@example.org> wrote:
> Ok, Makes sense to not use 0.
> In fact, I never use 0 with pointers, but I've seen a lot of code like this.
> Including IUP Lua.
> Why not memset with (int) NULL, wouldn't work?
Because memset uses whatever you pass it to fill a CHAR array ( you
pass an int because historically many machines did not pass char
arguments, they widened it to int because they used a stack position,
and you could only push something as wide as a register, which
normally was an int as it is supposed to be the fastest ).
So when filling with null using a loop indexing a POINTER array you
write, in x86, four bytes each times. Memset takes the 32 bit 0 int,
extracts the low order 0 byte and filles with it.
If NULL were 0x12345678 a pointer loop will fill mem with 78 56 34 12
78 56 34 12 78 56 34 12 , a memset will just put 78 78 78 78 78 78 78
78 78 .
You should normally not skip the ( pretty weak in C ) type system
using memsets/bzero/wmemset and all their friends unless you have
measured and are pretty sure of what you are doing. And sometimes you
may even make your code slower. Memset needs to be a char-aligned
function, working with char aligned destinations. If you are using
intrinsics a good optimizer may notice you are filling a
pointer-aligned area, but with the pointer loop it will surely notice