Is Vector Reserve guaranteed to allocate contiguous memory?

Discussion in 'C++' started by Gary Kuehn, Jul 18, 2005.

  1. Gary Kuehn

    Gary Kuehn Guest

    Is Reserve guaranteed to allocate contiguous memory?

    How safe is the following:



    vector<char> vbuff;

    int sz = numeric_limits<short int>::max();

    // reserve contiguous memory IS THIS ALWAYS TRUE ?

    vbuff.reserve(sz);

    ifstream file;

    try {

    file.open(fname.c_str(), ios_base::in);

    // first line should be the equity ticker identifiers

    file.getline(&vbuff[0], sz);



    Thanks
    Gary
     
    Gary Kuehn, Jul 18, 2005
    #1
    1. Advertising

  2. Gary Kuehn wrote in news:Q4WCe.16914$ in comp.lang.c++:

    >
    > Is Reserve guaranteed to allocate contiguous memory?


    Yes, but its implementation defined if that memory will
    actually be there when you need it.

    Some OS's only bprovide real memory when you use it, if some other
    process (ay) comes along and uses up all the real memory and
    swap file between when you "reserve" memory an actaly use it
    then ou're out of luck.

    >
    > How safe is the following:
    >


    Not at all, you need resize, not reserve.

    reserve only allocates memory, its not available until
    you actually populate the vector.

    >
    >
    > vector<char> vbuff;
    >
    > int sz = numeric_limits<short int>::max();
    >
    > // reserve contiguous memory IS THIS ALWAYS TRUE ?
    >
    > vbuff.reserve(sz);
    >


    vbuff.resize( sz );

    Even better (for paranoid people anyway):

    vbuff.resize( sz, '\0' );

    as this will copy a 0 char to every location

    It "uses" the memory, so you know its real not virtual.

    > ifstream file;
    >
    > try {
    >
    > file.open(fname.c_str(), ios_base::in);
    >
    > // first line should be the equity ticker identifiers
    >
    > file.getline(&vbuff[0], sz);
    >


    How about:

    std:string s;
    std::getline( file, s );

    I.e. let the standard library do all the memory allocation.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Jul 18, 2005
    #2
    1. Advertising

  3. Gary Kuehn

    Gary Kuehn Guest

    Thanks for the critique.
    I'm reviewing all your input, thanks again.

    Gary

    "Rob Williscroft" <> wrote in message
    news:Xns9697F297A8094rtwfreenetREMOVEcouk@216.196.109.145...
    > Gary Kuehn wrote in news:Q4WCe.16914$ in comp.lang.c++:
    >
    >>
    >> Is Reserve guaranteed to allocate contiguous memory?

    >
    > Yes, but its implementation defined if that memory will
    > actually be there when you need it.
    >
    > Some OS's only bprovide real memory when you use it, if some other
    > process (ay) comes along and uses up all the real memory and
    > swap file between when you "reserve" memory an actaly use it
    > then ou're out of luck.
    >
    >>
    >> How safe is the following:
    >>

    >
    > Not at all, you need resize, not reserve.
    >
    > reserve only allocates memory, its not available until
    > you actually populate the vector.
    >
    >>
    >>
    >> vector<char> vbuff;
    >>
    >> int sz = numeric_limits<short int>::max();
    >>
    >> // reserve contiguous memory IS THIS ALWAYS TRUE ?
    >>
    >> vbuff.reserve(sz);
    >>

    >
    > vbuff.resize( sz );
    >
    > Even better (for paranoid people anyway):
    >
    > vbuff.resize( sz, '\0' );
    >
    > as this will copy a 0 char to every location
    >
    > It "uses" the memory, so you know its real not virtual.
    >
    >> ifstream file;
    >>
    >> try {
    >>
    >> file.open(fname.c_str(), ios_base::in);
    >>
    >> // first line should be the equity ticker identifiers
    >>
    >> file.getline(&vbuff[0], sz);
    >>

    >
    > How about:
    >
    > std:string s;
    > std::getline( file, s );
    >
    > I.e. let the standard library do all the memory allocation.
    >
    > Rob.
    > --
    > http://www.victim-prime.dsl.pipex.com/
     
    Gary Kuehn, Jul 19, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    0
    Views:
    403
    Newsgroup - Ann
    Aug 15, 2003
  2. Ravi
    Replies:
    8
    Views:
    10,327
    Mike Wahler
    Mar 6, 2004
  3. Olumide
    Replies:
    9
    Views:
    1,924
    Dan Pop
    Nov 30, 2004
  4. Replies:
    8
    Views:
    1,939
    Csaba
    Feb 18, 2006
  5. Phil Endecott
    Replies:
    9
    Views:
    479
    Abhishek Padmanabh
    Feb 21, 2008
Loading...

Share This Page