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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:14:52+00:00 2026-05-13T17:14:52+00:00

tl;dr – Could you please expand on the 4 comments in the first code

  • 0

tl;dr – Could you please expand on the 4 comments in the first code snippet below? Specifically what is meant be deref

I’m a long time Java developer looking to learn C++. I came across this website aimed at developers in my situation.

   int x, *p, *q;
   p = new int;
   cin >> x;
   if (x > 0) q = &x;
   *q = 3;                 // 1. deref of possibly uninitialized ptr q
   q = p;
   p = new int;            // 2. potential storage leak (if x != 0 this
                           //     memory will not be returned to free storage)
   *p = 5;
   delete q;
   *q = 1;                 // 3. deref of deleted ptr q
   q = p;
   if (x == 0) delete q;
   (*p)++;                 // 4. deref of possibly dangling ptr p (if x is zero)

Although I thought I understood how pointers work, I am finding the comments difficult to understand.

My take;

  1. We are either assigning x (& *q of course) to be 3 OR if q != &x then q has a just value as it was uninitialized and we have just assigned a random piece of memory to the value 3. I’m not sure how you can dereference something that isn’t initialized?
  2. This is fine
  3. What’s wrong with dereferencing a deleted pointer? After ‘delete q’ is *q meaningless?
  4. What’s wrong with dangling pointers? Is the memory viable for reallocation now that we have deleted it even though we still have a pointer to it?

I think my basic misunderstanding is about just declaring an int pointer, does this allocate memory too? Is it on the stack or the heap?

Also does dereference just mean ‘read the value at the address of the pointer’? I think my confusion is that I interpret it as loosing a reference to some data as in;

int *x;
x = new int;
*x = 5;
x = new int; // Dereferencing the first bit of memory allocated.

Thanks for you patience I hope that this makes some sense as a question,

Gav

  • 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-13T17:14:52+00:00Added an answer on May 13, 2026 at 5:14 pm

    Simply put: A pointer is an address of a variable whose values you may be interested in.

    Dereferencing is the act of accessing that value — by prepending the * (dereferencing operator) to the pointer variable. The access may be for reading, writing or both.

    1. If you do not initialize a pointer to a valid address (or the special value NULL) — you do not know what that variable contains. Dereferencing will try to take whatever is there and treat it as an address. This is undefined behavior — all bets are off, anything might happen, though if you are lucky you’ll end up with a trap/hardware exception.

    2. Exactly. Because, p was holding the address of some memory you allocated. Not freeing up system resources causes a leak. Dereferencing a deleted pointer is the same as dereferencing an uninitialized pointer — you don’t know what value it may contain.

    3. Again, UB.

    4. True. You have UB for x == 0. Dangling pointers are dangerous because they creep up at the most inopportune time, format your hard disk and are never seen again. It becomes impossible to reasonably justify how your program will behave.

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

Sidebar

Ask A Question

Stats

  • Questions 386k
  • Answers 386k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why is it that you need to place columns you… May 14, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer There is an static embedded = [] It is described… May 14, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer Strange code ! What does the with (thisform) do ?… May 14, 2026 at 11:44 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.