Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7678863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:44:39+00:00 2026-05-31T17:44:39+00:00

struct node { Item item; node *l, *r; node(Item x) {item = x; l

  • 0
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; // return a local pointer
    x->l = max(a, l, m);
    x-r = max(a, m+1, r);
    Item u = x->l->item, v = x->r->item;
    if (u>v) x->item = u;
    else x->item=v;

    return x;    // return a local pointer
}

This is a piece of code from “Algorithm in c++” by Robert Sedgewick, page 252, Program 5.19. And in function max(), the returned variable is a pointer which is created inside the function.

In my opinion, returning a local pointer is not allowed in c/c++. So my question is that “is that OK to write a function like this”? I cannot believe such a classic book make mistake like this. Or I misunderstood the principle?
Please help. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:44:40+00:00Added an answer on May 31, 2026 at 5:44 pm

    x isn’t a “local pointer” – that is, a pointer to a local variable. *x was allocated using new. x points to dynamically allocated memory and returning that pointer is just fine. So yes, it’s OK to write a function like this one, and there’s not an error in the book.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is a b-day reminder code utilizing linkedlists typedef struct node { char name[61];
int Size(struct node* node) { if(node == NULL) { return 0; } else if(node
If I create a structure in C++ like this: typedef struct node { int
struct Node { int value Node* next; } typedef List Node* const Set operator
I have the following code struct Node { int accnumber; float balance; Node *next;
#include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int data; struct node*link; }; void push(struct node*,int);
#include <iostream> using namespace std; struct Node { int item; // storage for the
#include <iostream> using namespace std; struct Node { int item; // storage for the
The struct looks like this: template <class Node_entry> Node<Node_entry>::Node(Node_entry item, Node *add_on) { entry
struct node { node(int _value, node* _next) : value(_value), next(_next) {} int value; node*

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.