Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用共享内存C语言在struct中启动动态数组_C_Linux_Shared - Fatal编程技术网

如何使用共享内存C语言在struct中启动动态数组

如何使用共享内存C语言在struct中启动动态数组,c,linux,shared,C,Linux,Shared,我想问你们,如何使用共享内存在struct中为键盘大小(argv)初始化数组,我不能使用malloc,因为它是private之类的 例如一段代码 我要初始化的带数组的结构 记住我们将使用共享内存IPCV,如何调整此阵列的大小 感谢您的帮助:) 编辑: 如果我在struct中还有一个变量,我可以这样做吗 struct data { int counter; char *text; }*shared data; int shmid int main(int argc, char*

我想问你们,如何使用共享内存在struct中为键盘大小(argv)初始化数组,我不能使用malloc,因为它是private之类的

例如一段代码

我要初始化的带数组的结构

记住我们将使用共享内存IPCV,如何调整此阵列的大小

感谢您的帮助:)

编辑:

如果我在struct中还有一个变量,我可以这样做吗

struct data
{
   int counter; 
   char *text;
}*shared data;

int shmid

int main(int argc, char* argv[])
{
  int m = atoi(argv[1]) /* number of slots*/ 
  int n = atoi(argv[2]) /*size of txt */

  shmid = shmget(12345, m * n * sizeof(struct my_data), IPC_CREAT|)600|IPc_EXCL)); 

shared_data = (struct data*)shmat(shmid, NULL, 0);

/*So now Can I write to txt??? */


}


txt
char[]
更改为
char*
,然后使用
shm_open()
mmap()
在知道所需大小后分配共享内存

typedef结构数据
{
char*txt;
}数据;
数据d;
int shm_fd=shm_open(“name”,O_RDWR | O_CREAT);
d、 txt=mmap(空,大小,保护读取,保护写入,映射共享,shm\u fd,0);
...
munmap(d.txt,大小);
shm_取消链接(“名称”);

是否可以在IPCV中使用它?我问,因为shm_open和mmap是POSIX@Roundstic我不知道IPCV是什么,System V IPC,System V有另一个用于共享内存的API,所以只需将我的示例中的
shm_open()
/
mmap()
/
munmap()
/
shm_unlink()
替换为//。
struct data
{
   int counter; 
   char *text;
}*shared data;

int shmid

int main(int argc, char* argv[])
{
  int m = atoi(argv[1]) /* number of slots*/ 
  int n = atoi(argv[2]) /*size of txt */

  shmid = shmget(12345, m * n * sizeof(struct my_data), IPC_CREAT|)600|IPc_EXCL)); 

shared_data = (struct data*)shmat(shmid, NULL, 0);

/*So now Can I write to txt??? */


}