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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:15:41+00:00 2026-05-25T12:15:41+00:00

I wanted to know if the following code is well formed if ( nChildLines

  • 0

I wanted to know if the following code is well formed

 if (  nChildLines == 0 ) 
 {
    nChildLines = 1;
    Tag tempTag = attachmentlines.tag();
    cfgChildLines = &tempTag;
 }

Here, tampTag is some object. And cfgChildLines is a scoped pointer to that object declared outside of the ‘if’ block.

Now my question is, when the ‘if’ block completes, does tempTag get destructed? And if so, is the usage of the ‘cfgChildLines’, which points to tempTag, valid past the end of the if block?

  • 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-25T12:15:42+00:00Added an answer on May 25, 2026 at 12:15 pm

    Important to remember that taking an address in C++ does NOT guarantee any protections that that address continues to point to something valid. There’s no reference counting built into the language (thats why we had to invent scoped_ptr/shared_ptr/etc).

    Keeping that in mind, we can look in more detail at your situation:

    tempTag is an automatic variable and gets destroyed at the end of the scope it was created it. The address you took therefore will point somewhere on the stack to a destroyed object outside this scope. Since you assigned to a scoped_ptr, and scoped_ptr assumes it can destroy the object through delete, from the documentation

    The scoped_ptr class template stores a pointer to a dynamically allocated object

    (emphasis mine)

    So you’re violating scoped_ptr’s interface and you’re going to have some undefined behavior once the scoped_ptr is deleted.

     {
        nChildLines = 1;
        Tag tempTag = attachmentlines.tag();
        cfgChildLines = &tempTag;
     } // tempTag destroyed here
    
    
    // LATER
    } // scoped_ptr calls delete, undefined behavior possibly crash, 
      // possibly an occasional crash
    

    If you really need tempTag in a larger scope, then just declare it in the larger scope you need it in and don’t use scoped_ptr.

    void Foo()
    {
       Tag tempTag 
    
        {
            nChildLines = 1;
            tempTag = attachmentlines.tag();
        }
    }
    

    Another way to think about it: When you create a dynamically allocated object, you take ownership of its lifetime. You manually create and destroy the thing. Therefore you can pass this ownership to another object, such as a scoped_ptr which can manage things for you. In contrast, variables created on the stack will be automatically allocated and deallocated — the rights to create and destroy are held entirely by the call stack and you can’t give those rights to yourself or someone else (ie scoped_ptr). You can only strategically place those variables in a place that correctly scopes the variable so that the automatic, stack-based lifetime corresponds with how you intend to use the thing.

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

Sidebar

Related Questions

I wanted to know why the following code crashes. int main( ) { int
Hi I wanted know the reasons of the following code void main() { class
I have the following code which works fine, however I wanted to know if
i wanted to know the logic of following radix sort program. #include <stdio.h> #include
I wanted to know if anybody can explain me the following piece of Javascript
The following is a C++ program using STL vector container. Just wanted to know
Wanted to know if someone had a suggestion on code or maybe there's a
I wanted to know how linker resolves the printf symbol in the following assembly
I saw the following code, and wanted to use it for a simple activity
I wanted a TextBox with just digits as entry. So i wrote following code

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.