we have to use realloc only after malloc or calloc ? or we
can use initially with out depending on whether we are
using malloc or calloc in our program ?

Answers were Sorted based on User's Feedback



we have to use realloc only after malloc or calloc ? or we can use initially with out depending on..

Answer / vadivel t

Hi Vignesh,

Ur explanation about realloc() is correct. But the answer
is wrong. Because, it is possible to use realloc() before
using malloc() or calloc() function be used.

Lets, try the below code.

int *ptr;
ptr = (int *)realloc(NULL, 5);
printf("%d \n", ptr);

In this code, realloc will allocate five bytes of memory
and will return a valid pointer. It can be used in ur
program.

Conclusion:

It simple means that, if u pass a NULL pointer to a realloc
() function, it will exactly behave as like malloc().

Is This Answer Correct ?    6 Yes 0 No

we have to use realloc only after malloc or calloc ? or we can use initially with out depending on..

Answer / vignesh1988i

ya we must use realloc (RE-ALLOCATION) only after malloc (MEMORY ALLOCATION) ....
actually realloc is a fuction which will be mainly implemented after knowing the accurate amount of memory that is going to be used... if our memory is allocated using malloc is less compared to the total values that is going to be stored we will RE-ALLOCATE the memory to our wish to store all the values...

but in other case if the data has been stored in the allocated block by malloc , but still more is remaining we can again RE-ALLOCATE usin realloc funcion.......


thank u

Is This Answer Correct ?    3 Yes 2 No

Post New Answer

More C Interview Questions

Reverse the bit order in a single macro. eg. i/p = 10010101 --> o/p = 10101001

2 Answers  


What is sparse file?

1 Answers  


Why is not a pointer null after calling free? How unsafe is it to use (assign, compare) a pointer value after it is been freed?

0 Answers  


can any one provide me the notes of data structure for ignou cs-62 paper

0 Answers   Ignou,


What is the difference between far and near in c?

0 Answers  






How can I change the size of the dynamically allocated array?

0 Answers  


Write a C program that computes the value ex by using the formula ex =1+x/1!+x2/2!+x3+3!+………….

1 Answers  


what is the difference between global variable & static variable declared out side all the function in the file.

2 Answers  


matrix multiplication fails introspect the causes for its failure and write down the possible reasons for its failurein c language.

5 Answers   TCS,


What is struct node in c?

0 Answers  


What is the 'named constructor idiom'?

0 Answers  


Explain argument and its types.

0 Answers  


Categories