all I want to do is just write "hey" to my shared memory, but it gets thrown at that line. very simple code as follows:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#define SHM_SIZE 1024
#define FLAGS IPC_CREAT | 0644
int main(){
key_t key;
int shmid;
if ((key = ftok("ex31.c", 'k')) == -1){
exit(1);}
if ((shmid = shmget(key, SHM_SIZE, FLAGS)) == -1) {
exit(1);}
char* shmaddr;
if( shmaddr=shmat(shmid,0,0) == (char*)-1){ //WRONG ARGUMENTS ??
exit(0); }
printf("opened shared memory\n"); //gets here
strcpy(shmaddr, "hey"); //GETS THROWN HERE
printf("after writing to memory\n"); //never get here
the error the debugger gives me is:
Program received signal SIGSEGV, Segmentation fault. 0x0000000000401966 in main (argc=1, argv=0x7fffffffe068) at ../ex31.c:449 449 strcpy(shmaddr, "hey"); //GETS THROWN HERE