#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    2
    Rep Power
    0

    same code, one function works, the other gives an access violation error


    Hello,

    I have a DCT and an iDCT function

    The DCT function starts as follows and the computer has no problems running through it.

    Code:
    BOOL DCT(MyImage image)
    {
    	int i,j,x,y;
    	float k;
    	int Width = image.getWidth();
    	int Height = image.getHeight();
    	char *Data = image.getImageData();
    	char *RBuf = new char[Height*Width]; 
    	char *GBuf = new char[Height*Width]; 
    	char *BBuf = new char[Height*Width]; 
    	if ((Data == NULL) || (RBuf == NULL) || (GBuf == NULL) || (BBuf == NULL))
    	{
    		return FALSE;		
    	}
    	
    	//I do not get a access violation here
    	for (i = 0; i < Height*Width; i++)
    	{
    		*(BBuf+i) = *(Data+3*i);
    		*(GBuf+i) = *(Data+3*i+1);
    		*(RBuf+i) = *(Data+3*i+2);
    	}
                    // function goes on to perform DCT and store the coefficients in the memory pointed to by Data
    The iDCT starts the same way, but I get an access violation error when running.

    Code:
    BOOL iDCT(MyImage image)
    {
    	int i,j,x,y;
    	float k;
    	int Width = image.getWidth();
    	int Height = image.getHeight();
    	char *Data = image.getImageData();
    	char *RBuf = new char[Height*Width]; 
    	char *GBuf = new char[Height*Width]; 
    	char *BBuf = new char[Height*Width]; 
    	if ((Data == NULL) || (RBuf == NULL) || (GBuf == NULL) || (BBuf == NULL))
    	{
    		return FALSE;		
    	}
    
    	//I am getting an access violation inside this loop
    	for (i = 0; i < Height*Width; i++)
    	{
    		*(BBuf+i) = *(Data+3*i);
    		*(GBuf+i) = *(Data+3*i+1);
    		*(RBuf+i) = *(Data+3*i+2);
    	}
    
                    //function is supposed to continue on to perform inverse DCT and store the RGB values in memory pointed to by Data
    Data points to a dynamic char array that is 3* Width * Height bytes long that is allocated by another function, which reads the data from a file.
    I call the iDCT function right after the DCT function in my program.

    I have tried accessing like standard arrays,

    BBuf[i] = Data[3*i];

    and using memcpy

    memcpy(BBuf+1,Data+3*i,1);


    but they all give the access violation error.
    Does anyone have an idea of what I am doing wrong? or why my program is throwing the exception for the iDCT function, but not the DCT function?

    Thanks in advance,

    Brandon Young
  2. #2
  3. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    The bits of the functions you've provided appear identical.

    My guess is that the problem is either in the caller, or in the code within DCT() that you have commented out. For example, you may have a pointer falling off then end of an array (eg pointing one or more past the end). Very easy to do that with pointer arithmetic if you're not careful.

IMN logo majestic logo threadwatch logo seochat tools logo