My stack is corrupted?!?
-
Hey, I was just fooling around with what appeared to me to be a relatively straightforward bit of code when Visual Studio gave me with the error that "the stack around the variable 'count' was corrupted". I ran it in the debugger and everything looked fine, up until the moment the error popped up, which was between return 0; and the closing brace. The problem also seems to be dependent on the value passed to srand(). I would assume it had something to do with the program being unable to deallocate its memory or something, but I really don't have a good guess. Any ideas?
int main() { int count[10]={0}; srand(3); for(int x=0;x!=50000;x++) { count[(int)(rand()*10/RAND_MAX)]++; } for(int i=0;i!=10;i++) { cout<<"count["<
-
Hey, I was just fooling around with what appeared to me to be a relatively straightforward bit of code when Visual Studio gave me with the error that "the stack around the variable 'count' was corrupted". I ran it in the debugger and everything looked fine, up until the moment the error popped up, which was between return 0; and the closing brace. The problem also seems to be dependent on the value passed to srand(). I would assume it had something to do with the program being unable to deallocate its memory or something, but I really don't have a good guess. Any ideas?
int main() { int count[10]={0}; srand(3); for(int x=0;x!=50000;x++) { count[(int)(rand()*10/RAND_MAX)]++; } for(int i=0;i!=10;i++) { cout<<"count["<
count[(int)(rand()*10/RAND_MAX)]++;
This will corrupt the stack if rand() == RAND_MAX because count[10] is invalid. try this code instead:
count[rand() % 10]++;
John
-
Hey, I was just fooling around with what appeared to me to be a relatively straightforward bit of code when Visual Studio gave me with the error that "the stack around the variable 'count' was corrupted". I ran it in the debugger and everything looked fine, up until the moment the error popped up, which was between return 0; and the closing brace. The problem also seems to be dependent on the value passed to srand(). I would assume it had something to do with the program being unable to deallocate its memory or something, but I really don't have a good guess. Any ideas?
int main() { int count[10]={0}; srand(3); for(int x=0;x!=50000;x++) { count[(int)(rand()*10/RAND_MAX)]++; } for(int i=0;i!=10;i++) { cout<<"count["<
The expression
rand()*10/RAND_MAX
yields values between 0 and 10; when it returns 10, you're writing out of the bounds ofcount
, hence the problem (and the reason why it only pops up for certain values ofsrand(...)`.) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote [here](http://www.codeproject.com/script/comments/forums.asp?msg=910893&forumid=1645&mode=all&userid=111#xx910893xx)[[^](http://www.codeproject.com/script/comments/forums.asp?msg=910893&forumid=1645&mode=all&userid=111#xx910893xx "New Window")]!`
-
count[(int)(rand()*10/RAND_MAX)]++;
This will corrupt the stack if rand() == RAND_MAX because count[10] is invalid. try this code instead:
count[rand() % 10]++;
John
:doh: Oh my, too early in the morning. Thanks.