Access violation using vector::reserve()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mphil1974
    New Member
    • Mar 2008
    • 5

    Access violation using vector::reserve()

    Hello,

    I don't understand how the problem I'm having is even possible--

    lines.reserve(3 ); //Access violation

    'lines' is a vector of structs ('Line'). It doesn't matter whether I use a vector<Line>::s ize_type object in place of '3', I get the same result.

    Crucially, 'lines' is *not* empty at this point--the first element has been assigned.

    The books I've looked at (e.g. Josuttis) have given me no indication that a post-assignment call to 'reserve' should be illegal--is it?

    I'm new to C++ (but not C, Matlab), and I have run into errors arising from sloppiness on my part with 'const'--could that have something to do with it?

    Again, if people have ideas as to how this error is even possible I would appreciate it (posting all my code seems like it would just cloud the issue at this point).

    Thanks--
    Best,
    Matt
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Hi,
    Did your program first get ahold of that vector by calling a function that you defined? If it did, maybe you forgot the return statement on that function. I've gotten an access violation before from a missing a return statement. Hope this helps.

    Comment

    • mphil1974
      New Member
      • Mar 2008
      • 5

      #3
      No, I think my compiler (DevC++ 4.9.9.2) catches all of those (in any case, didn't happen this time).

      I should perhaps have been more clear--the access violation happens literally at the point of the reserve statement--not one statement before or one statement after.

      Best,
      Matt

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        Based on the docs I've seen, it's not the assignment. Declaring it as const, however, probably would make reserve() fail.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Soemthing else is going on. This code compiles and runs OK using Visual Studio.NET 2008:

          [code=cpp]
          struct Line
          {

          };
          int main()
          {
          vector<Line> lines;
          lines.reserve(3 ); //OK}
          [/code]

          Comment

          • boxfish
            Recognized Expert Contributor
            • Mar 2008
            • 469

            #6
            When I use reserve on a vector that was declared as const, I get some kind of compile error, not an access violation. I'm using Dev-C++ 4.9.8.0. The only thing I can think of is that there's something wrong with your vector. Are you sure posting some more of your code wouldn't help?

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              When you create a const, you can't change it. The reserve() would change the vector and that will cause a compiler error.

              Comment

              • mphil1974
                New Member
                • Mar 2008
                • 5

                #8
                Hello,

                Thank you everybody for your input, I'm glad to hear that const issues can be ruled out as the program compiled just fine.

                In fact it now looks like I wasted everyone's time, as I was able to trace the error back through STL to a copy constructor and an attempt to access info that didn't exist. The Line struct contained a member which is a recursive object (I'm writing a natural deduction program) so I had to create my own ctors. I still maintain not subjecting you to my code was the right thing to do and you would too if you saw it :).

                A related issue which I found even stranger, and which also went away when I resolved the ctor issue, was

                cout << lines.capacity( ); //Access violation if this line NOT included!!

                That's right--the program crashed *unless* this call to lines.capacity was made, and the result outputted. Was this some kind of DevC++ quirk? What does vector::capacit y do?

                Best,
                Matt

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  vector::capacit y() returns the current number of objects the vector can contain without allocating more memory.

                  Comment

                  • mphil1974
                    New Member
                    • Mar 2008
                    • 5

                    #10
                    Originally posted by weaknessforcats
                    vector::capacit y() returns the current number of objects the vector can contain without allocating more memory.
                    Right--I meant, what if any calls to possibly user-defined functions does vector::capacit y make? Does it call ctors or overloadable operators like '==' ? Thanks--

                    Best,
                    Matt

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      vector::capacit y() is a const member funciton so it cannot change any data in the vector. That means it cannot call any user-defined functions.

                      Comment

                      • mphil1974
                        New Member
                        • Mar 2008
                        • 5

                        #12
                        Originally posted by weaknessforcats
                        vector::capacit y() is a const member funciton so it cannot change any data in the vector. That means it cannot call any user-defined functions.
                        Okay, thank you!

                        Best,
                        Matt

                        Comment

                        Working...