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

  • Home
  • SEARCH
  • 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 1030651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:44:08+00:00 2026-05-16T12:44:08+00:00

Quoting The C++ programming language (special edition, section 4.9.6, Objects and Lvalues), and as

  • 0

Quoting “The C++ programming language” (special edition, section 4.9.6, “Objects and Lvalues”), and as is well known:

[…] an object declared in a function is created when its definition is encountered and destroyed when its name goes out of scope

OK ! And in section 4.9.4:

A name can be used only in specific part of the program text. For a name declared in a function, that scope extends from its point of declaration to the end of the block in which its declaration occurs.

This all sounds fine !

But my question is: how can an (auto) variable be destroyed when the control reach the end of its block? And a subquestion: Is it actually the case ?

For example:

int main()
{
  int* c = 0;
  {
    int b = 999;
    c = &b;
  } // End of the scope of b...
  std::cout << b; // ... so this is illegal
  // But ...
  std::cout << *c; // ... is OK, so 'b' has not really been destroyed
}

I understand that a local variable is destroyed when quitting the scope of its function because of the stack related things involved in a function call. But when quitting a simple { // ... } block, nothing happens.

Is it then a language specific that leads to undefined behavior (in my case the last cout is actually undefined) but that is in practice without effect at execution (nothing is actually executed to destroy the object) ?

Thanks !

Edit 1: I am not considering static variables.

Edit 2: In case where the variable is an object with a destructor was clear to me, I was asking about non object variables.

  • 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-16T12:44:08+00:00Added an answer on May 16, 2026 at 12:44 pm

    The code in your sample is indeed undefined behavior, and it will appear to work in simple examples like this. But the compiler may choose to reuse the slot used to store variable b. Or it may be destroyed as the result of data being pushed on the stack because of a function call.

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

Sidebar

Related Questions

I'm currently developing a general-purpose agent-based programming language (its syntaxt will be somewhat inspired
Quoting from Kernighan and Ritchie's 'The C Programming Language' Page 16 - #include<stdio.h> main()
Base R defines an identity function, a trivial identity function returning its argument (quoting
Quoting from the C-std section 6.7.2.1, struct s { int n; double d[]; };
quoting from The C++ Standard Library by N M Jousttis, Section 5.9 #include <
In here, interpretation of Hello $world per each quoting symbol I mean language .
The $@ variable seems to maintain quoting around its arguments so that, for example:
Quoting the answer of Andrew Hare on the This Question . Object data sources
I'm pretty new to javascript and am wondering about quoting the keys in object
The C++ standard (quoting from draft n3242) says the following about subobjects [intro.object]: Unless

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.