javascript - node.js Buffer not empty -


when create lots of buffers aren't empty:

for (var = 0; < 100; i++) {     console.log((new buffer(30)).tostring('hex')); } 

(partial) output:

782668013a0000003b00000035000000b0c17900391100003c0000003d00 e4216801ffffffff000000000100000000000000000000003e0000003f00 40c27900100000000100000000000000000000000000000018c379000000 000000000000000000000000000000000000000000000000000000000000 --> empty 000000000000000000000000000000000000000000000000000000000000 --> empty 0000000000000000108269014000000041000000c86f79000cf679000000 6611000080c27900c0c27900040000000100000000000000d0c279000000 00000000000000005c2468014200000043000000cc6f7900002668014400 

(partial) output (without .tostring('hex') , new buffer(10)):

<buffer 01 00 00 00 58 db 62 00 b4 86> <buffer 90 b9 65 00 08 00 00 00 03 00> <buffer 10 ba 65 00 04 00 00 00 00 00> <buffer 04 00 00 00 00 00 00 00 00 00> <buffer 10 00 00 00 00 00 00 00 70 ba> <buffer 00 00 00 00 00 00 00 00 00 00> --> empty <buffer 00 00 00 00 00 00 00 00 00 00> --> empty <buffer ff ff ff ff ff ff ff ff 00 00> <buffer 00 00 00 00 0f 00 00 00 8c 6f> <buffer 80 ba 65 00 00 00 00 00 aa 00> 

i'm running node.js v0.10.33 on 32-bit windows 7 machine.

  • is problem specific machine?
  • is general problem (aka. bug) of node.js?
  • is problem of special composition (e.g. windows environments)?
  • is expected , have clear buffer before start using it?
  • is documented?

update: better behaviour on v0.11.14, worse behaviour on v0.8.28

tl;dr

  • not problem specific machine, it's on machines.
  • not bug. behavior exists in memory allocation libraries.
  • not os depended. os behave way.
  • if need initialized, yes, clear using buffer.prototype.fill
  • yes, it's documented in underlying libraries nodejs/webkit use: see malloc(3) / stdlib

this that's expected when working api handles memory allocation, such buffer.

buffer using smalloc module, think of malloc(3) / free(3) stdlib.h

the principle behind malloc allocates/reserves memory pointer - in webkit might closer calloc externalarray on objects.

see http://linux.die.net/man/3/malloc

the malloc() function allocates size bytes , returns pointer allocated memory. the memory not initialized.

memory allocation/deallocation not handle memory initialization, because it's more expensive go through each byte , set 0.

besides 0 not initial value need byte.

and since memory allocation can return block of memory has been used before other process, it's expected have data in newly allocated block.

the general rule is: when allocating memory (such buffer does), if need initialized use buf.fill(0); although not needed since in scenarios when need buffer have knowledge of length of data, implicitly contents.

for instance, when create new buffer(30) know data 30 bytes long, means have idea of data written looks like, in end you'll end writing each byte, before passing buffer else - therefore no need prepend additional initialization loop set each byte 0.

for buffers use bytes flags/states object, initialize those.

e.g. if buffer(10) uses first 2 bytes flags state, , other 8 bytes data, buffer[0] = buffer[1] = 0, set them initial 0 value, instead of initializing 10 bytes, , doing write on last 8 anyway.

one last note: if memory block returned memory allocation hasn't been used before, yes, bytes set 0. however, memory handling os - using sorts of optimisations - therefore it's impossible predict memory segment , contents.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -