Solved

memcpy + mmap

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

792 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