Home > Archive > Unix Programming > November 2005 > Error when using memcpy on an address returned by mmap()
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Error when using memcpy on an address returned by mmap()
|
|
|
| I am sorta flailing on something that should be pretty easy. I am
getting a bus error when I try to copy into a memory space created by
mmap(). What would cause that? Is there something special that I have
to do to use memcpy with a memory mapped address?
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string>
int main(int argc, char** argv)
{
shm_unlink("/MMAPNAME");
int shmID = shm_open("/MMAPNAME", O_CREAT | O_EXCL | O_RDWR, 0666);
if (shmID == -1)
{
printf("Can not open shared memory \n");
return 1;
}
printf("Shared memory id: %i \n", shmID);
void * memBlock = mmap(0, sizeof(double), PROT_READ | PROT_WRITE,
MAP_SHARED, shmID, 0);
if (memBlock == MAP_FAILED)
{
printf("Can not map shared memory \n");
return 1;
}
else
{
double tempDouble = 102.2;
double tempDouble2;
/* //This commented block below does work
memcpy(&tempDouble2, &tempDouble, sizeof(double));
printf("Temp double 2: %f \n", tempDouble2);
*/
printf("Calling memcpy(%p, %p, %d)\n", memBlock, &tempDouble,
sizeof(double));
memcpy(memBlock, &tempDouble, sizeof(double)); //Bus error
here. Why?
printf("Mem copy ok \n");
}
}
}
| |
| Paul Pluzhnikov 2005-11-26, 9:56 pm |
| "Frank" <fshaw@uci.edu> writes:
> I am sorta flailing on something that should be pretty easy. I am
> getting a bus error when I try to copy into a memory space created by
> mmap(). What would cause that?
mmap() doesn't really "create space" (except when MAP_ANON{YMOUS}).
> Is there something special that I have
> to do to use memcpy with a memory mapped address?
No, but you need to "create space" first. From linux shm_open(3):
On successful completion shm_open returns a new file descriptor ...
The file descriptor is normally used in subsequent
calls to ftruncate(2) (for a newly-created object) and mmap(2).
Your program crashes on Linux in the memcpy() just as it does for you.
It runs fine after fixing it so:
--- junk.c.orig 2005-11-26 18:37:49.157713376 -0800
+++ junk.c 2005-11-26 18:37:39.342205560 -0800
@@ -14,6 +14,10 @@
return 1;
}
printf("Shared memory id: %i \n", shmID);
+ if (-1 == ftruncate(shmID, getpagesize())) {
+ perror("ftruncate");
+ return 1;
+ }
void * memBlock = mmap(0, sizeof(double), PROT_READ | PROT_WRITE,
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| Casper H.S. Dik 2005-11-27, 7:56 am |
| "Frank" <fshaw@uci.edu> writes:
>I am sorta flailing on something that should be pretty easy. I am
>getting a bus error when I try to copy into a memory space created by
>mmap(). What would cause that? Is there something special that I have
>to do to use memcpy with a memory mapped address?
The file is size 0 and the memcpy will attempt to write to a non-existant
address; you will need to use ftruncate to set the size.
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
|
|
|
|
|