Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

C++ Vector Error

The following function generates an Access Violation error at the commented line.  I do not understand why it is so, because the .reserve(2) should have allocated enough memory for 2 locations.  Any help would be appreciated.  Thanks in advance.


void load(char filename[100], vector< vector<float> > &myVector)
{
     ifstream inf(filename);
     
     int num;
     inf >> num;

     myVector.reserve(num);
     for (int i = 0; i < num; i++)
          myVector[num].reserve(2);
     
     for (i = 0; i < num; i++)
     {
          float x, y;
          inf >> x;
          inf >> y;
          v[i][0] = x;   // access violation here
          v[i][1] = y;
     }
     

}
0
klax33
Asked:
klax33
  • 3
  • 2
  • 2
  • +2
1 Solution
 
mphankeCommented:
Hi,

as far as I know you can't encapsulate the vector template into each other. What you might do is create an array of vector.
The other thing is what STL are you using the SGI one or the one provided by Micro$oft? This might be a problem solver, too. I had problems when using the M$ stuff but this was resolved as soon as I used the SGI stuff.

Martin
0
 
mphankeCommented:
Hi,

okay I learnd something the thing you want to do is the following:

void load(char filename[100], vector< vector<float> > &myVector)
{
    ifstream inf(filename);
   
    int num;
    inf >> num;

    myVector.resize(num); // you didn't reserve mem for vectors!
    for (int i = 0; i < num; i++)
         myVector[i].reserve(2);
   
    for (i = 0; i < num; i++)
    {
         float x, y;
         inf >> x;
         inf >> y;
         myVector[i][0] = x;   // access violation here
         myVector[i][1] = y;
    }
}

For me this works.

Martin
0
 
cmaryusCommented:
The cleanest way would be: (no resize, no reserve):

int num;
inf >> num;

/*   myVector.resize(num); // you didn't reserve mem for vectors!
   for (int i = 0; i < num; i++)
        myVector[i].reserve(2);
*/  
   for (i = 0; i < num; i++)
   {
        float x, y;
        inf >> x;
        inf >> y;
        myVector[i].push_back(x);   // access violation here
        myVector[i].push_back(y);
// now at position 0 is x, at 1 is y
// generally is not recomended to explicit resize the vector, when you use push_back it's doing his own resizing.
   }
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mphankeCommented:
Hmm,

That sounds very reasonable. Didn't think about this possibility. Kinda narrow minded at the moment.

Martin
0
 
cmaryusCommented:
why complicate things when vector class make them so simple :))
0
 
MafaldaCommented:
1) use the syntax
  vector< vector<float> > v(10); // assign initial max size
when defining the vector variable.

2) change
    for (int i = 0; i < num; i++)
         myVector[num].reserve(2);

to

    for (int i = 0; i < num; i++)
         myVector[i].reserve(2);

And everything will work fine
0
 
MafaldaCommented:
The vector v I just mentioned in 1) is the vector that you pass to load and is defined outside the function.
0
 
klax33Author Commented:
Actually, you are both correct.  cmaryus' notation is more clean, but mphanke's correction about the .resize still must be done prior.  I have notified community support and the points will be split between the two of you.  Thank you both for your help. :)
0
 
SpideyModCommented:
Points reduced to 62 to facilitate the split.  

klax33 you can now accept one expert's comment as answer.  
Then create a "points for expertname re 20553482" (replace expertname with the actual expert name) question.

Put the new "points for" question in the same topic area: http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/

Make it's value 62 as well.

When finished, grab the URL for the new question, and return bakc here with it.  Paste a message stating "expertname, points for you at:" and paste the URL.  This way they get email notification that the points are there.

I fyou need assistance, or the "points for" question doesn't get posted within a day or two, somebody drop me a line.  Thanks.

SpideyMod
Community Support Moderator @Experts Exchange
0
 
klax33Author Commented:
0
 
SpideyModCommented:
mphanke,
The points listed above are for you.  Please claim them at your earliest convenience.

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now