If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

C++ vector length error at run time

I'm having a problem with vectors, its the first time i'm using this structure so i'm probably missing something obvious.

What i'm trying to do is, create a class with a vector as a member, my code compiles with no errors but 4 warnings ... also to do with another vector.

the code compiles, but when debugging there is an error: vector<T> too long... also it says std::length_error at memory location 0x0030f1d8..

Just to explain my code, i'm trying to implement a bfs search for the 8 puzzle problem. I feel logically my code is correct and so i have not posted everything. Also it is a lot to read, but i have posted all the bits concerning the vectors.

Re: C++ vector length error at run time

Originally Posted by bloo

the code compiles, but when debugging there is an error

First thing you should know is that successful compilation means nothing when it comes to actually running the program. A successful compilation means the program is syntactically correct,. i.e. the C++ syntax is valid -- it doesn't determine if the program is logically correct or will run correctly.

Second, you say you're using vectors, but for the things you should be using vector for, you don't use it. Things like this:

Code like this is exactly what vector is supposed to replace. You then mishandle the pointers at the end, namely you use the wrong form of delete (you use "delete" when you should use "delete[]"). You also have a memory leak if levelb or levelc or leveld throw an exception on new, since you didn't deallocate the memory for the previous levels (you only do a return).

Also, I didn't even check if there is more mismanagement with these pointers.

If an exception is thrown, you now shouldn't call delete, as the vector will clean up the memory automatically when the function exits or returns for any reason whatsoever. IMO the number 1 reason to use vectors is for things like this, but you completely missed using them here.

Don't you mean "resize()"? The reserve() does not create valid entries in a vector. All it does is make sure there is preallocated memory up-front so that subsequent insertions in the vector take less time. The resize() is the function that actually creates entries in the vector.

I feel logically my code is correct and so i have not posted everything. Also it is a lot to read, but i have posted all the bits concerning the vectors.

More times then not, code that is not posted is the cause of the error. If you corrupt memory anywhere in your program, it can affect other parts of the code that you may think it won't affect.

here i'm trying to create arrays of objects because i'm building a tree, and cannot predetermine how many nodes will be needed at each stage. so i tried to automatically create them, as i mentioned before i'm doing a bfs search, so my idea was to add a vector to each node as they created and create them in the way a bfs would be performed in a tree, so i'm "creating and searching" at the same time. so each node in the tree will have a vector which will store the path to it.

here i did mean to preallocate memory in an effort to solve the error i mentioned in my previous post. i will post my complete code now. forgive me if u still feel i should create the nodes with vectors, i have spent a good few days on this code and i think u may be misunderstanding the point of the entire code.

Re: C++ vector length error at run time

Paul was suggesting that you should use vectors there, since you're already using them in other places.

Using 'reserve' is fine, as long as you know how it works. For example, this is wrong, but will compile fine:

Code:

std::vector<int> myVec;
myVec.reserve(50);
myVec[48] = 42;

Perhaps you meant to use "resize"?

Viggy

okay, but i don't totally understand how to use the vector in the first place and as i mentioned previously the vector is giving me the runtime error.
i do not want to resize them, as i explain the error says that the vector is too long and that's why i wanted to preallocate memory to it, should i resize it?

please take a look at my entire code and let me know if it's possible to simply resolve the current error, i know it could be completely wrong. all help is appreciated.

if Paul could please explain how i would use this, as u can see i tried to do everything with very simple knowledge of c++

will not work. The code above does not mean that the vector has 10 entries in it. It has zero entries, so accessing element 6, for example, is illegal and produce a runtime error for debug versions of Visual C++. This is probably the error you are referring to. The main use for reserve() is again, to preallocate memory so that subsequent calls to add items in the vector occur faster.

The resize() actually places 10 empty elements in the vector. So the above code will work if you replace reserve() with resize(). So, get rid of the reserve() and use resize() -- you want to access element x of a vector, that vector better have at least x+1 elements in it, and reserve() does not accomplish this -- resize() does.

I think you're confused as to what resize() does, because the name of the function is "resize". When you declare a vector, it is initially empty:

Code:

std::vector<int> v;

The vector v has no entries. Now what if you want it to have 10 entries, and you already declared it with 0 entries? You call resize().

Code:

std::vector<int> v;
//.. a bunch of code here, who cares
// now we want to make v have 10 elements
v.resize( 10 );

Understand now? resize(n) means "make my vector have n elements".

if Paul could please explain how i would use this, as u can see i tried to do everything with very simple knowledge of c++

A vector is a dynamic array -- you are trying to do dynamic arrays using the tricky and error-prone new[]/delete[]. So instead of using new[]/delete[] use vector, as that is exactly what it's designed to do. In addition, your code has a memory leak because of you mishandling new[]/delete[], so even if you didn't realize this in the beginning, we here are going to look at all the code you posted and comment on anything that is amiss.

As to how to use it, you use it exactly as I stated previously -- get rid of new[] and delete[] and use vector as a dynamic array of node.

