a pointer i can not delete!
-
unsigned char* pImg1=new unsigned char[40+120*30]; unsigned char* pImg2=new unsigned char[40+92*30]; unsigned char* pDib1=pImg1+40; unsigned char* pDib2=pImg2+40; //int the next codes i give the pDib2 values to pDib1, assume, i have //given the pImg2 values before for(int i=0;i<30;i++) { int k=0; for(int j=0;j<90; ) { pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=0; } } delete []pImg1; delete []pImg2; when i want to delete the two pointers, there is debug error! i suspect the pImg1 memory space is bigger than its defination, but from the upper loop, the last value is pDib[29*120*90+29]=pDib[2599], the memory spance is just equal to the defination, what the matter? thanks first
-
unsigned char* pImg1=new unsigned char[40+120*30]; unsigned char* pImg2=new unsigned char[40+92*30]; unsigned char* pDib1=pImg1+40; unsigned char* pDib2=pImg2+40; //int the next codes i give the pDib2 values to pDib1, assume, i have //given the pImg2 values before for(int i=0;i<30;i++) { int k=0; for(int j=0;j<90; ) { pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=0; } } delete []pImg1; delete []pImg2; when i want to delete the two pointers, there is debug error! i suspect the pImg1 memory space is bigger than its defination, but from the upper loop, the last value is pDib[29*120*90+29]=pDib[2599], the memory spance is just equal to the defination, what the matter? thanks first
-
unsigned char* pImg1=new unsigned char[40+120*30]; unsigned char* pImg2=new unsigned char[40+92*30]; unsigned char* pDib1=pImg1+40; unsigned char* pDib2=pImg2+40; //int the next codes i give the pDib2 values to pDib1, assume, i have //given the pImg2 values before for(int i=0;i<30;i++) { int k=0; for(int j=0;j<90; ) { pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=0; } } delete []pImg1; delete []pImg2; when i want to delete the two pointers, there is debug error! i suspect the pImg1 memory space is bigger than its defination, but from the upper loop, the last value is pDib[29*120*90+29]=pDib[2599], the memory spance is just equal to the defination, what the matter? thanks first
Your illegal access is where j is reaching 90 when i is 30, the array bounds are 120*30, not 120*30 + 90 for pDib1, so "+ j" is taking you outside of the array. Is this code doing what you want? The k variable here is redundant. ... int k=0; // k = 0 for(int j=0;j<90; ) // k not modified { pDib1[i*120+j+k]=pDib2[i*92+j++]; // k not modified pDib1[i*120+j+k]=pDib2[i*92+j++]; // etc... pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=0; } Also, a much better way to access an array like this is to increment to vars by 120 and 92 for each loop with i, saves the continual multiplication by constants, a slow down which dwarfs any speed benefit you gain from putting your increment inside the index. Consider: int a=0, b=0; for(int i=0;i<30;++i) { for(int j=0;j<90;j+=3) { pDib1[a+j]=pDib2[b+j]; pDib1[a+j]=pDib2[b+j+1]; pDib1[a+j]=pDib2[b+j+2]; pDib1[a+j]=0; } a+=120; b+=92; } This is faster and more legible (and still broken!). Hope this helps.
-
Your illegal access is where j is reaching 90 when i is 30, the array bounds are 120*30, not 120*30 + 90 for pDib1, so "+ j" is taking you outside of the array. Is this code doing what you want? The k variable here is redundant. ... int k=0; // k = 0 for(int j=0;j<90; ) // k not modified { pDib1[i*120+j+k]=pDib2[i*92+j++]; // k not modified pDib1[i*120+j+k]=pDib2[i*92+j++]; // etc... pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=0; } Also, a much better way to access an array like this is to increment to vars by 120 and 92 for each loop with i, saves the continual multiplication by constants, a slow down which dwarfs any speed benefit you gain from putting your increment inside the index. Consider: int a=0, b=0; for(int i=0;i<30;++i) { for(int j=0;j<90;j+=3) { pDib1[a+j]=pDib2[b+j]; pDib1[a+j]=pDib2[b+j+1]; pDib1[a+j]=pDib2[b+j+2]; pDib1[a+j]=0; } a+=120; b+=92; } This is faster and more legible (and still broken!). Hope this helps.
thanks Jheriko++: it was my fault that i forgot writting a code line: int k=0; for(int j=0;j<90; ) { pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=pDib2[i*92+j++]; pDib1[i*120+j+k]=0; k++;// k is modified here! } my expectiong is that: //first loop pDib1[0]=pDib2[0]; pDib1[1]=pDib2[1]; pDib1[2]=pDib2[2]; pDib1[3]=0; //secont loop pDib1[4]=pDib2[3]; pDib1[5]=pDib2[4]; pDib1[6]=pDib2[5]; pDib1[7]=0; //the third loop pDib1[8]=pDib2[6]; pDib1[9]=pDib2[7]; pDib1[10]=pDib2[8]; pDib1[11]=0; //and so on //that int pDib1, every 4 numbers are one team, in pDib2, every 3 numbers are one team