Strange "Stack Overflow" message occurring with use of vector



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.

.



Relevant Pages

  • Re: Strange "Stack Overflow" message occurring with use of vector
    ... with a dynamic array). ... recursion can be expected to run many times in my ... binary chains stored consecutively in the vector at each iteration), ... the code breaks with a "Stack Overflow" error message. ...
    (microsoft.public.vc.language)
  • Re: Keeping score
    ... Sorry for misunderstanding the first time. ... you are adding one to numCorrect or numIncorrect no matter ... I have tried using the qAnswered array found on the site, ... Dim visitedAs Boolean ...
    (microsoft.public.powerpoint)
  • Re: PHP Search & Links
    ... >> file as an array. ... >and to tell you the truth it is the first time i see something like ... I have replaced the $ at times or the echo I ... the part displaying the lines should display all lines and ...
    (alt.php)
  • Re: /dev/urandom vs. /dev/random
    ... Why am I seeing so many zeroes in my output? ... using POSIX I/O functions rather than the C ones. ... You are assuming that you know that the whole array was filled. ... The first time it got 1024 bytes. ...
    (comp.lang.c)
  • Re: Temperature Script
    ... > you want to take first time for day, ... > just a tip, when posting a question, it is great that you show code and ... > I need two items in this array like this ...
    (alt.php)