I'm a student working on a simple C program that implements two shared memory segments. The thing is that when I use strcpy function with the pointer to the second memory segment called nptr2 changes the value of the firt memory segment with the name nptr.
I've been reading another questions that were solved putting nulls at the end of the string because the string that was being copied had not a null terminating caracter. But I expect that's not my case, as I'm using fgets. I'm showing you the output of some printf I've added to my code to see easier what's happening.
Struct used for nptr
struct estadistikak{
int erabiltzaile;
int bbluzera;
int mezukop;
int karakkop;
};
Memory segment declarations and attachments
int memid,memid2;
char mezua[50],*nptr2;
struct shmid_ds buff;
struct estadistikak *nptr;
if ((memid=shmget(0x1234L,sizeof(mezua),0600|IPC_CREAT))<0){
perror("shmget error");
exit(-1);
}
printf("%d\n",memid);
if((nptr2=(char*)shmat(memid,0,0))==(char*)-1){
perror("shmat error");
exit(-1);
}
//Estatistiken memoria sortu eta atzitu
if ((memid2=shmget(0x12345L,sizeof(stats),0600|IPC_CREAT))<0){
perror("shmget error");
exit(-1);
}
printf("%d\n",memid);
if((nptr=(struct estadistikak*)shmat(memid,0,0))==(struct estadistikak*)-1){
perror("shmat error");
exit(-1);
}
The part in which the problem happends
Notice that nptr and nptr2 are two different memory pointers that had no relation between them.
printf("Sartu nahi duzun mezua:\n");
fflush(stdout);
__fpurge(stdin);
fgets(mezua,50,stdin);
printf("Before: %d\n",nptr->erabiltzaile);
strcpy(nptr2,mezua);
printf("After: %d\n",nptr->erabiltzaile);
strcpy(mezua2,nptr2);
printf("Mi mensaje: %s\n",mezua2);
The output with printfs
Sartu nahi duzun mezua:
asdf
Before: 1
After: 1717859169
Mi mensaje: asdf
Thanks for your time and have a nice code!