Solved

memcpy + mmap

Posted on 2010-09-12
2
1,724 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
Comment
Question by:zizi21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
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

by:HalfAsleep
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

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

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

Join & Ask a Question