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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:16:26+00:00 2026-06-10T07:16:26+00:00

Given this little piece of code : #include <iostream> #include <assert.h> using namespace std;

  • 0

Given this little piece of code :

#include <iostream>
#include <assert.h>
using namespace std;


struct Foo
{
    // something
};


int main()
{

    Foo *p1 = new Foo;
    Foo * p2 = p1;
    assert(NULL != p1);
    delete p1;
    p1 = NULL;

    assert(NULL != p2);
    delete p2;

    cout << "everything is cool!" << endl;

    return 0;
}

When I delete p1 , the second assert (assert(NULL != p2);) is not failing , why ?

The output : everything is cool!

Then why the assert of p2 is not failing ?

  • 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-10T07:16:27+00:00Added an answer on June 10, 2026 at 7:16 am
    1. Watch the stars, in particular.

      int i;
      
      int *p1 = &i;
      assert(p1 != NULL);
      
      int *p2 = p1;
      assert(p2 != NULL);
      
      *p1 = 10;
      assert(i == 10);
      assert(*p2 == 10);
      
      p1 = NULL; // does not affect the object p1 was pointing at
      
      assert(i == 10);
      assert(*p2 == 10);
      assert(p2 != NULL); // (which we already know, if the previous assert didn't crash)
      
    2. You’re right to suspect that everything is not cool. The program invokes the delete operator twice on the same object (a “double free” error), which will tend to corrupt the heap. If the program continued, you would see undefined behavior at some point. Having undefined behavior rather defeats the point of writing a computer program. If you want to see errors like this immediately and unambiguously, run it under valgrind’s memcheck or equivalent.

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

Sidebar

Related Questions

Given this little piece of code //============================================================================ // Name : prwe.cpp // Author :
Given this little piece. http://jsfiddle.net/4gb6K/7/ I am attempting to align the bot element at
I was given this little bit of code by my teacher for this weeks
In this piece of code, if I match the given pattern on the line,
Given this snippet of code public abstract class Foo { private static SqlConnection _sqlConnection;
This little piece of code is supposed to fire off and give me the
I recently found this little piece of code for my .vimrc if has(autocmd) Highlight
Given this little fiddle: http://jsfiddle.net/5Sw3h/8/ I have a navigation panel to the left, a
I'm wondering why does Visual Studio code analysis give a warning with this little
Given this code: List<Integer> ints = new ArrayList<Integer>(); // Type mismatch: // cannot convert

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.