access violation with delete

This is a discussion on access violation with delete within the C++ Programming forums, part of the General Programming Boards category; I am getting an access violation when I try to delete the temp object. It only get the error on ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    167

    access violation with delete

    I am getting an access violation when I try to delete the temp object. It only get the error on the third iteration of the outermost loop. I don't understand why I am getting the error because the object is declared at the beginning of the loop and destroyed at the end. Any ideas where the problem could be? Another thing, the problem compiles and runs fine with g++ but not with borland c++.

    Code:
     
    HugeInt HugeInt::operator*( const HugeInt &op2 )
    {
        HugeInt temp2;
        int carry = 0;
        int result = 0;
        int zeroCount = 0;
    
        for( int i = 29; i >= 0; i-- ){
    
          HugeInt *temp = new HugeInt;
    
          // Insert appropriate number of zeroes
          for( int a = zeroCount; a > 0; a-- ){
            (*temp).integer[30 - a] = 0;
          }
    
          for( int x = 29; x >= 0; x-- ){
            result = integer[x] * op2.integer[i];
            if( result > 10 ){
               (*temp).integer[x - zeroCount] = carry + ( result % 10 );
                carry = 1;
            }
            else {
               (*temp).integer[x - zeroCount] = carry + result;
               carry = 0;
            }
          }
          temp2 = temp2 + *temp;
          delete temp;
          zeroCount++;
        }
        return temp2;
    }
    silk.odyssey

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You'll have to step through the execution of this function to see where the memory corruption is. Without something to play with, I try to stay away from guestimating memory bugs.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    What could be causing the memory corruption?
    silk.odyssey

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    82
    Quote Originally Posted by silk.odyssey
    What could be causing the memory corruption?
    Just in general? Writing past the end of an array will do it, and its an easy error when doing a lot of math to find an array index.

    If you use your IDE's debugger, you should be able to step through your function line by line, which will show you where the problem is. That might point to a math error where an array index is off by 1 or some such.

    On that subject, you might want to replace some of the numeric literals in your function with constants. I notice that you keep using 30 and 29, but by using a constant, you make it much more explicit when you need 30-1, and its simple offsets like that which screw me up when working with arrays.
    Last edited by AH_Tze; 05-19-2004 at 11:07 PM.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    The code writes past the beginning of the array but when I tried to correct it it didn't work but thats a problem still so I will rewrite the function.
    silk.odyssey

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Start small and work up. Create the function with the minimum possible features, then compile and debug. Gradually add code, compiling and debugging at each step. I guarantee it will save you time in the development process.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Post your code for this function:
    HugeInt operator +( const HugeInt &op2 );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation?
    By rwmarsh in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2006, 11:56 AM
  2. DLL Access Violation?
    By durban in forum C++ Programming
    Replies: 12
    Last Post: 10-04-2005, 06:55 PM
  3. Access violation...
    By major_small in forum C++ Programming
    Replies: 5
    Last Post: 12-18-2003, 12:53 AM
  4. access violation
    By bonkey in forum C++ Programming
    Replies: 15
    Last Post: 11-20-2003, 10:22 AM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21