Casting and dereferencing of pointer

From: sed <hidden>
Date: 2016-06-15 22:45:10

Maybe I should not post in this group but anyway...

Please look at my code that do the same, except of endianness.
static void _parseItems(const unsigned char *pBuffer)
{
  unsigned int itemSize;
  itemSize = *((unsigned int*)pBuffer); // this give system fault
  memcpy(&itemSize, pBuffer, sizeof(unsigned int)); // this works well
  .......
}

I'm not very experienced with C so I use git as example of good written code.
In object.c I've found two functions that looks like my one.

static unsigned int hash_obj(struct object *obj, unsigned int n)
{
	unsigned int hash = *(unsigned int *)obj->sha1;
	return hash % n;
}

static int hashtable_index(const unsigned char *sha1)
{
	unsigned int i;
	memcpy(&i, sha1, sizeof(unsigned int));
	return (int)(i % obj_hash_size);
}

I wonder why in the second used memcpy instead of:
unsigned int i = *(unsigned int *)sha1
Maybe there is explanation that will help to solve my problem.
Thank you
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help