Knowee
Questions
Features
Study Tools

In realloc(ptr, size), what happens if the size is set to zero?

Question

In realloc(ptr, size), what happens if the size is set to zero?

🧐 Not the exact question you are looking for?Go ask a question

Solution

In the realloc function, if the size is set to zero, it is equivalent to the function free(ptr). This means that the memory previously allocated at the pointer ptr is deallocated or freed, making it available for further allocations. However, the behavior is implementation-defined: realloc may return NULL or some other valid pointer. If it returns a non-null pointer, you should not use it, as its content is indeterminate. It's always a good practice to check the returned pointer from realloc, even when you're reducing the size to zero.

This problem has been solved

Similar Questions

What is the purpose of the realloc function?Marks : 1Negative Marks : 0Answer hereAllocates memory for a specified number of elements without initialization.Resizes the allocated memory block.Allocates memory for a specified number of elements and initializes them to zero.None of the mentioned options

Multi Choice Type QuestionWhat does realloc function do in C?Marks : 1Negative Marks : 0Answer hereAllocates memory and initializes it to zeroFrees allocated memoryChanges the size of the allocated memory blockAllocates memory without initializing i

hat is the purpose of the realloc function?Marks : 1Negative Marks : 0Answer hereAllocates memory for a specified number of elements without initialization.Resizes the allocated memory block.Allocates memory for a specified number of elements and initializes them to zero.None of the mentioned options

In C, what is the result of declaring a pointer as int *ptr; without initialization?Marks : 1Negative Marks : 0Answer hereThe value of the variableptr points to a specific known memory locationptr contains a garbage value as its addressptr points to the memory location 0

What will be the output of the following C code?int main(){char *ptr1, *ptr2;printf("%d %d", sizeof(ptr1), sizeof(ptr2));return 0;} 4 4Undefined2 21 1

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.