Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. My stack is corrupted?!?

My stack is corrupted?!?

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpvisual-studiodata-structuresdebugging
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nathan Addy
    wrote on last edited by
    #1

    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["<

    J J 2 Replies Last reply
    0
    • N Nathan Addy

      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["<

      J Offline
      J Offline
      John M Drescher
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • N Nathan Addy

        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["<

        J Offline
        J Offline
        Joaquin M Lopez Munoz
        wrote on last edited by
        #3

        The expression rand()*10/RAND_MAX yields values between 0 and 10; when it returns 10, you're writing out of the bounds of count, hence the problem (and the reason why it only pops up for certain values of srand(...)`.) 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")]!`

        1 Reply Last reply
        0
        • J John M Drescher

          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

          N Offline
          N Offline
          Nathan Addy
          wrote on last edited by
          #4

          :doh: Oh my, too early in the morning. Thanks.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups