Wednesday 15 August 2012

Fixing size of array as opposed to using a pointer in C -



Fixing size of array as opposed to using a pointer in C -

if have array , have upper limit on how big array can get, , if number of elements added can considerably smaller limit, right selection declare pointer , reallocate memory whenever array grows?

for instance, instead of declaring int a[max] can declare pointer int *a , realloc when add together elements. save memory. there drawback doing this?

some drawbacks:

increased code complexity additional failure cases (realloc might fail) possible performance loss (realloc can slow if array has moved)

you have check , decide if drawbacks outweigh benefit of using less resources in cases. advantage don't have know maximum array size in advance.

c arrays pointers

No comments:

Post a Comment