That code is all you need, and you remove those lines that use new[]/delete[]. You also don't need those try/catches any more, and you don't need those delete's at the end of main(). In other words, your code will become smaller and safer using vector than when you were using new[]/delete[].

Re: C++ vector length error at run time

Originally Posted by Paul McKenzie

There is no such thing as an error saying a vector is "too long".

Perhaps unless something (completely unrelated) has corrupted some data members private to the vector object, thereby making the vector appear larger than vector::max_size() and triggering some internal assertion.

According to a little experiment I made, in a VC++ 2010 debug build vector<node>::max_size() (where node is the OP's class with commented-out c'tor and d'tor) returns 63161283 (0x3C3C3C3), which is way less than numeric_limits<size_type>::max().

Re: C++ vector length error at run time

A vector is designed to be a drop-in replacement for a dynamic array allocated with "new". That tells you 95% of what you need to know about vectors.

When you hit the runtime error, which line is indicated in your debugger?

Hi lindley I have tried what Paul suggested... using resize instead of reserve and I still get the same run time error, however I will use his other suggestions now and try again.

The error I receive is: Unhandled exception at 0x77a915ee in 8puzzle.exe: Microsoft C++ exception: std::length_error at memory location 0x0045f0cc
I am using Visual Studio 2008, with the library update.

Originally Posted by Paul McKenzie

There is no such thing as an error saying a vector is "too long". If anything, the vector is too small, and you're attempting to index an invalid entry.
Understand the difference between resize() and reserve(), and I mentioned this already in my first post.

The reserve() function does not create entries in the vector. Therefore this code:

will not work. The code above does not mean that the vector has 10 entries in it. It has zero entries, so accessing element 6, for example, is illegal and produce a runtime error for debug versions of Visual C++. This is probably the error you are referring to. The main use for reserve() is again, to preallocate memory so that subsequent calls to add items in the vector occur faster.

The resize() actually places 10 empty elements in the vector. So the above code will work if you replace reserve() with resize(). So, get rid of the reserve() and use resize() -- you want to access element x of a vector, that vector better have at least x+1 elements in it, and reserve() does not accomplish this -- resize() does.

I think you're confused as to what resize() does, because the name of the function is "resize". When you declare a vector, it is initially empty:

Code:

std::vector<int> v;

The vector v has no entries. Now what if you want it to have 10 entries, and you already declared it with 0 entries? You call resize().

Code:

std::vector<int> v;
//.. a bunch of code here, who cares
// now we want to make v have 10 elements
v.resize( 10 );

Understand now? resize(n) means "make my vector have n elements".
A vector is a dynamic array -- you are trying to do dynamic arrays using the tricky and error-prone new[]/delete[]. So instead of using new[]/delete[] use vector, as that is exactly what it's designed to do. In addition, your code has a memory leak because of you mishandling new[]/delete[], so even if you didn't realize this in the beginning, we here are going to look at all the code you posted and comment on anything that is amiss.

I do understand now, thank you for that explanation. I have tried this and still get the error which I stated above. However I will continue to make the changes you suggested and I will post my results. Thank you!

That code is all you need, and you remove those lines that use new[]/delete[]. You also don't need those try/catches any more, and you don't need those delete's at the end of main(). In other words, your code will become smaller and safer using vector than when you were using new[]/delete[].

Regards,

Paul McKenzie

Hi Paul, I just attempted changing this code now. The previous error does not come up, but instead I have a new error: Debug Assertion Failed! Program: ...8puzzle.exe File: ....\include\vector Line: 779
Expression: vector subscript out of range

By using vectors here, is there anything else I need to change, I read a bit and I think the way you access the objects is the same, is that correct? What other changes do I need to suit using vectors here?

Re: C++ vector length error at run time

Originally Posted by bloo

Hi Paul, I just attempted changing this code now. The previous error does not come up, but instead I have a new error: Debug Assertion Failed! Program: ...8puzzle.exe File: ....\include\vector Line: 779
Expression: vector subscript out of range

That is much better. Now you are told exactly what you were doing wrong before, and probably the reason why you were crashing from the beginning.

When you were using new[]/delete[] and pointers, you were overwriting memory. The problem is that overwriting memory when using raw pointers does not usually reveal itself when you run the program. Instead, you corrupt other areas of the program thereby getting the messages that "vector too long".

Now that you've changed to vector from using pointers, it is obviously clear that you were accessing invalid memory, but this time you get the indication that you're accessing an invalid vector entry. That is also the advantage of using vector (at least in debug mode). Visual C++ checks if you're using operator[] illegally, and you are.

The error means that your vector has a maximum of x elements, and you're writing beyond element x-1 (elements start at 0 and go to x-1). This is simple to diagnose, but you have to debug your program as to why you're writing beyond the legal limits of your vector.

By using vectors here, is there anything else I need to change, I read a bit and I think the way you access the objects is the same, is that correct?

It is exactly the same.

What other changes do I need to suit using vectors here?

None. You need to debug your code, as your quote below isn't correct:

I feel logically my code is correct and so i have not posted everything

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.