在C fork()的调用之后引用指针 [英] Referencing pointers after a fork() call in C

查看:413
本文介绍了在C fork()的调用之后引用指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个加载了一个char **变量与一些字符串数据的功能。我的目标是到餐桌的过程中,并打印一些孩子的数据,有的来自父母。不过,我无法通过fork()调用后引用指针。

So, I've got a function that loads up a char** variable with some string data. My goal is to fork the process, and print some of that data in the child, and some from the parent. However, I'm unable to reference the pointer after the fork() call.

我觉得fork()的制作父进程的整个地址空间,这似乎,这将包括各种堆栈指针的副本...

I thought that fork() made a copy of the entire address space of the parent process, which seems that it would include the various stack pointers...

从本质上讲,我的code目前看起来是这样的:

Essentially, my code currently looks like this:

load_data(char **data);

char** data;
load_data(data);
printf("String 0: %s\n", data[0]);
fork(); 
printf("String 0 again: %s\n", data[0]);    /* Segfaults Here! */

任何人有任何想法,我做错了什么?我做了一点谷歌这个搜索,似乎我在做什么应该工作 - 但事实并非如此。因此,我误解的东西根本...

Anyone have any ideas what I'm doing wrong? I've done a bit of google searching on this, and it seems what I'm doing should work - but it doesn't. Thus, I'm misunderstanding something fundamental...

推荐答案

您在做错误的指针操作和刚开在第一次通话幸运的 - 这里的code应该是什么样子:

You're doing bad pointer operations and just getting lucky on the first call - here's what the code should look like:

load_data(char **data);

char* data = NULL;
load_data(&data);
printf("String 0: %s\n", data);
fork(); 
printf("String 0 again: %s\n", data);    /* Doesn't Segfault Here! */

这篇关于在C fork()的调用之后引用指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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