CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Join Date
    Nov 2010
    Posts
    13

    new [] / delete [] errors, going crazy!

    I am writing a program that allows for the creation of a theoretically infinitely large integer. I have created the class MyInt and implemented the storage using DMA.
    I have attached the .cpp and .h files. These are not the complete files, there are several other overloads but for simplocity sake I narrowed it down to only those functions involved in the problem.
    I have placed many cout statements in here trying to trace the problem. And the problem seems to be in the Grow() function that increases the size of the array. Any help would be greatly appreciated.

    Thanks in advance.
    Attached Files Attached Files

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: new [] / delete [] errors, going crazy!

    Yes in Grow you use delete instead of the proper delete[]

    Why struggle with all this memory management yourself? You could use a std::vector<unsigned int> instead.

    EDIT: std::vector<unsigned char> would be better to avoid current waste of memory (or change to use binary arithemetic)
    Last edited by S_M_A; November 7th, 2010 at 04:39 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Nov 2010
    Posts
    13

    Re: new [] / delete [] errors, going crazy!

    It's an assignment. They don't want you using things that might make your life too easy. I still can't use the string class much less vectors. I have tried putting the [] in there and it doesn't work either. I have tried everything I can think of. I have never been so stumped. There error I am getting starts with this, and then the "memory map" which I am not experienced enough to understand.

    *** glibc detected *** ./myint: free(): invalid next size (fast): 0x099bc020 ***

    It crashes when it tries to use delete. From the research I have done this error is often times caused by trying to delete an object/variable that was not created using new. Not sure I can see how that is the case here. It seems like amybe I am doing something wrong in line 178 where this is:

    unsigned int *newArray = new unsigned int[maxSize];

    Thanks again.

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: new [] / delete [] errors, going crazy!

    I would guess that you write out of bounds in either MyInt(int n) or the + operator. Have you runned the code in a debugger?

    EDIT: Have you tried using numbers <= 99999? For numbers > 99999 I guess this was supposed to kick in but it's not
    Code:
    int i = 0;
    while(i != 0) // Guess you meant to have n here?
    {
        intArray[i] = n % 10;
        n = n/10;
        currentSize++;
    
        if(currentSize == maxSize)
           Grow();
        i++;
    }
    Last edited by S_M_A; November 7th, 2010 at 05:11 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: new [] / delete [] errors, going crazy!

    Quote Originally Posted by weaverx9x9 View Post
    It's an assignment. They don't want you using things that might make your life too easy.
    Using container and string classes make things easier in terms of having working components that you can use to build the larger application. However, you still have to write the program using these components, so that part is still not "easy", but at least your thinking is now geared towards solving the actual problem that needs to be solved.

    Having said that, C++ beginner courses that still teach C++ as if it's 1990 and not 2010 should be abolished (if I had my way). It's the year 2010, and string classes, vectors, etc. have been standardized in C++ since 1998. I wish these courses were honest, and say you are learning 'C' and not C++.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: new [] / delete [] errors, going crazy!

    Another issue is your assignment operator:
    Code:
     if(this != &n)
     {
            delete [] intArray;
            intArray = new unsigned int[n.currentSize];
    What happens if new[] throws an exception? You've messed up your object by deleting your data.
    Code:
     if(this != &n)
     {
          unsigned int *temp = new unsigned int[n.currentSize];
          //...
          // copy data into temp
          //
          //
         delete [] intArray;
         intArray = temp;
         //
      }
    Now the memory is allocated before anything is done. If new[] throws an exception, then your original intArray is still intact.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 7th, 2010 at 05:44 PM.

  7. #7
    Join Date
    Nov 2010
    Posts
    13

    Re: new [] / delete [] errors, going crazy!

    Yeah your're right let me try that. I did mean n, not i.

  8. #8
    Join Date
    Nov 2010
    Posts
    13

    Re: new [] / delete [] errors, going crazy!

    Thanks Paul, actually most of that code block came from an example program from my proffessor. I will take a look at it again and see if I can find something. I had to take a break starting to see cross eyed and want to break stuff, just kidding. Thanks again, I will post what I find when I look at it.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: new [] / delete [] errors, going crazy!

    Quote Originally Posted by weaverx9x9 View Post
    Thanks Paul, actually most of that code block came from an example program from my proffessor.
    If it came from your professor, then it's quite revealing that he/she didn't see this issue, as anyone who is responsible for teaching a complex language such as C++ should/would have corrected it.

    More than likely, C++ is not this teacher's main programming language, which can be an issue when it comes to teaching C++ (as opposed to teaching other computer languages).

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Nov 2010
    Posts
    13

    Re: new [] / delete [] errors, going crazy!

    Actually c++ is his main course. I took him for intro to c++ and am currently taking oop with him. He has a Ph.d in something I'm guessing computer science.

  11. #11
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: new [] / delete [] errors, going crazy!

    Quote Originally Posted by weaverx9x9 View Post
    He has a Ph.d in something I'm guessing computer science.
    You may be surprised to learn that having a qualification in computer science has no bearing on your ability to program (or ever have programmed) in any computer language.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  12. #12
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: new [] / delete [] errors, going crazy!

    Quote Originally Posted by weaverx9x9 View Post
    It's an assignment. They don't want you using things that might make your life too easy. I still can't use the string class much less vectors.
    When you think you've got the hang of dynamic memory allocation, then maybe you could write you own simple string and vector classes and use them in future assignments that ban the STL. They can't complain about that!
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  13. #13
    Join Date
    Nov 2010
    Posts
    13

    Re: new [] / delete [] errors, going crazy!

    I have actually considered that. I wasn't sure which would be more work though, writting the vector and string classes or just using the libraries permitted. Next semester I'm taking data structures and algorithms. Apparently in that class they introduce the STL so I'm guessing at that point we will be able to use more of the libraries.

    In the prereq course intro to c++ we weren't even alowwed to use iomanip or cctype. We had to use ios:: to format output. I understand why they did it this way it's kind of like in math they didn't want you to use a calculator until you got to a certain level. It's just frustrating to know there is a tool that could make what I am trying to do allot easier.

    I was actually dreaming about this program last night. I think I got something, but I need to take a look at it first.

  14. #14
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: new [] / delete [] errors, going crazy!

    Quote Originally Posted by weaverx9x9 View Post
    I understand why they did it this way it's kind of like in math they didn't want you to use a calculator until you got to a certain level.
    That would be a defensible argument (though I still won't agree with it) iff you were taught to write completely safe programs. But as Paul has pointed out, the code provided by your professor is not safe. Hence, you are simply being taught bad programming practices, rather than the basics.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  15. #15
    Join Date
    Nov 2010
    Posts
    13

    Re: new [] / delete [] errors, going crazy!

    Yeah that's true D_Drmmr.

    I changed the overload of the assignment operator to reflect what Paul McKenzie suggested. I am still getting the same error though. I am going to stop by one of my teching assistants offices today at school. But I have to say, I don't have allot of faith in those guys.

    const MyInt MyInt:perator= (const MyInt& n)
    {
    if(this != &n)
    {
    // Create an array to copy data to before deleting intArray
    // in case an exception is thrown.
    unsigned int *temp = new unsigned int[n.currentSize];
    for(int i = 0; i < n.currentSize; i++)
    {
    temp[i] = n.intArray[i];
    }

    // Delete the old array.
    delete [] intArray;
    // Assign the new array pointer to original.
    intArray = temp;
    // Assign the size information.
    currentSize = n.currentSize;
    maxSize = n.maxSize;
    }
    return *this;
    }

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured