git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Casting and dereferencing of pointer

From
Ssed <sed.nivo@gmail.com>
Date
Aug 16, 2008, 09:33 UTC
Message-ID
<loom.20080816T093019-717@post.gmane.org>
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

Next: Kalle Olavi Niemitalo
Message 1 of 2 in “Casting and dereferencing of pointer”
  1. sedAug 16, 2008
  2. Kalle Olavi NiemitaloAug 16, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.