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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:51:02+00:00 2026-05-15T11:51:02+00:00

I saw this bit of code in another thread void foo { int *n

  • 0

I saw this bit of code in another thread

void foo {
  int *n = malloc(sizeof(int)); 
  *n = 10; 
  n++;
  printf("%d", *n);
}

The mistake here is obvious. n isn’t being dereferenced.
There is a memory leak.
Let’s assume there is a garbage collector working here. The reference count to our initial value of n is zero now because n isn’t referencing it anymore. So it’s garbage and returned back. But what about the new location pointed by n? Technically this area of memory hasn’t been allocated yet. So will the reference count be incremented here?

  • 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-15T11:51:03+00:00Added an answer on May 15, 2026 at 11:51 am

    A correctly implemented garbage collector would work as follows:

    int *pi = malloc(sizeof int);
    *pi = 10;
    

    Everything’s fine here.

    pi++;
    

    This statement lets pi point behind the allocated int, which is explicitly allowed by the ISO C99 standard (see 6.5.6p7, 6.5.6p8). Later in the code, there may be pi--, so the allocated int can still be accessed. Or it may be accessed by the expression pi[-1], which is perfectly valid at this point.

    pi = NULL;
    

    At this point, there is no way to ever access the allocated int anymore, so the garbage collector may collect the memory.

    In summary: All pointers that point either to the beginning of an object, to somewhere in the middle of that object or to the location behind that object can be used to access the object. So if such a value exists in memory, the object must not be garbage collected.

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

Sidebar

Related Questions

I saw this bit of code on the interents somewhere. I'm wondering what the
Here is code i saw in a C program , i knew this piece
I recently saw a bit of code that looked like this (with sock being
Sorry, lots of code coming up.. I saw another question like this that used
I'm sure I saw this happening to me once. In a 64-bit OS with
I was Dreamweaving a bit and saw something called Spry. Is this powerful or
I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that
I saw this line in the jQuery.form.js source code: g && $.event.trigger(ajaxComplete, [xhr, s]);
I saw some .NET 2.0 code which looked like this: public enum abc :
I saw this code (explicly it's in jQuery, with modification) (function(window,undefined){ var jQuery=(function(){ var

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.