473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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>::size_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
Mar 18 '08 #1
11 3071
boxfish
469 Expert 256MB
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.
Mar 18 '08 #2
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
Mar 18 '08 #3
Laharl
849 Expert 512MB
Based on the docs I've seen, it's not the assignment. Declaring it as const, however, probably would make reserve() fail.
Mar 18 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
Soemthing else is going on. This code compiles and runs OK using Visual Studio.NET 2008:

Expand|Select|Wrap|Line Numbers
  1. struct Line
  2. {
  3.  
  4. };
  5. int main()
  6. {
  7.       vector<Line> lines;
  8.       lines.reserve(3); //OK}
  9.  
Mar 19 '08 #5
boxfish
469 Expert 256MB
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?
Mar 19 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
When you create a const, you can't change it. The reserve() would change the vector and that will cause a compiler error.
Mar 19 '08 #7
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::capacity do?

Best,
Matt
Mar 20 '08 #8
weaknessforcats
9,208 Expert Mod 8TB
vector::capacity() returns the current number of objects the vector can contain without allocating more memory.
Mar 20 '08 #9
vector::capacity() 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::capacity make? Does it call ctors or overloadable operators like '==' ? Thanks--

Best,
Matt
Mar 21 '08 #10
weaknessforcats
9,208 Expert Mod 8TB
vector::capacity() is a const member funciton so it cannot change any data in the vector. That means it cannot call any user-defined functions.
Mar 21 '08 #11
vector::capacity() 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
Mar 22 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: john smith | last post by:
HI, when I try the following code, I get a segfault when compiled with VC.NET and g++ under cygwin. #1 vector<int> vi; #2 vector<int>::iterator ii = vi.begin(); #3 vi.reserve(10); #4 ...
18
by: Alex Vinokur | last post by:
Hi, vector<T> v (100); T *a = new T ; v.push_back(T()); Is it possible to add new T to array to get _contiguous_ storage area (for 101 element in an example above)? -- Alex Vinokur
2
by: Gary Kuehn | last post by:
Is Reserve guaranteed to allocate contiguous memory? How safe is the following: vector<char> vbuff; int sz = numeric_limits<short int>::max();
7
by: Dilip | last post by:
If you reserve a certain amount of memory for a std::vector, what happens when a reallocation is necessary because I overshot the limit? I mean, say I reserve for 500 elements, the insertion of...
3
by: pkirk25 | last post by:
vector<stringbuf_string; buf_string.reserve(256); vector<intbuf_mat_prices; buf_mat_prices.reserve(1000); During loops I fill the vectors and then I empty them with commands like...
2
by: Chris Roth | last post by:
When I create a vector with: vector<doublev(10); am I right to assume that it has initialized ten elements with the number zero? What if I want to just make space for 10 elements I'll add...
2
by: NewlandArcher | last post by:
Understanding that fully-dynamic insertion and removal from lists is part of their appeal, is there any support for allocating a block of memory for the list to preferably operate in, a la the...
23
by: Mike -- Email Ignored | last post by:
In std::vector, is reserve or resize required? On: Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri Jul 27 18:10:34 EDT 2007 i686 athlon i386 GNU/Linux Using: g++ (GCC) 4.1.2 20070502 (Red Hat...
3
by: jason.cipriani | last post by:
What is the official word on what happens if I call reserve() on an std::vector, and specify a capacity that is less than the current *size* (not capacity) of that vector? Is it supposed to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.