1.

Solve : C++ fork() function?

Answer»

I'd like to be able to do a fork() as an exercise for future reference if I need it. Examples on the internet have it similar to this:

Code: [Select]#include <iostream>
#include <sys/types.h>
#include <unistd.h>

using namespace std;
pid_t pid;

int main()
{
    switch(pid = fork())
    {
    case -1:
        cerr << "Fork failed\n";
    case 0:
        cout << "In child process.\n";
    default:
        cout << "In parent process.\n";
    }
    RETURN 0;           
}
I'm getting a compile error:

`fork' undeclared (first use this function)

I have read that it's supposed to be defined in unistd.h, but it doesn't seem to be. What am I (are they?) doing wrong?


Also, I have a question: If I make a pointer to a class, but initialize it to 0, have I allocated memory for just the variable, or the whole class? If I have a class BeachBall and have this code:

BeachBall *theBall;
theBall = 0;

Did I make a BeachBall, or just a pointer of type BeachBall? It's the difference between 4 bytes and roughly 32, so it means something to me.
I can't help you there as I don't know about the fork function. CHECK the header file to see if it's in?


I am 95% certain that no class is created (and THEREFORE no memory taken up) until you get to the new KEYWORD. So you've used memory for a pointer, but not a class yet.



Discussion

No Comment Found