a = b or memset/cpy?

memset and memcpy are turning up in profiles a lot. I'd like to speed
things up a bit.

Sometimes it is clear that using = to initialize a local would be
better than memset. I might not gain anything, but at least there's a

However, can I gain performance improvements when zeroing out say some
global element in an array like so:

typedef struct x { int var0; char var1[20]; } X;

X gX[30];

void f(int slot)
X init = {0};

gX[slot] = init;


void f(int slot)
memset(&gX[slot], 0, sizeof(X));


Normally I wouldn't look for a micro-optimization like this but I'm
kind of stuck with the parameters I'm given.

Relevant Pages

  • [PATCH 5/7] rename locking functions - convert init_MUTEX* users, part 3
    ... int init_vfc_devstruct(struct vfc_dev *dev, int instance) ... memset ); ... /* Initialize device descriptor */ ... /* Initialize JFFS2 superblock locks, ...
  • Re: Initialization of very large blocks of memory
    ... After that, we initialize that memory: ... Is there any way to manage performance ot the initializing (not necessarily using memset)? ... Since vallocexplicitly says it doesn't initialize and is managed by ... thread within the kernel... ...
  • Re: [PATCH] sha: prevent removal of memset as dead store in sha1_update()
    ... If the memset is optimized away then the clear out does not occur. ... Furthermore passing a volatile void * ... to a function expecting a void * provokes a compiler warning. ... find a better alternative to barrier() here. ...
  • Re: [bug] block subsystem related crash with latest -git
    ... trying to initialize it late is pointless - it contains crap. ... that's a bug regardless of anything else. ... a memset() is guaranteed to be either a no-op or hiding another bug! ... those members had better be zero. ...
  • Re: Mandating typeof keyword
    ... The only examples I could find were of bind and glob/globfree. ... the use of memset to initialize the pointer values. ... but that's clearly not common practice. ...