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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:46:32+00:00 2026-06-17T09:46:32+00:00

I was just going through an article which explains about Wild pointers. For garbage

  • 0

I was just going through an article which explains about Wild pointers. For garbage memory, it states that when a pointer pointing to a memory object is lost, i.e. it indicates that the memory item continues to exits, but the pointer to it is lost; it happens when memory is not released explicitly. I was trying to understand this with an example. Here is what I wrote

#include <iostream>

using namespace std;
int q =12;
int point()
{
   int *p;
   p = &q;
   //delete p;
}
int main()
{
   point();
   return 0;
}

So, in the above example, memory item (q) continues to exist, but the pointer to it is lost. I may have misunderstood it all wrong, but if I understood it correctly, then does this example addresses ‘garbage memory’ definition given above? If yes, then I should use delete p, right?

  • 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-17T09:46:33+00:00Added an answer on June 17, 2026 at 9:46 am

    C++ Does not have garbage collection the way you understand it.. But what you are showing is not a “memory leak” which is what I think you mean.

    In this case you are pointing to a memory location that is NOT dynamically allocated, and is accessible from outside the function for the duration of the program.


    int point()
    {
       int *p = new int();
       //delete p;
    }
    

    This is now a memory leak, since the memory is still allocated, but noone can access it any more. If this were Java or any other language which supports GC, this memory would now be queued to be Garbage collected.


    The recommended style in C++ is to make all allocated memory auto deallocate themselves, as far as possible, with a concept called Resource Acquisition Is Initialization (RAII), using smart pointers as below.

    int point()
    {
       std::unique_ptr<int> p(new int());
       //delete p;  // not needed.
    }
    

    When the variable p is destroyed, at the end of its scope, it will automatically call delete on the allocated memory so you dont have to. By making stuff clean up after themselves, we enable the below.

    C++ is my favorite garbage collected language because it generates so
    little garbage

    Bjarne Stroustrup’s FAQ: Did you really say that?.
    Retrieved on 2007-11-15.
    http://en.wikiquote.org/wiki/Bjarne_Stroustrup

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

Sidebar

Related Questions

I was just going through glibc manual for description about posix_memalign function when I
I was just going through all the Mathematical stuff which is used in programming.
I am about to submit my iPhone app for review. Was just going through
Hey I was just going through some article on XML, I came across the
I was just going through certain code which are frequently asked in interviews. I
I was just going through the HAProxy. Articles suggests that having the maxconn set
Just going through the sample Scala code on Scala website, but encountered an annoying
I was just going through the iterative version of fibonacci series algorithm. I found
I know there isn't enough validation in here just going through some testing. $result
I'm just learning the Opa language and going through their Manual. In the third

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.