News for hackers. Stuff that matters.
Re:Android Project Changeset 4f8b683: libc/memset.c by hoelle on Tuesday May 25, 2010 @08:51PM (#1379184)
I'll offer the tip that even if you have a fast memset, you can and should avoid using it when possible.

Seeing memset is usually a sign that someone is being lazy about watching the length of their buffers and/or verifying their copies aren't off-by-one.

Re:Android Project Changeset 4f8b683: libc/memset.c by scott_s on Tuesday May 25, 2010 @11:09PM (#1379403)
If I understand you correctly, you think programmers should say:

  int i;
  for (i = 0; i < SIZE; ++i) {
    buffer[i] = 0;
  }
Instead of:

  memset(buffer, 0, SIZE)
Because it indicates better buffer management.
Re:Android Project Changeset 4f8b683: libc/memset.c by SamReidHughes on Wednesday May 26, 2010 @04:18AM (#1379805)
If I understand hoelle's point, it is that you shouldn't need to fill the buffer with a constant value in the first place. Why do people do it? The only time you generally need to prefill something is if you're using some algorithm whose initial state includes a prefilled array. It's hard to find a place where a byte array needs to be memset.

If you need an array zeroed on first use, why didn't you use calloc? The only reason memset has for existing is when you need to reinitialize an existing array to zero. If things are so tight that you're wiping out an existing array and reusing it, which usually involves being deliberately less abstract than you'd naturally be, then, well, that's a pretty unusual situation.

(While we're on memset, I'd like to point out that the weirdest thing is that while memset initializes value byte by byte, the places where you do need a prefilled array are always places where the array is an array of larger values.)

Re:Android Project Changeset 4f8b683: libc/memset.c by scott_s on Wednesday May 26, 2010 @08:17AM (#1380085)
Sometimes you manage your own memory, so you can't use calloc. Sometimes you're zeroing out stack allocated memory, such as the common practice when doing socket programming in C.

Setting memory you own to a known value is just good, defensive programming. If I screw up - and in C, you're going to screw up - it's good to see a known value rather than unknown values.

Re:Android Project Changeset 4f8b683: libc/memset.c by hoelle on Wednesday May 26, 2010 @11:08AM (#1380445)
Removing the memsets from the networking layer of one of my projects basically halved the frametime of that system (which was a bottleneck).

I'm all for defensive programming, but when you need to be fast, you'll just have to make sure it's correct.

Re:Android Project Changeset 4f8b683: libc/memset.c by prodigal_erik on Tuesday May 25, 2010 @11:47PM (#1379451)
I took it as saying you should remember how much of the buffer you have actually used. If you only read what you write, it doesn't matter whether the rest of the buffer is filled with \0 or not, so you can skip the memset.
Re:Android Project Changeset 4f8b683: libc/memset.c by np3000 on Tuesday May 25, 2010 @11:39PM (#1379438)
it should be memset(buffer,0,SIZE*sizeof(int))
Re:Android Project Changeset 4f8b683: libc/memset.c by scott_s on Tuesday May 25, 2010 @11:41PM (#1379441)
Only if it's a buffer of ints. I had assumed it was buffer of unstructured memory, which would be chars and of 1 byte each.