• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1787
  • Last Modified:

memcpy + mmap

hi,

i am using mmap and memcpy but i am getting bus error with memcpy.

a = (unsigned int*) mmap(NULL, size, PROT_READ, MAP_PRIVATE, fp, 0);

memcpy(&x,(unsigned int *)a[0],sizeof(unsigned int));

if i do this
x=a[0]; no bus error but when i do above, i got bus error...

thanks for any help.
0
zizi21
Asked:
zizi21
2 Solutions
 
shajithchandranCommented:
i think you should do this

memcpy(&x,(unsigned int *)&a[0],sizeof(unsigned int));

a[0] is the value of array element. you need the address.. so you should do either &a[0] or just a.
0
 
HalfAsleepCommented:
If you want to treat "a" as a pointer to an array, you cannot cast a[0] to an unsigned int* (unless the array contains pointers).

a is the address, or pointer, to the first element in the array.

a == &a[0] (both are pointer values)

*a == a[0] (two different ways of dereferencing to the first element of the array)

Since the second argument of memcpy is the pointer to the source, you want to supply an address.  a[0] is not an address (unless your array is a collection of pointers).  a[0] dereferences the first element of your array.

If you want to copy from the beginning of your array, then simply "a" will suffice.

I like to use &a[0], just to make it abolutely clear that I intended to start at the 0 index of my array.

So, you either want to use
memcpy( &x, &a[0], sizeof(unsigned int) );

or
memcpy( &x, a, sizeof(unsigned int) );





0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now