Performance impact of realloc ()

Viewed 18125

I have a list of records, at the starting I dont know the number of records. I need to read them into array. so is it advisable to read all record one by one & doing realloc one by one & go on increasing the array size as the element comes OR should I spend one pass in identifying the number of records & do malloc only once ? Which one will be be less computationally expensive ?

4 Answers

You could also consider using a linked list instead of an array for your task if it is possible. It will require only malloc to add new elements.

Related