开发者

adding element to an array

开发者 https://www.devze.com 2023-02-13 15:39 出处:网络
How can I add an element to an array where the size of the开发者_运维知识库 array is unknown and vectors are prohibited?If the size of the array is unknown how do you know where to put the element and

How can I add an element to an array where the size of the开发者_运维知识库 array is unknown and vectors are prohibited?


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.


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


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 :)


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 [].

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号