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:
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.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++; } } }
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.



2Likes
LinkBack URL
About LinkBacks




My prime() function calls extend_primes() and vice versa. "n" wasn't getting corrupted; extend_primes(100) was calling prime() with odd values which was causing extend_primes() to get called with odd values also. I think it is an iterator/overrun problem in extend_primes() which causes prime() to get called with random junk.