Hello...
I try to create some simple posix shared memory programmed that will take some value... then
I facing with some segmentation fault... I only have basic knowledge about struct and pointer... only learn by theory.. never used it before.. please advice
My structure
Code:
typedef struct {
unsigned int shm_hour;
double *shm_adc;
unsigned int *shm_dio;
/* pid_t pid; */
} shmem_t;
My idea is to store the data inside the struct.. shm_hour only have one value but shm_adc and shm_dio will have multiple input value...
Here some of my declaration...
Code:
...
shmem_t *data;
double *shm_adc = NULL;
unsigned int *shm_dio = NULL;
int main(int argc, char *argv[])
{
...
/* attach/map shared memory to our data type */
data = (shmem_t *) shmat(shmid, NULL, 0);
...
data->shm_hour = 13; /* data is the pointer to the struct */
data->shm_adc[0] = 0.01;
data->shm_adc[1] = 1.23;
data->shm_adc[3] = 3.33;
data->shm_adc[2] = 1.52;
data->shm_adc[7] = 2.58;
data->shm_dio[1] = 1;
data->shm_dio[3] = 0;
...
And after debugging.. I know the problem came from shm_adc.. maybe I have wrong declaration and wrong concept.. Really appreciate if I could get some little help from all you guys..
Code:
Program received signal SIGSEGV, Segmentation fault.
0x080484e4 in main (argc=1, argv=0xbffff3b4) at shm_data.c:50
50 data->shm_adc[0] = 0.01;