How to watch stack variable for corruption?

I have a monster of a function that is corrupting a stack variable (an argument which was passed to the function) somehow. How can I set GDB to watch a local variable?

Here's my code:

Code:

`#define I unsigned int`

vector<I> primes{2, 3};

void extend_primes(I n)

{

static unsigned int primorial_index = 0;

static I primorial = 2;

static vector<I> addends{1};

I base = primes.back() - primes.back() % primorial;

auto offset = addends.begin();

while(base + *offset <= primes.back())

offset++;

while(primes.back() < n)

{

while(primes.back() < n && offset < addends.end())

{

if(prime(base + *offset))

primes.push_back(base + *offset);

offset++;

}

base += primorial;

offset = addends.begin();

if(primes.back() > primorial * primes[primorial_index + 1])

{

primorial_index++;

primorial *= primes[primorial_index];

addends = {1};

for(auto i = primorial_index + 1;primes[i] < primorial;i++)

addends.push_back(primes[i]);

base = primes.back() - primes.back() % primorial;

auto offset = addends.begin();

while(base + *offset <= primes.back())

offset++;

}

}

}

Somewhere in it, "n" is getting corrupted. It doesn't get corrupted when I comment out the last if-statement, but I still don't see what in that block could be corrupting it.

In case you're wondering, I'm using "I" for "unsigned int" because I plan to change it to "mpz_class" when I have GMP available.

Thanks for your time,

Tyler

Edit: I know it isn't kosher to post code that can't be compiled and ran, but it's rather large and obnoxious. If anyone wants it, just say so.