Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with pointers and allocation of dynamic 2d array

Status
Not open for further replies.

bbonik

Technical User
Dec 28, 2009
4
GR
Dear all,

I am a new in Borland C++ 6 and I have a problem with pointers(!). I have a project in which i have to allocate a
dynamic 2-dimensional array, perform some image processing functions and then scan again all the values with the
use of a pointer. My code is this:




int i,j,width,height;
float *PP,q;


width=(size of image);
height=(size of image);


float **image = new float*[height];
for (i = 0; i < height; ++i)
{
image = new float[width];
}


/* some image processing*/


PP=&image[0][0];
for (i = 0; i < height; ++i)
{

for (j = 0; j < width; ++j)
{
q=*PP;

PP++;
}
}


Everything is going fine until, at a certain point, an exception is raised in line q=*PP; (where q reads the
value that address PP indicates).

The exception is: Project xxx.exe raised exception class EAccessViolation with message 'Access violation at address
004023C8 in module 'xxx.exe'. Read of address 00C44000'.

At the begining of the second nested 'for', the pointer PP contains the address of the first element of the array 'image'.
Every time the value is read the pointer increments, pointing to the next element of the array.

I have run the program in steps and i have found that the exception is raised when i=83 and j=width-3. All the previous values of
q are read correctly and are exactly the same as those of 'image'.

Why do you believe that this is happening? Why does it run correctly until i=83 and j=width-3?
Is there a problem with the logic that I used?

Thank you all in advance,

Bbonik
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top