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 7196821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:51:03+00:00 2026-05-28T20:51:03+00:00

Have a look at the following code. The goal here is to return a

  • 0

Have a look at the following code. The goal here is to return a reference through two functions (from ReferenceProvider::getReference() to getRef() to main()):

#include <tchar.h>
#include <assert.h>
#include <string>

class BaseClass {
public:
  virtual void write() const {
    printf("In base class\n");
  }
};
typedef BaseClass* BaseClassPointer;

class ChildClass : public BaseClass {
public:
  virtual void write() const {
    printf("In child class\n");
  }
};
typedef ChildClass* ChildClassPointer;

//////////////////////////////////////////////////////////////////////////

ChildClass* g_somePointer = new ChildClass();

class ReferenceProvider {
public:
  const BaseClassPointer& getReference() {
    const BaseClassPointer& val = g_somePointer;
    return val;
  }
};

ReferenceProvider g_provider;

const BaseClassPointer& getRef() {
  std::string test;

  const BaseClassPointer& val = g_provider.getReference();
  return val;
}

int _tmain(int argc, _TCHAR* argv[]) {
  BaseClass* child = getRef();

  assert(child == g_somePointer);
  child->write();

  return 0;
}

Now, when debugging this code (in Visual C++), breaking at return val; in getRef() will give you a screen like this:

Breaking a return

Notice how the values of g_somePointer and val are the same. Now, step over the return statement and you’ll get a screen like this:

Breaking after return

Notice how val has become invalid (0xcccccccc). This is probably because the stack of getRef() has been cleared and val is no longer available.

The problem now is that child in _tmain() will get this invalid value (0xcccccccc) rendering child unusable. So my first (and main) question is: How to do this correctly?

(Please note that this is just an boiled down example from some other code I’ve been working on. It needs to be structured like with, including using references to pointers.)

What’s making this whole thing very strange (and hard to debug) is that the function getRef() works under some conditions:

  1. If you change the type of g_somePointer to BaseClass* (from ChildClass*)
  2. If you remove the local variable in getRef() (i.e. the line std::string test;)

In both cases the reference variable val (in getRef()) will not become invalid and the function will return the correct pointer address. Can anybody explain this to me?

  • 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-28T20:51:04+00:00Added an answer on May 28, 2026 at 8:51 pm

    The problem is here:

    const BaseClassPointer& val = g_somePointer;
    

    Since g_somePointer has a different type (ChildClass* is convertible to BaseClass*, but is not the same type), val cannot refer directly to g_somePointer. Instead, a temporary copy is made, converted to the correct type, and val refers to that.

    The temporary only lasts as long as val, going out of scope at the end of the function, so the function returns an invalid reference.

    If you change the type of g_somePointer to BaseClass* (from ChildClass*)

    In that case, no pointer conversion is required, and so val can refer directly to g_somePointer. The code is then correct, but fragile.

    If you remove the local variable in getRef() (i.e. the line std::string test;)

    With the string variable, there is a destructor call at the end of the function, which overwrites the defunct stack frame that contains the temporary pointer. Without it, nothing overwrites the memory, so the code appears to work – which is unfortunate, as it makes the error much harder to notice.

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

Sidebar

Related Questions

Have a look at the following code from here . It's about preserving circular
Have a look here: In the following code, what would be the type of
am new here. i have a slight problem; PLease look at the following code
Have a look at the following code you are not required to read the
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct
Please have look on the following code: $_SESSION[process_y] = new Process(); $process_y = $_SESSION[process_y];
Have a look at the following code (additionally, you will need jquery.js , jquery.viewport.js
Have a look at the following code: class A(object): defaults = {'a': 1} def
Please have a look a the following code sample, executed on a Windows-32 system

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.