struct node { Item item; node *l, *r; node(Item x) {item = x; l = 0; r = 0;} }; typedef node* link; link max(Item a[], int l, int r) { int m = (l+r)/2; link x = new node(a[m]); if (l==r) return x;
This is a code snippet from Robert Sedgewick's Algorithm in C ++, p. 252, Program 5.19. And in the max() function, the returned variable is a pointer that is created inside the function.
In my opinion, returning to a local pointer is not allowed in c / C ++. So my question is, "is it right to write such a function"? I canβt believe that such a classic book is mistaken. Or did I misunderstand the principle? Please help. Thanks.
Ke li
source share