reference to pointer

reference to pointer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int func(char *&ptr) {
    if (something()) {
        ptr = new char[10];
        return 10;
    }
    else {
        ptr = new char[20];
        return 20
    }
}

void main() {

char *ptr;
auto size = func(ptr);
...

Definition of ptr is in main
'new' is in func
Is reference to pointer a good way to do this?
I think this will work, so long as you remember to delete[] ptr out in main(). I don't know what this is for, but couldn't you use a dynamic container like vector or string instead of dynamically allocating it manually?
Topic archived. No new replies allowed.