0

Given such a struct, how can I create a expandable(vector) array?

struct IntArray {
   int size;
   int capacity;
   int *contents;
};

I was thinking of using the int *contents to point to a array of integers and just change the pointer as the IntArray expands, but how will I know what size to make my initial array of integers?

5
  • "expandable array" you mean std::vector Commented Oct 11, 2013 at 5:45
  • Why don't you just use a vector Commented Oct 11, 2013 at 5:45
  • Because I am playing around with pointers at the moment Commented Oct 11, 2013 at 5:46
  • 3
    You would do something like pass the size in a constructor, then allocate an array of that size and make contents point to it. Then you would implement the copy constructor, assignment operator and destructor (look up "the rule of three". Once you have that up and running, you can start thinking of the expanding part. Commented Oct 11, 2013 at 5:46
  • 1
    Well you won't learn much if you just ask SO how to create thing your trying to make instead of struggling with it yourself Commented Oct 11, 2013 at 5:47

1 Answer 1

1

As @aaronman said, it would be no good for you if we just gave you the final solution: if you do want to learn something, you'd better try this on your own from scratch (otherwise just use existing std::vector or QVector or whatever vector).

So instead I would suggest you to see some tutorials or some exapmles for inspiration, and then start from scratch. Even simple google search for c++ tutorial implementing dynamic array will give some tolerable videos like this as well as some text links.

After you are clear with the implementation details, you might want to design the interface of your array, and the points to consider may be as following:

  1. Make this a class instead of struct to make the fields be private by default (encapsulation)
  2. Implement initial allocation in constructor and de-allocation in desctuctor (see @juanchopanza's comment). Copy constructor and assignment operator are also good, but IMHO can be omitted at this point (e.g., you can make them private without implementation to simply prohibit copying)
  3. Implement all your functionality as methods, for example, a method push_back might add to end. You will see that adding element can cause reallocation, so it would be a good idea to extract reallocation process into another (possibly private) method (to make it reusable) and implement it using ideas from that video, then call it from push_back if size == capacity.
  4. Learn about operator overloading, and implement operator[] to have access to elements just like in normal array.
  5. ...Continue adding functionality until you enjoy your implementation :) I think it is important when learning to have enjoyment from creating something neat.

Finally you can compare your implementation with std::vector or some other existing dynamic array to see if you are missing something. Some three years ago I had to make own implementation of vector in my diploma work (for fine-grained control of its modification and reallocation for performance reasons), you might also check it out, but (disclaimer!) you might find it kinda complicated and messy after all my attempts of optimization :)

Go ahead! Hope this helps (and inspires you a bit).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.