1

I tried to find similar title questions with mine, but I didn't find what I wanted. So here is my question. If I malloc an array with size of 10, is it able to realloc the size and make it 9? And the 8 etc etc ? And if it's able, if I have this array: [1,2,3,4,5,6,7,8,9,10] How can I know which cell will be deleted ? Is it able to choose which one to delete every time ?

6
  • 1
    Isn't it clear in the manual? The realloc() function tries to change the size of the allocation pointed to by ptr to size, and returns ptr. Commented May 13, 2020 at 11:40
  • 1
    Why wouldn't it be able? The whole point of realloc is to change the size. Please add details to your question. Commented May 13, 2020 at 11:40
  • Is it able to choose which one to delete every time ? - no, you can only remove from, or add to, the end. Commented May 13, 2020 at 11:48
  • No you can't choose. The end of the array is lost if the size is decreased. Also covered in the realloc man page: The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes Commented May 13, 2020 at 11:49
  • Thank you @500-InternalServerError . But what if I changed 2 with 10, delete the 2 and then put again the 10 at the end ? You think this will work ? Commented May 13, 2020 at 11:51

2 Answers 2

2

When you realloc with a smaller size, many implementation just do nothing: you have a bloc that can hold at least 10 elements, it can hold at least 8 elements. Simply you shall no longer use the elements past the declared size. Nothing will prevent you to do it, but it just invokes Undefined Behaviour.

It can be surprising for beginners:

int *arr = malloc(10 * sizeof(int));   // allocate an array of 10 int
for (int i=0; i<10; i++) {             // initialize it with 0-9
    arr[i] = i;
}
arr = realloc(arr, 8*sizeof(int));     // realloc the array to 8 elements
for (int i=0, i<8; i++) {
    printf(" %d", i);
}
printf("\n");                          // should have correctly printed 0 1 ... 7
// the interesting part
for (int i=8; i<10; i++) {
    printf(" %d", i);                  // Oops, UB!
}
printf("\n");                          // but what is to be expected is just 8 9...

Now it works but is UB. Say differently never pretend that I said that it was correct code. But most implementations will accept it and will give the expected result (which is allowed by UB...) without any other side effect.

Sign up to request clarification or add additional context in comments.

Comments

1

The realloc() and reallocarray() functions serve this purpose. The method header is actually:

void *realloc(void *ptr, size_t size);
void *reallocarray(void *ptr, size_t nmemb, size_t size);

Where *ptr is the pointer to the memory that you want to reallocate.

However, you can't choose. If you increase the size, the old elements will remain as they are, and there will be some elements at the end that are undefined (null). If you decrease it, the memory space will be "cut", that means, if you have space for 10 elements, and you realloc for 6, the last 4 elements will not be accessible.

If you want to achieve that behaviour, arrange your array before reallocating

Much more information can be found in the man pages (enter link description here) and in several websites.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.