?
Solved

C++ Vector Error

Posted on 2003-03-17
11
Medium Priority
?
273 Views
Last Modified: 2010-05-18
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
Comment
Question by:klax33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 1

Expert Comment

by:mphanke
ID: 8152154
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
 
LVL 1

Expert Comment

by:mphanke
ID: 8152295
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
 
LVL 3

Accepted Solution

by:
cmaryus earned 248 total points
ID: 8152574
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:mphanke
ID: 8152662
Hmm,

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

Martin
0
 
LVL 3

Expert Comment

by:cmaryus
ID: 8152687
why complicate things when vector class make them so simple :))
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 8155330
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
 
LVL 6

Expert Comment

by:Mafalda
ID: 8155340
The vector v I just mentioned in 1) is the vector that you pass to load and is defined outside the function.
0
 

Author Comment

by:klax33
ID: 8155808
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
 

Expert Comment

by:SpideyMod
ID: 8155845
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
 

Expert Comment

by:SpideyMod
ID: 8156919
mphanke,
The points listed above are for you.  Please claim them at your earliest convenience.

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question