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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:04:11+00:00 2026-06-10T07:04:11+00:00

If do the next: int* array = malloc(10 * sizeof(int)); and them I use

  • 0

If do the next:

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

and them I use realloc:

array = realloc(array, 5 * sizeof(int));

On the second line (and only it), can it return NULL?

  • 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-10T07:04:13+00:00Added an answer on June 10, 2026 at 7:04 am

    Yes, it can. There are no implementation guarantees on realloc(), and it can return a different pointer even when shrinking.

    For example, if a particular implementation uses different pools for different object sizes, realloc() may actually allocate a new block in the pool for smaller objects and free the block in the pool for larger objects. Thus, if the pool for smaller objects is full, it will fail and return NULL.


    Or it may simply decide it’s better to move the block

    I just used the following program to get size of actually allocated memory with glibc:

    #include <stdlib.h>                                                          
    #include <stdio.h>                                                           
    
    int main()                                                                   
    {                                                                            
        int n;                                                                   
    
        for (n = 0; n <= 10; ++n)                                                
        {                                                                        
            void* array = malloc(n * sizeof(int));                               
            size_t* a2 = (size_t*) array;                                        
    
            printf("%d -> %zu\n", n, a2[-1]);                                    
        }                                                                        
    }
    

    and for n <= 6, it allocates 32 bytes, and for 7-10 it is 48.

    So, if it shrank int[10] to int[5], the allocated size would shrink from 48 to 32, effectively giving 16 free bytes. Since (as it just has been noted) it won’t allocate anything less than 32 bytes, those 16 bytes are lost.

    If it moved the block elsewhere, the whole 48 bytes will be freed, and something could actually be put in there. Of course, that’s just a science-fiction story and not a real implementation ;).


    The most relevant quote from the C99 standard (7.20.3.4 The realloc function):

    Returns

    4 The realloc function returns a pointer to the new object (which may have the same value as a pointer to the old object), or a null pointer if the new object could not be allocated.

    ‘May’ is the key-word here. It doesn’t mention any specific circumstances when that can happen, so you can’t rely on any of them, even if they sound obvious at a first glance.


    By the way, I think you could consider realloc() somewhat deprecated. If you’d take a look at C++, the newer memory allocation interfaces (new / delete and allocators) don’t even support such a thing. They always expect you to allocate a new block. But that’s just a loose comment.

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

Sidebar

Related Questions

If I have the next array: int[] arr = { 123, 243, 0, 0,
How can I move elements in an array to the next element eg: x[5]
Consider the following example program: next :: Int -> Int next i | 0
struct node { node(int _value, node* _next) : value(_value), next(_next) {} int value; node*
I have next code: int main() { OwnSelect(23, FD_READ | FD_WRITE); // <---- Several
What is the most elegant way to do the next stuff: int i =
struct Node { int value Node* next; } typedef List Node* const Set operator
I have the next scenario: I define an int[][] variable in my main class.
I have the next for loop header: for ( int row = interval[intRow][_START_]; row
I got next code: private Dictionary<int?, byte[]> GetAllLocalScanFiles() { using (DZine_IStylingEntities ctxLocal = new

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.