On 2024-09-22T18:40:44.000+02:00, Страхиња Радић <sr@strahinja.org> wrote:
> Attached is an example C program using fork(2). It dynamically
> allocates a string array with malloc(3), then forks. Child free(3)s the
> array, sleep(3)s for 3 seconds, then exits. Parent wait(2)s for
> children, then prints the array. The array in the parent is not
> affected by free(3) in the child.
I'm sorry I can't express myself better, but with this code below, what is actually happening?
else if (pid == 0)
{
/* Child */
for (i = 0; i < NSTRINGS; i++)
free(strings[i]);
free(strings);
printf("child: Sleeping for 3 seconds...\n");
fflush(stdout);
sleep(3);
printf("child: Done sleeping.\n");
fflush(stdout);
malloc(13);/* <--- what will happen, a leak, it will be freed ? */
exit(0); /* <-- we exit from child here, not continuing */
/* the rest of the function main */
}
No comments:
Post a Comment