C++ Vector - Is there a max. it can take?

Hi, I am implementating a C++ vector with struct. e.g. vector<student> s;

Is there a maximum number a vector can take?

I am reading a file into the vector and it is taking forever to display the result. I have close to 50,000 lines. Each line to be inserted into one vector.

I have no problem when i use the file with 1000 lines.

Please advise. There is no errors when i compile.
Yes, but it's a huge number. Check the size of size_t or how many bytes you have in your memory (whichever is smaller), divide that by the size of your student class and that's how many elements you could store in a vector.

-Albatross
The problem I think your having is that there may not be 5000 student sized bytes of memory that is consecutive (right next to each other). You may need to look into a different storage method, such as a list.

To find a max of the student vector use the vector.max_size() method.
Last edited on
do you mean like this:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <vector>
#include <conio.h>
#include <string>
using namespace std;

int main () {
    vector <string> s;
    cout << sizeof (size_t) / sizeof (s) << endl;
    getch();
    return 0;
}


but the result is 0
Try:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <limits>
#include <string>
#include <vector>

int main ()
{
  std::vector <std::string> s;
  std::cout << s.max_size() << " is the biggest vector you can make using strings.";

  std::cin.ignore(std::numeric_limits <std::streamsize>::max(), '\n');
  return 0;
}


EDIT: Of course this can be wrong as strings are variable in length.

EDIT 2:
Best seen with comparison to long double which my compiler defines as 12 bytes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <limits>
#include <string>
#include <vector>

int main ()
{
  std::vector <std::string> s;
  std::vector <long double> ld;

  std::cout << s.max_size() << " is the biggest vector you can make using strings.\n";
  std::cout << ld.max_size() << " is the supposed largest vector that can be made using long doubles.\n";

  std::cin.ignore(std::numeric_limits <std::streamsize>::max(), '\n');
  return 0;
}


1073741823 is the biggest vector you can make using strings.
357913941 is the supposed largest vector that can be made using long doubles.
Last edited on
50,000 elements is certainly far away from any limit that there might be.
Reading the file and storing the elements in the vector should take no longer than a fraction of a second.
You might want to post your code for review.
Of course this can be wrong as strings are variable in length.
do you mean that string doesn't have a fix size? but isn't it that it size is 4 byte?
yes of course. Usually all the heap segment of memory. This of course fluctuates and changes as your kernel needs it for other processes.
BTW, the heap is memory that you completely control. Other segments of memory that you have to worry about are data, bss, text and stack.

data and bss being global/static and uninitialized variables... text being read only instructions for the processor being converted to asm/machine and stack... which is a bunch of stack frames that remembers the current context your in.

Idk, if i'm not 100percent correct, someone can correct me. (yay i learn something!)
Are you using the vector's reserve() method to precommit storage?

If not... when you know up front that you're expecting a lot of vector elements, you should use the vector's reserve() method to precommit storage. Otherwise every few push_back() calls the vector is going to have to resize its memory. This can have a high impact on speed.

Also, do you actually need to hold all entries in memory at once for your purpose?

Andy

@chipp

1
2
vector <string> s;
... sizeof (size_t) / sizeof (s)  ...


- sizeof(size_t) = 4
- a vector is a struct with various members, including the size of the buffer, so whatever size it is, it's bigger than 4.
=> and in integer arithmatic 4/5 = 0 R 1

char* variables have a size of 4 bytes (in 32 bit case), but they are just pointers to the actual null terminating string, which can be any length (well, memory allowing)

And the size of std::string (Visual C++ 2008 release build) is 28 bytes. But this does not include the buffer.
Last edited on
Topic archived. No new replies allowed.