简体   繁体   中英

modifying double pointer without memset

char **ARRAY;
char people[4][20];
shmid = shmget (shmkey, sizeof (people), 0644 | IPC_CREAT);
ARRAY = (char**) shmat (shmid, NULL, 0);    

memcpy(ARRAY, "BOBBY1", sizeof("BOBBY1")); 
memcpy(ARRAY + 20, "BOBBY2", sizeof("BOBBY2"));
memcpy(ARRAY + 2*20, "BOBBY3", sizeof("BOBBY3"));
memcpy(ARRAY + 3*20, "BOBBY4", sizeof("BOBBY4"));

SO I'm wondering how I would go about modifying the existing strings in this array without the use of memcpy?

The reason for this is that **ARRAY is shared memory between process, and I want to use a semaphore to control access (and if I understand memcpy correctly, it will allow a process to access the region of memory that is currently locked by a semaphore).

C trusts you to only do safe things. There is no way to make the compiler enforce something like "this block can only be accessed if you hold that semaphore".

You document all requirements (in the source), and then you follow the contract manually.

What functions you use for that does not matter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM