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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:40:03+00:00 2026-05-23T12:40:03+00:00

int main( ) { int *p; p=(int *)malloc(20); printf(%u,p); \\ printing some memory adress

  • 0
int main( )    
{
    int *p;
    p=(int *)malloc(20);
    printf("%u",p); \\ printing some memory adress 
    p=(int *)realloc(p,0);
    printf("%u",p); \\ printing as 0
    printf("%u",*p); \\ printing the value as 0
}

Now my question is the statement relloc working as the free() function as the pointer p is pointing to the NULL and NULL value in it. Will that 20 bytes be freed?

  • 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-23T12:40:04+00:00Added an answer on May 23, 2026 at 12:40 pm

    No, it may not be freed in all circumstances. C99 has this to say in 7.20.3.4 The realloc function:

    If memory for the new object cannot be allocated, the old object is not deallocated and its value is unchanged.

    Now you may think that an allocation of zero bytes couldn’t possibly fail but the standard doesn’t mandate that. It says, for zero-sized objects:

    If the size of the space requested is zero, the behaviour is implementation defined: either a null pointer is returned, or the behaviour is as if the size were some non-zero value, except that the returned pointer shall not be used to access an object.

    So, if your implementation is one of those that doesn’t return NULL for a zero-size allocation, and it requires some housekeeping information for each allocation, and the memory arena is so full that you can’t store even another housekeeping header, and it’s not smart enough just to split the current allocation and return most of it to the arena, it may fail a zero-size realloc and hence not free the original memory.

    Now this is a (very) edge case depending on a lot of decisions made in the implementation, most of which may seem bizarre to people with experience of writing them. Unfortunately, we have to allow for the edge cases as well 🙂

    If it really worries you, I’d just change:

    p=(int *)realloc(p,0);
    

    into:

    free (p) ; p = NULL;
    

    A couple of other points.

    1. Comment marjers are forward slashes /, not backslashes.
    2. You are not allowed to dereference p in that final line whether or not your implementation returns NULL for a zero-size allocation. See my second quote above where it states: “except that the returned pointer shall not be used to access an object”.
    3. You shouldn’t really cast the return values from malloc-type calls. This is a hangover from the dim dark past before C had a void*, and malloc returned char*. The problem with doing so nowadays is, if you forget to include stdlib.h, problems can be hidden from you.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

int main(void) { int* p = (int*) malloc(sizeof(int)); int* q = (int*) malloc(sizeof(int)); *p
I want to know how malloc and free work. int main() { unsigned char
int main(void) { char tmp, arr[100]; int i, k; printf(Enter a string: ); scanf_s(%s,
int main() { int *d=0; printf(%d\n,*d); return 0; } this works fine. >cc legal.c
Why doesn't this print 5 ? void writeValue(int* value) { value = malloc(sizeof(int)); *value
#inlcude <stdio.h> #inlcude <stdlib.h> #inlcude <string.h> int main() { char *buff = (char*)malloc(sizeof(char) *
int main() { int i=0; int* p_numbers = ((int*) malloc (5*sizeof(int))); int* temp; temp
I came across the following code: int main() { char *A=(char *)malloc(20); char *B=(char
#include<stdio.h> #include<stdlib.h> int main() { int count=0; char c=0; printf(Reading this Source file\nFile name
Consider: #include <stdio.h> #include <stdlib.h> int main() { int *numeros = malloc(sizeof(int) * 3

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.