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. whats going on??????

whats going on??????

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 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.
  • R Offline
    R Offline
    Rajveer
    wrote on last edited by
    #1

    This is a C question. I have the following for loop in my program: for(double i=0.0; i<360.0; i+=3.0/10.0) { //do stuff } everything works fine, but I changed it to this because I need to generalize the code later on: double angle=3.0/10.0; for(double i=0.0; i<360.0; i+=angle) { //do stuff } and now the program crashes when I run it. I don't see the difference between either of those two. And "no" there is nothing complicated in the for loop such as changing the value of angle part way through while I'm running through the loop. So what is going on here????????

    E J 2 Replies Last reply
    0
    • R Rajveer

      This is a C question. I have the following for loop in my program: for(double i=0.0; i<360.0; i+=3.0/10.0) { //do stuff } everything works fine, but I changed it to this because I need to generalize the code later on: double angle=3.0/10.0; for(double i=0.0; i<360.0; i+=angle) { //do stuff } and now the program crashes when I run it. I don't see the difference between either of those two. And "no" there is nothing complicated in the for loop such as changing the value of angle part way through while I'm running through the loop. So what is going on here????????

      E Offline
      E Offline
      Ernest Laurentin
      wrote on last edited by
      #2

      I tried it and didn't see any crash! double angle=3.0/10.0; for(double i=0.0; i<360.0; i+=angle) { //do stuff TRACE1("%f", i); } Look at the reason of the error, it is an exception error? did you pass some invalid value to a math function (most of them will return NaN value)? The other thing I can say, debug (F9, F10, F11). "Dirty hands lead to important discovery..." - Thomas Edison

      1 Reply Last reply
      0
      • R Rajveer

        This is a C question. I have the following for loop in my program: for(double i=0.0; i<360.0; i+=3.0/10.0) { //do stuff } everything works fine, but I changed it to this because I need to generalize the code later on: double angle=3.0/10.0; for(double i=0.0; i<360.0; i+=angle) { //do stuff } and now the program crashes when I run it. I don't see the difference between either of those two. And "no" there is nothing complicated in the for loop such as changing the value of angle part way through while I'm running through the loop. So what is going on here????????

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

        A significative difference is that in the latter case, the increment value is stored in the stack (at least in debug mode), while in the former it is a constant value directly fed to the ADD assembly instruction. So, I'd bet for your program somehow corrupting he stack (maybe writing out of bonds of some array?) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        R 1 Reply Last reply
        0
        • J Joaquin M Lopez Munoz

          A significative difference is that in the latter case, the increment value is stored in the stack (at least in debug mode), while in the former it is a constant value directly fed to the ADD assembly instruction. So, I'd bet for your program somehow corrupting he stack (maybe writing out of bonds of some array?) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          R Offline
          R Offline
          Rajveer
          wrote on last edited by
          #4

          So how do I deal with something like that?

          J 1 Reply Last reply
          0
          • R Rajveer

            So how do I deal with something like that?

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

            To make sure, put something like this at the beginning of your for:

            for(double i=0.0; i<360.0; i+=angle)
            {
            assert(angle==3.0/10.0);
            ...
            }

            if the assert fires, then it is almost sure you're corrupting the stack. Check your indices, do a step-by-step run examining the exact moment when angle changes, etc. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            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