0

I want to use realloc in C in an insert function for heap. This is the code:

typedef struct MaxHeap {
 int size;
 int* heap;
} MaxHeap;
void max_insert(MaxHeap* max_heap, int* key_index, int key) { // O(logn)
 max_heap->heap = realloc((max_heap->size+1), sizeof * max_heap->heap);
 max_heap[max_heap->size] = N_INF;
 max_heap->size += 1;
 increase_key(max_heap, key_index, max_heap->size, key)
}

And I get this warning:

warning: passing argument 1 of ‘realloc’ makes pointer from integer without a cast [-Wint-conversion] and I tried this fix:

max_heap->heap = realloc((max_heap->heap), (max_heap->size+1) * sizeof(*(max_heap->heap)));

Update

I did this:

void max_insert(MaxHeap* max_heap, int* key_index, int key) { // O(logn)
 int* temp = realloc (max_heap->heap, (max_heap->size + 1) * sizeof (*(max_heap->heap)));
 if (!temp) exit(1);
 max_heap->heap = temp;
 max_heap->heap[max_heap->size] = N_INF;
 max_heap->size += 1;
 increase_key(max_heap, key_index, max_heap->size, key);
 temp = 0;
}

And I got this error realloc(): invalid old size

Jim Mischel
135k25 gold badges197 silver badges377 bronze badges
asked Mar 5, 2023 at 6:56
1
  • 2
    NEVER realloc() to the original pointer. When (not if) realloc() fails, you overwrite the original pointer address with NULL, losing the address and creating a memory-leak. ALWAYS realloc() using a temporary pointer, e.g. void *tmp = realloc (max_heap->heap, new_size); and validate the realloc() succeeds before assigning to the original pointer, e.g. if (!tmp) { perror ("realloc-max_heap->heap"); /* handle the error, e.g. return NULL */ }; on success assign max_heap->heap = tmp;. If you alloc in a function, it must have a return type to indicate success/failure. Commented Mar 5, 2023 at 7:23

1 Answer 1

6

You've swapped the arguments to realloc().

(max_heap->size+1)

evaluates to an int, but the first argument to realloc() expects a void * pointer. Replace it with:

(max_heap->heap);

And the call to realloc() becomes:

realloc (max_heap->heap, (max_heap->size + 1) * sizeof (*(max_heap->heap)));

Note that this fails for two reasons:

  1. There was not enough memory and realloc() returned NULL, which will go unnoticed because we didn't check the return value of realloc(). A subsequent operation would now be writing to / dereferencing / reading from NULL, which would invoke undefined behaviour.
  2. If realloc() returned NULL, we would lose access to the original memory, which would result in a memory leak.

Fix:

Use a temporary pointer to hold the return value of realloc():

int *tmp = realloc (... , ...);
if (!tmp) {
 perror ("realloc()");
 /* realloc() was unable to allocate memory.
 * The original memory is left untouched.
 * Handle error here.
 */
} 
/* Now we can assign the result to `max_heap->heap`: */
max_heap->heap = tmp;
tmp = 0; /* This is unnecessary, but eliminates a dangling pointer. */
answered Mar 5, 2023 at 7:07
Sign up to request clarification or add additional context in comments.

3 Comments

Was max_heap() not already allocated with malloc() or a similar function? The question said resizing. How was this insert function called? Kindly edit the question and provide a minimal reproducible example.
That was the problem yes, I fixed it.
Do note that it's bad design, and lazy, for library code to abort a program, and that too without printing an error message. The standard library functions on error return an error code on allocation failure, they do not abort the program. Consider doing the same. Your insert() function could be declared to return a bool type instead of void, and return true on success, and false elsewise. The library user can then decide whether to exit the program or not. And consider accepting the answer if it you found the solution helpful.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.