5

For an assignment I am supposed to recreate the C++ Vector library, and I am currently stuck on how to return maximum size.

According to this website: http://www.cplusplus.com/reference/stl/vector/ size_type max_size () const; Return maximum size

Returns the maximum number of elements that the vector container can hold.


Currently my functions are based on the manipulation of arrays and the vector library is not based on a template class, the vector library uses the int and size_t data-types. (I am unsure whether this has any bearing on it)

Relevant code:

class vector{
private:
int *vect;
size_t length;
size_t cap;

public:
//=====================Constructor================
vector(){
    length = 0;
    cap = 20;
    vect = new int[20];
}

//INCOMPLETE
size_t max_size() const{
    //return???!?!?!?!?
}

}

Scott Curtis
  • 91
  • 1
  • 5
  • What's your question? You have to keep track of the number of elements in your array. Which I assume is `length`. – wkl Feb 28 '12 at 02:25
  • No, as per the c++ reference website it states: This is not the amount of storage space currently allocated to the vector (this can be obtained with member vector::capacity), but the maximum potential size the vector could reach due to system or library implementation limitations. – Scott Curtis Feb 28 '12 at 02:27
  • Just figure out the constant that stl vector's max_size() returns and put it in your function? `size_t max_size() const { return some_huge_number_here; }` – Nic Foster Feb 28 '12 at 02:34
  • 1
    @ScottCurtis oops misread which function it was - yeah `max_size()` tends to be the max value of a native int, but there is no standard size. – wkl Feb 28 '12 at 02:54
  • 1
    possible duplicate of [C++ vector max_size();](http://stackoverflow.com/questions/3813124/c-vector-max-size) – netcoder Feb 28 '12 at 03:00
  • 1
    I am probably nitpicking, but you can choose the allocator used when declaring a STL vector, and max_size() seems defined by the allocator. So technically, you can have a STL vector having any max_size value you want, just need to provide your custom allocator. Anyway, a new student may safely ignore this. – Alink Feb 28 '12 at 03:20

2 Answers2

5

This is pertaining to the max size due to limitations within your library/code or system. For a very contrived example, say your implementation used an unsigned short (2 byte) number to store the number of records in your vector. Then your max_size function would return 65,536 as your library would have this limitation due to poor implementation.

As another and more realistic example, if you knew that the maximum size of your vector in bytes was limited to 4G, and the size being contained within the container was 128 bytes per instance, then max_size would return something to the tune of 33,554,431. (0xFFFFFFFF / 128)

Below is how this is done in my implementation of C++. Essentially, finding the largest value of the size_type (unsigned being -1) then dividing that by the size of the object being stored within the vector. (value_type) On 32 bit hardware, the size_type(-1) will yield 4,294,967,295, and if you were storing an unsigned int your value sizeof(value_type) would yield 4 bytes giving you a max_size() return value of 1,073,741,823.

/**  Returns the size() of the largest possible %vector.  */
size_type max_size() const
{ 
    return size_type(-1) / sizeof(value_type); 
}
RC.
  • 27,409
  • 9
  • 73
  • 93
  • Is there any method to determine the max size for different systems using a template class without knowing the system hardware limitations, I imagine there, but as a new c++ student, I am really unsure if/how this would be possible – Scott Curtis Feb 28 '12 at 02:37
1

From here in your link given:

but the maximum potential size the vector could reach due to system or library implementation limitations

In your case the max_size is the maximum size of an integer array on the system.

devil
  • 1,829
  • 16
  • 23
  • How do you measure this and return it, isn't the maximum size reliant on the physical memory of the system as well as the data type – Scott Curtis Feb 28 '12 at 02:31
  • One limit is the max value of std::size_t. The parts of the array can always be paged out. Also, take a look here: http://stackoverflow.com/questions/216259/is-there-a-max-array-length-limit-in-c – devil Feb 28 '12 at 02:46