Thread: A C++ Dynamic Array

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    3

    A C++ Dynamic Array

    Hi Devs,
    So i made this c++ project ,which is an array that allocates itself automatically so you can use it without defining its size and it works with all types
    here is the GitHub repository if you're interested ,please star it so more people can see it :
    GitHub - unknown989/cxx-dynamic-array: This is a Header-Only Dynamic Allocated Array Library for C++

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So how does this differ from std::vector or std::array ?

    I see your readme has "If you wanna use type std::string it won't work , so instead use char*"
    Does that go for anything else which isn't PoD ?
    IE, anything with a non-trivial constructor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    3
    Quote Originally Posted by Salem View Post
    So how does this differ from std::vector or std::array ?

    I see your readme has "If you wanna use type std::string it won't work , so instead use char*"
    Does that go for anything else which isn't PoD ?
    IE, anything with a non-trivial constructor.
    Well in std::array yoy have to set a size with the ability to update it.
    and it is the same as std::vector.
    It was intended to be a C project not a Cpp but i switched to cpp

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, I'd say that it is good that you're learning how to write a generic dynamic array class template from scratch, but you might want to do some reading on how to write classes that do resource management and what std::vector provides and why.

    For example, one obvious problem is that your Array does memory management, but you only implemented the default constructor and destructor: what happens should an Array get copied?

    A legitimate concern might be: how do I modify an element within an Array? Both the operator[] and the get member functions return by value.

    Another problem with your Array is that appending to it is slow: you only reallocate for one new element each time. The append also can unnecessarily result in two copies of the original object because you use a value parameter and then don't use move assignment within the append... although I'm not sure if copying/moving is going to work in the first place because normally we would use placement new as you only have memory allocated rather than a constructed object in the destination location.
    Last edited by laserlight; 05-08-2021 at 06:45 PM.
    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

  5. #5
    Registered User
    Join Date
    May 2021
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Well, I'd say that it is good that you're learning how to write a generic dynamic array class template from scratch, but you might want to do some reading on how to write classes that do resource management and what std::vector provides and why.

    For example, one obvious problem is that your Array does memory management, but you only implemented the default constructor and destructor: what happens should an Array get copied?

    A legitimate concern might be: how do I modify an element within an Array? Both the operator[] and the get member functions return by value.

    Another problem with your Array is that appending to it is slow: you only reallocate for one new element each time. The append also can unnecessarily result in two copies of the original object because you use a value parameter and then don't use move assignment within the append... although I'm not sure if copying/moving is going to work in the first place because normally we would use placement new as you only have memory allocated rather than a constructed object in the destination location.
    Hi, thanks for your feedback.
    i noticed that it is slow while appending compared to std::vector, it's 2-3 times slower.
    and for the allocation,it re-allocates itself if it expects an element size that is bigger than the current size of the array.
    I'm planning to make it equivalent to std::vector or close at least and why not better,i haven't gave it too much time,bcz it's just something i did to learn memory allocation in c++.if you got any tips on how to make it faster,tell me.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This looks pretty problematic:
    If you wanna use type std::string it won't work , so instead use char*
    My guess is that this goes back to my observation that within append you're assigning to an object that has not been constructed. You should look into the use of placement new to create and place the new object in the allocated memory, and then into the use of an explicit destructor call to destroy the object when you pop (and also to destroy any remaining objects when the Array is destroyed).
    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. why can't a static array copy values from a dynamic array
    By c++noob145 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2013, 10:25 AM
  2. Replies: 10
    Last Post: 12-03-2011, 02:26 PM
  3. Copy array into a Dynamic array
    By pantherman34 in forum C Programming
    Replies: 15
    Last Post: 05-01-2010, 10:58 PM
  4. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  5. dynamic array
    By sweets in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 11:24 AM

Tags for this Thread