0

I am trying to replace VC2010 default runtime calloc with my own implementation of calloc

I tried this code but my program still uses the default VC2010 calloc

void* calloc(size_t num, size_t size) {
// Calculate the total size. Check for potential overflow.
if (size * num / num != size) { // Basic overflow check. More robust checks are possible.
  return NULL; // Or handle the error in another way.
}
size_t total_size = num * size;

// Allocate memory using malloc.
void* ptr = malloc(total_size);

// If allocation fails, return NULL.
if (ptr == NULL) {
    return NULL;
}

// Zero out the allocated memory using memset.
memset(ptr, 0, total_size);

return ptr;

}

0

Your Answer

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.