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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:06:32+00:00 2026-06-13T02:06:32+00:00

void function(typeA* ptr_to_A) { if (!ptr_to_A) { typeB B; // typeB is a derived

  • 0
void function(typeA* ptr_to_A) {
  if (!ptr_to_A) {
    typeB B; // typeB is a derived class of typeA
    ptr_to_A = &B;
  }
  do_stuff_to_ptr_to_A(ptr_to_A);
  // my hope is that B is still in scope here so that this function will operate on my B object (which is on the stack) which only gets created if ptr_to_A was initially NULL
}

Will this function do what I think it does (what I want it to do)? That is, only allocate B on the stack if the argument was a null pointer?

  • 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-06-13T02:06:33+00:00Added an answer on June 13, 2026 at 2:06 am

    Will this function do what I think it does?

    No, it’s undefined behavior because B goes out of scope. Since this is undefined behavior anything can happen, and therefore you can’t predict the results. You want to keep B in at least the same scope as the function call, so just move it to the top of the method:

    void function(typeA* ptr_to_A) {
       typeB B; // typeB is a derived class of typeA
       if (!ptr_to_A) {
          ptr_to_A = &B;
       }
       do_stuff_to_ptr_to_A(ptr_to_A);
    }
    

    But if you only want to allocate a typeB if ptr_to_A is null then you could do this:

    void function(typeA* ptr_to_A) {
      if (!ptr_to_A) {
        typeB B; // typeB is a derived class of typeA
        do_stuff_to_ptr_to_A(&B);
      } else {
        do_stuff_to_ptr_to_A(ptr_to_A);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function which adds any class that derives from Object to a
I am trying to pass a function pointer using boost::bind. void Class::ThreadFunction(Type(*callbackFunc)(message_type::ptr&)) { }
Assume the following function prototype: void *function(int arg) . Is that a pointer to
Visual C++ is saying my void function needs a return value I compiled this
I keep getting 'warning: control reaches end of non-void function' with this code: -
I'm trying to turn this C++ code/answer (class that creates map string <-> some_array_of_predefined_function_types
I have a function that I need to call int (*trytoprint_f)(int val, void *param);
What is the difference between passing-by-reference and using the C pointer notation? void some_function(some_type&
I am writing a functor F which takes function of type void (*func)(T) and
If a function has a return type other than void , and the function

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.