Solved

malloc() and fork()

Posted on 1998-07-16
2
543 Views
Last Modified: 2013-12-26
When I call fork(), are all my dynamic memory pointers copied, and pointing to new memory addresses? Is this chunk of code correct? Please explain. Thank You.

int fork_func(void)
{
   char *p;

   p = (char *)malloc(100);
   if (!fork()) {
      /** some processing **/
      free((void *)p);
   }
   free((void *)p);
   return 0;
}
0
Comment
Question by:Ignatz
2 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
ID: 1294939
You are correct, fork() duplicates the current variables and stack.  The problem with the above code is that free() will get called twice for the child.  You should do this instead:

int fork_func(void)
{
   char *p;

   p = (char *)malloc(100);
   switch(fork()){
   case 0: // child
      /** some processing **/
      free((void *)p);
      return 0;

   case -1: // error
      // some error processing
     free((void *)p);
     return 1;

   default:  // normal parent process
   free((void *)p);
  }
   return 0;
}
0
 

Author Comment

by:Ignatz
ID: 1294940
Thanks, I am currently calling exit(0) after free() in child process, but I forgot to put that in my sample code.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 113
ASCII Non-Printable characters/codes and their HTML equivalents 6 105
conditional code and condition difference 9 77
FizzBuzz challenge 9 76
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now