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. a pointer i can not delete!

a pointer i can not delete!

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingperformancehelpquestion
4 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.
  • K Offline
    K Offline
    King Tran
    wrote on last edited by
    #1

    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

    F J 2 Replies Last reply
    0
    • K King Tran

      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

      F Offline
      F Offline
      followait
      wrote on last edited by
      #2

      I have tested the code in VC6, no error. Consider whether you used the memory after the delocation.

      1 Reply Last reply
      0
      • K King Tran

        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

        J Offline
        J Offline
        Jheriko
        wrote on last edited by
        #3

        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.

        K 1 Reply Last reply
        0
        • J Jheriko

          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.

          K Offline
          K Offline
          King Tran
          wrote on last edited by
          #4

          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

          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