none
Access violation reading location 0xcdcdcdcd.

    Question

  • In the Header file  "DrawCurves.h" , I declared  double B[200];

    Code snippet :

    CDrawCurves::CDrawCurves(void)
    {
    B[0]=1.0;
    for(int i=1;i<200;i++)
    B[i]=0.0;
    }

    CDrawCurves::~CDrawCurves(void)
    {
    }
    void CDrawCurves::allBernstein(int n, double u, double B[]) {
      int j,k;
      double u1 = 1.0 - u;
      double saved;
      //B[0] = 1.00;
      for (j = 1; j <= n; j++) {
        saved = 0.0;
        for (k = 0; k < j; k++) {
          double temp = B[k];
          B[k] = saved + u1*temp;
          saved = u*temp;
        }
        B[j] = saved;
      }
    }
    void CDrawCurves::pointOnBezierCurve(float P[][3], int n, float u, float C[3])
    {  
      int k;
      allBernstein(n, u, B);
      C[0] = C[1] = C[2] = 0.0;
      for (k = 0; k <= n; k++) 
      {
        C[0] += B[k]*P[k][0]; <------- It is locating error here with  B[K]
        C[1] += B[k]*P[k][1];
        C[2] += B[k]*P[k][2];
      }
    }

    I hope everything is done properly. I don't understand why i am getting the runtime error 
    " Unhandled exception at 0x0041327f in SurfaceDraw.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.
    I am using Visual studio 2008 MFC for drawing curves.
    Could anyone please help me.

    Wednesday, March 17, 2010 12:42 PM

Answers


  • As Nikita said, i checked n variable.No problem because of that.But i don't understand why this is happening.

    I changed some other code where this function is called. It is working now.
    Thanks for all the replies.
    • Marked as answer by Nancy Shao Wednesday, March 24, 2010 9:40 AM
    Thursday, March 18, 2010 1:00 AM

All replies

  • It would happen if n > 199 because there is no B[200]

    You don't show where n is set.

     

    Wednesday, March 17, 2010 1:01 PM
  • Hi Scott,

    Thanks for the reply. Now i made it B[400] and initialized.  But no improvement. 
    still i am getting the same error.
    ........
    Harinath
    Wednesday, March 17, 2010 1:19 PM
  • The debugger will show you what's wrong.  Set a breakpoint on that line and look at all the variables.
    Wednesday, March 17, 2010 1:41 PM
  • Thanks for the quick reply. Yes, i have done. It is showing expression can't be evaluated. 
    Still i can't rectify the error.
    Wednesday, March 17, 2010 1:57 PM
  • Check n variable.
    Wednesday, March 17, 2010 2:45 PM

  • As Nikita said, i checked n variable.No problem because of that.But i don't understand why this is happening.

    I changed some other code where this function is called. It is working now.
    Thanks for all the replies.
    • Marked as answer by Nancy Shao Wednesday, March 24, 2010 9:40 AM
    Thursday, March 18, 2010 1:00 AM
  • If I remember correctly, the debug builds set the memory that is deleted (or deallocated) to 0xCD before it is actually deleted.  So most likely B[] or P[] have been deleted by the time you reach that line.  Track the life cycle of the arrays.

    To find out which array, examine each pointer to see which one is pointing to a block of 0xCD or which one has a value of 0xCDCDCDCD (this is the most likely case).
    MCP
    Thursday, March 18, 2010 4:27 AM
  • When the error occur, what is the value of n variable?
    I am still think that the n variable is out of safe range.

    Thursday, March 18, 2010 6:57 AM
  • I agree with the others.

    webJose - Yes, 205 is a familiar value to me, as well. VC 1.52 (iirc) did not do that. At the time of VC 5.0, 0xcd was used as a flag of some sort. I think that it was a fill value for uninitialized pointers, too.

    bob.shao - I think so, too.

    Harinath CAU - I think it is this your for loop. Ordinarily, [I] do not run a loop on an array from 0 to <= the count of items in the array, that will go beyond the last element, since arrays are zero based. That upper bound limiter comes into the function, so the caller must know the size of arrays B and P to call it safely.

    Do you get an error if you change the loop to be:

    for (k = 0; k < n; k++)

    Naturally, if it fails to error and fails to draw the Bezier properly....you might not be adding all of the elements to the array in to first place.
    Thursday, March 18, 2010 1:04 PM
  • Yes.
    It has been my experience that 0xcdcdcd... is because the graphics device itself  did not initialize properly. 

    I would simply (for now) comment out all the drawing code and proof read the setup/init methods of the graphics device that acquire a handle to the device itself.  I maybe even tempted to use verbatim the initialization code of a graphics device from the many, I assume DirectX, examples on the web.

    I of course could be wrong.
    Thursday, March 18, 2010 2:44 PM