Re: Strange "Stack Overflow" message occurring with use of vector
- From: "Alexander Grigoriev" <alegr@xxxxxxxxxxxxx>
- Date: Wed, 10 Jan 2007 21:04:42 -0800
Is there any particular reason you don't write 'ch[r]' instead of kludgy
*(ch + r)?
"ralphey" <Trapp.Andrew@xxxxxxxxx> wrote in message
news:1168490662.609154.101710@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hello,
Though I've gleaned quite a bit of helpful information from this forum,
this is the first time I've posted to it, and I'm hoping that someone
out there will be able to help me through a tough situation that I am
experiencing.
A bit of background:
In my C++ code (compiled with Visual Studio 2003), I take as input an n
by m size data array, analyze it, and then make use of a vector to
store a variable number of binary chains of fixed length m (e.g., m =
10, and a binary chain of length 10 would be: 0100101010). The code I
use to create the vector is as follows:
****Code snippet A****
vector<int> vec;
vec.reserve(1000000);
****Code snippet A****
I intentionally reserve more space than I will need for the vector (my
code manipulates large sets of data). Also, my understanding is that a
vector is self-contained, and I do not need to delete it (as I would
with a dynamic array).
The only time I add elements to the vector is in a function which is
called recursively, and to do this I am passing the vector to the
function via reference (using the & operator); furthermore, the
recursion can be expected to run many (in the 1000's of) times in my
code.
Here's the code that I use to store values in the vector:
****Code snippet B****
for (int r = 0; r < m; r++)
{
vec.push_back(*(ch + r));
}
****Code snippet B****
where m is #defined, and ch is a dynamic array that is functioning
properly. Later on in the code, which incidentally is the only other
time I touch the vector, it is accessed as follows:
****Code snippet C****
if (vec[v] == 1)
****Code snippet C****
So here's the problem. When I compile and run the code with n = 50, or
even 100, and m = 15, the code behaves perfectly. However, when I
increase the m dimension to size 20 (directly affecting the size of the
binary chains stored consecutively in the vector at each iteration),
the code breaks with a "Stack Overflow" error message. When I attempt
to debug, a file named "vector" (apparently a file internal to Visual
C++) appears, and the apparent code at which it broke is here:
****Code snippet D****
const_iterator(_Tptr _Ptr)
{ // construct with pointer _Ptr
_Myptr = _Ptr;
}
****Code snippet D****
I can't figure out why I am getting a "stack overflow" error when a
vector is created on the heap; it should be noted that I have taken all
means possible to replace all possible "stack" variables with "heap"
variables (that is, there were some intermediate arrays that I have
created dynamically instead of statically in order to save space on the
stack). Also, when I check the current values in the vector through the
watch window, only several hundred values have been set in the vector,
so I am not running out of size in the vector. At this point I am
completely puzzled and out of ideas, I have searched many times online
for help with "vector" and "stack overflow", but to no avail. Is there
somebody out there who can help?
I would be glad to post more code if it would help. If this was too
verbose, sorry about that, I chose to explain in detail what's going on
the first time around.
.
- References:
- Prev by Date: Re: Strange "Stack Overflow" message occurring with use of vector
- Next by Date: How to make a variable unique in the program?
- Previous by thread: Re: Strange "Stack Overflow" message occurring with use of vector
- Next by thread: Re: Strange "Stack Overflow" message occurring with use of vector
- Index(es):
Relevant Pages
|