Solved

memcpy + mmap

Posted on 2010-09-12
1,655 Views
Last Modified: 2012-05-10
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
Question by:zizi21
2 Comments

LVL 5

Accepted Solution

shajithchandran earned 250 total points
ID: 33659759
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

LVL 7

Assisted Solution

HalfAsleep earned 250 total points
ID: 33659936
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

Join & Write a Comment Already a member? Login.

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!