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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:51:39+00:00 2026-06-14T23:51:39+00:00

I was playing with this code: #include <stdlib.h> #include <stdio.h> void function(int *p){ free(p);

  • 0

I was playing with this code:

#include <stdlib.h>
#include <stdio.h>

void function(int *p){
    free(p);
}

int main(){
    int *i = calloc(3, sizeof(int));
    function(i);
    i[0] = 'a';
    printf("%c\n", i[0]);
    return 0;
}

I expected to get an error, but instead it printed ‘a’, if ‘a’ is deallocated why does it print it? What happens in function(int *p) has any effect if the main()?(if you can, explain what happen to that ‘p’ pointer)

Suppose that I have those two in the same function:

int *a = malloc(...);
int *b = a;

Both of them point to the same piece of memory, but when I have to free it, should I call free on both of them, or just one of them?(again explain why if possible)

  • 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-14T23:51:40+00:00Added an answer on June 14, 2026 at 11:51 pm

    You have returned the memory to the heap, but the memory isn’t reused yet. So, sometimes the memory contents remain the same and you can read and write it as if it where still in use.

    But you should never use memory that is already freed, since there’s no guarantee how long the memory stays this way.

    In your second part a = malloc(...); b = a, both variables point to the very same memory. Since this is just one piece of bytes, you must free() it exactly once. Otherwise you have a double free, which might corrupt the heap.

    Update for @SSHThis’s question:

    free(a) only returns the memory to the heap. It doesn’t modify a or b. The check if(a) or if(b) evaluates to true, whether the memory was freed or not. Only when you set a or b to null, will the check evaluate to false, whether the memory was freed or not.

    So free(a) and if(a) are independent operations. They don’t influence each other.

    a = malloc(10); /* assuming malloc doesn't return NULL */
    if (a) {
        /* will be true, memory is NOT freed */
    }
    
    free(a);
    if (a) {
        /* will be true, although memory is freed */
    }
    
    a = malloc(10); /* assuming malloc doesn't return NULL */
    a = NULL;
    if (a) {
        /* will NOT be true, although memory is NOT freed */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was playing around with some Javascript snippets today and noticed that this code
i'm playing with MEF and in the example i see this code ( i
this code gives an instant seg fault,I tried placing printf's all over, I even
I've been playing with this and I can't understand why the RegDeleteKey function is
Why this piece of code here: #include <ncurses.h> #define WIN 5 #define WI win[0]
This surprised me a little bit, but I was playing around with some code
I've been playing around with c++11 move semantics In the code... #include <vector> #include
Im planing on using this code to make random sounds play with an action,
When placing a conditional breakpoint in my code, this fails: Sophie Dee <> myString
I have been playing with this one for a few days now, and keep

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.