Allocate

I'm trying to return a pointer to the new process that was just created, but i get an error because it's a void function. So what is the correct syntax? -Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    allocate_pcb();
    cout << pcb;
    return 0;
}

void allocate_pcb()
{
    pcb = new process;
    assert(pcb != NULL);
}
You need to change it to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
process *allocate_pcb(); // This is the prototype. It's required so that you can call this function

int main()
{
    process *pcb = allocate_pcb();
    cout << pcb; // The ouput is the value of the pointer
    return 0;
}

process *allocate_pcb() // Needs to match the prototype
{
    process *pcb = new process;
    assert(pcb != NULL);
    return pcb;
}
C++ is strongly typed. It's required to provide always the type.
Topic archived. No new replies allowed.