Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3722152
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:48:04+00:00 2026-05-19T05:48:04+00:00

I have created a parent and a child process using fork( ), and both

  • 0

I have created a parent and a child process using fork( ), and both share a memory address called “ptr”. But i am confused due to the one output of a program:

1) Address of ptr: 123456
NOTE: Same address for both parent and child, so expected is if one process alters this address, it should reflect for the other too process too as the address is same.

2) Parent: *ptr=44

3) Child: *ptr=33

4) Printing values:
Parent still retains old value: printf(“ptr = %d”,*ptr); // Output: still 44, exp is 33
Child prints 33, the expected value. printf(“ptr = %d”,*ptr);//Prints 33 fine

Question1) Can anyone tell me, how the values are different? Although the pointer address is same for both the parent and the child?

Question2) I am working on a memory leak tool which is giving double free, error as it is seeing parent and the child free the same address. However, it is not a case of double free, as we see. How to sort this problem? As the memory address the tool sees for the parent and the child is the same addrerss?

P.S: Please see the below code snippet:

#include <sys/types.h>
#include <unistd.h>
#include <cstdlib>
int main()
{
 int pid, *ptr
 ptr=(int*)malloc(sizeof(int));
 *ptr=33; // Parent keeps the data as 33, before forking.

 if(pid==0){*ptr=44;} // Child modifies data, which is ignored by parent

 // Now we print the memory address and the value both by child and parent
  if(pid==0)
  {
    printf("Child data: %u\n",*ptr);
    printf("Child address: %u\n",ptr);
  }
  if(pid>0)
  {
    printf("Parent data: %u\n",*ptr);
    printf("Parent address: %u\n",ptr);
  }
}

Output:
Child data: 44
Child address: 123456

Parent data: 33 (how come still old value?)
Parent address: 123456 (How come same address but data different than child?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T05:48:06+00:00Added an answer on May 19, 2026 at 5:48 am
    if(pid==0){*ptr=44;} // Child modifies data, which is ignored by parent
    

    Question1) Can anyone tell me, how the values are different? Although the pointer address is same for both the parent and the child?

    This is the whole idea. They may have the same address, but these addresses are virtual. Each process has its own address space. What fork() does is creates a new process, and makes its virtual memory layout look like the parent.

    See the Wikipedia article on page tables and similar topics for some illustrations of how this works.

    — (Long aside follows) —

    What typically happens at a fork() is that page tables for both the parent and child are set up such that the pages are marked as read-only. When a write instruction happens for some location the kernel gets a page fault, which the CPU generates at a bad memory access. The kernel will allocate new memory for the trapped process, map it into the right address by manipulating its page table, copy the old buffer to the newly allocated one and let the write continue. This is called copy-on-write. This makes the initial fork quick and keeps memory consumption down for pages that are not written in either process.

    The previous paragraph is all just an optimization of the fork programming model. They say early Unix didn’t do this — it did a full memory copy of the whole process. I’ve also heard that Cygwin’s fork() does a full copy.

    But the virtual address has nothing to do with the physical address of the memory. The CPU uses it as a “key” for the page table, which defines where the actual memory is. The page table may also say that the page is not valid, in which case the kernel has an opportunity to do a “fixup” (perform copy-on-write, recall the page from swap space, etc.) or kill the process in the case of a legitimately invalid pointer access.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
I have created a custom dialog for Visual Studio Setup Project using the steps
I have created a C# class file by using a XSD-file as an input.
When creating a child process in C++ using Windows API, one can allow inheritance
Have created a c++ implementation of the Hough transform for detecting lines in images.
I have created a template for Visual Studio 2008 and it currently shows up
I have created a PHP-script to update a web server that is live inside
I have created a UserControl that has a ListView in it. The ListView is
I have created a few small flash widgets that stream .mp3 audio from an
i have created a workflow activity that do give the item creater of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.