Take the 2-minute tour ×

How can I add an element to an array where the size of the array is unknown and vectors are prohibited?

share|improve this question
Show us the code that you've come up so far. –  Maxim Yegorushkin Feb 25 '11 at 12:50
If you are going to ask 'scholarly' questions, please consider using capitalization and punctuation. I've edited your question to increase its over-all quality. –  Tim Post Feb 25 '11 at 13:09
add comment

4 Answers

If the size of the array is unknown how do you know where to put the element and whether it will fit?

Anyway, if it won't fit you have to allocate a new array that is big enough.

If you allocated originally with malloc rather than new[] you can use realloc. You might be surprised to know that malloc / realloc are not "replaced" by new but are used for a different purpose, and in this case it is a useful thing to use. You can then insert objects into the allocated memory using placement new, and vector works this way. (allocator is used to actually allocate the memory but has an interface like malloc and although the implementation is up to the library author, they will almost certainly use malloc).

If you reallocated using realloc, you need to know that:

  • Any memory will be copied over. Beware though that if they are non-POD objects stored it is not safe to just do byte-by-byte copy

  • If realloc fails it returns NULL and your previous array is not freed. Be certain to keep a pointer to the old location until you know realloc worked.

  • If your array is not POD you cannot realloc, so malloc the new memory and use placement-new with a copy-constructor, then call the destructor on each object of the old memory before freeing it.

  • Placement new is used so you can allocate more memory than you need for now, i.e. more than you have objects for. This prevents you having to go through this process every single time you append.

  • I have explained how you might implement vector, but this is probably far too complex for you and probably for your tutor too. So just create an array with new[] and copy over the elements although it is horribly inefficient if you have to do this every time you add one.

share|improve this answer
add comment

You use lists for this purpose. An array is not supposed to be extended.

share|improve this answer
Arrays can still be used. They need to be allocated from dynamic memory. The answer to the OP's assignment is to create an array class that has the same functionality as an array but also maintains the capacity. If an insert occurs beyond capacity, the class needs to allocate a large array, copy elements over and delete the previous array. –  Thomas Matthews Feb 25 '11 at 17:24
Also, a list violates the contiguous allocation property of an array. –  Thomas Matthews Feb 25 '11 at 17:25
@Thomas Matthews - I agree question does not mention any requirements. It seems that you are describing ArrayList implementation from .NET. –  Jenea Feb 26 '11 at 9:45
add comment

Your question does not make sense. If you can't use vector, means this is an assignment. I assume you have to use native arrays. If you don't know the size, you just cannot add an element to it. If you knew the size, you would have to allocate a bigger array, copy the contents of the old array plus the new element. Which the vector actually does for you :)

share|improve this answer
add comment

Without knowing the current size of the array and the capacity of the array (the distinction there is important), adding elements to the array is dangerous. You will never know when you have passed the bounds of the array (at least not before your program crashes from a buffer overrun error).

If your professor has banned vector for an assignment, chances are he wants to show you how to manage memory. He's looking for you to create an array using new and properly recognize when you need to allocate a new array of a larger size, copy the original elements, and deallocate the original array using delete.

One hint most professors fail to mention: when you allocate an array using new [], you must also deallocate it using delete [].

share|improve this answer
add comment

Your Answer


