Thread: Fixing the size of an array as opposed to using a pointer

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    1

    Fixing the size of an array as opposed to using a pointer

    If I have an array and all I have is an upper limit on how big the array can get, and if the number of elements that get added can be considerably smaller than this limit, is it always the right choice to declare a pointer and just reallocate extra memory whenever the array grows?

    For instance, instead of declaring int a[max] I can declare a pointer int *a and than just realloc when I add elements. This would save memory. Is there any drawback to doing this?
    Last edited by zaphod; 10-15-2014 at 12:43 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The drawback is that it tends to be expensive (as in there's a cost in terms of time that might not be negligible) to call realloc for each new element. One way to mitigate this is to separate the notion of the number of elements in use from its capacity. That is, you start with some initial capacity, after which you can increase the capacity of the dynamic array by a factor (e.g., doubling it) when necessary (i.e., when the number of elements in use will exceed the capacity). This way, you trade having no unused space for fewer calls to realloc.

    However, when the dynamic array's capacity gets too large, it may be the case that you should increase the capacity by a constant amount instead of a constant factor, or risk allocation failure or simply too many unused elements. So, there is another drawback here in that your scheme for managing the capacity of an dynamic array could become far more complicated than if you just had a large fixed size array.

    Oh, and of course, you must remember to free what you malloc/realloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-24-2012, 04:33 PM
  2. size of array pointer
    By padaboo in forum C Programming
    Replies: 6
    Last Post: 02-11-2012, 08:25 PM
  3. How do I get the size of an array from a pointer to the array?
    By Programmer_P in forum C++ Programming
    Replies: 31
    Last Post: 06-03-2010, 04:13 PM
  4. What is int% opposed to int
    By koooee in forum C++ Programming
    Replies: 12
    Last Post: 06-12-2009, 05:00 AM
  5. Replies: 17
    Last Post: 10-31-2007, 08:43 AM

Tags for this Thread