Solved

memcpy + mmap

Posted on 2010-09-12
2
1,671 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
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Beginner Probleme with my IDE Code::Blocks C++ 5 233
Concatenate two strings Last and First Name 10 59
Want to delete all my personal data 13 138
valid enum? 6 73
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

867 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now