简体   繁体   English

修改没有指针的双指针

[英]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? 所以我想知道如何在不使用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). 原因是** ARRAY是进程之间的共享内存,并且我想使用信号量来控制访问(如果我正确理解memcpy,它将允许进程访问当前由a锁定的内存区域。信号)。

C trusts you to only do safe things. C相信您只会做安全的事。 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. 您为此使用什么功能都没有关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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