C使用共享内存传递空指针 [英] C pass void pointer using shared memory

查看:362
本文介绍了C使用共享内存传递空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要作废处理程序传递到另一个应用程序,要复制我一直在使用共享内存创建了一个小程序的方案,并尝试将无效指针传递到另一个应用程序并打印的价值,我可以得到另一个空指针地址应用程序,但是当我尝试取消引用指针第二个应用程序崩溃。

I need to pass void handler to another application, To replicate the scenario I have created one small program using shared memory and try to pass the void pointer to another application and print the value, I can get the void pointer address in another application, but when I try to dereference the pointer second application crash.

下面是示例应用程序wire.c。

Here are the sample application wire.c .

#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

int main() {

key_t key=1235;
int shm_id;
void *shm;
void *vPtr;

shm_id = shmget(key,10,IPC_CREAT | 0666);
shm = shmat(shm_id,NULL,NULL);
sprintf(shm,"%d",&vPtr);
printf("Address is %p, Value is %d \n", (void *)&vPtr, * (int *)vPtr);
return;
}

下面是read.c

#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>

int main() {

key_t key=1235;
int shm_id;
void *shm;

void *p = (void *)malloc(sizeof(void));

shm_id = shmget(key,10,NULL);
shm = shmat(shm_id,NULL,NULL);
if(shm == NULL)
{
printf("error");
}
sscanf(shm,"%d",&p);
printf("Address is %p %d\n",(void *)p);
return 0;
}

当我尝试取消引用p将其崩溃。我需要通过在第二个应用程序void指针的地址和值。

When I try to dereference p it crash. I need to pass the void pointer address and value in second application.

我不希望共享应用程序之间的价值,它的工作原理使用共享内存,我知道了。

I don't want to share value between application, It works using shared memory,I know.

在默认情况下无效* PTR会有一些garbadge值(前。添加= 0xbfff7f,值= 23456),你能告诉我,我怎么能使用该地址传送空指针的地址到另一个应用程序,并从第二个应用程序我可以打印这是在第一个应用程序中(即23456)的值。

By default void *ptr will have some garbadge value (for ex. add= 0xbfff7f, value=23456), Can you please tell me, how can i pass void pointer address to another application and from the second application using that address i can print the value which was found in first application (i.e. 23456).

除了共享内存有任何其他替代可用?

Apart from the shared memory is there any other alternate available?

感谢。

推荐答案

这可能是因为指针依然是虚拟的;它的指向是共享存储器但不能保证两个进程共享同一存储器映射到相同的虚拟地址。在物理的地址当然是相同的(这是相同的实际内存,毕竟),但从未过程与物理地址处理。

This is probably because the pointer is still virtual; it's pointing at memory that is shared but there is no guarantee that two processes sharing the same memory maps it to the same virtual address. The physical addresses are of course the same (it's the same actual memory, after all) but processes never deal with physical addresses.

您可以尝试使用的mmap()(在Linux中)做共享请求特定的地址。您还可以通过简单的打印共享内存块的地址,在这两个过程中验证的虚拟地址的理论。

You can try to request a specific address by using mmap() (in Linux) to do the sharing. You can also verify the virtual address theory by simply printing the addresses of the shared memory block in both processes.

此外,不要投在的malloc的返回值() ç并没有分配,当你将要使用共享内存的内存。这部分只是没有任何意义。

Also, don't cast the return value of malloc() in C and don't allocate memory when you're going to be using the shared memory. That part just makes no sense.

这篇关于C使用共享内存传递空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