CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    May 2013
    Posts
    5

    Heap Corruption during run time.

    Hello, I am having a problem with my code. I am trying to delete an array, after I created a bigger one, and copied all the values from the previous to the new one, but when i try to delete the old one, I get : heap corruption detected after normal block. Please help me. Here is the code:

    this is the add function:

    Code:
    bool IntArrayList::add(Number& num)
    {
    	if(m_Arr[0] == NULL)///If array is empty put the first element at 0 index
    	{
    		m_Arr[0] = #
    		m_Size += 1;
    		return true;
    	}
    	else
    		{
    			int in_place = SearchInsertPlace();///find the place where you store another element
    			if(in_place != -1)///there is still place available
    			{
    				m_Arr[in_place] = #
    				m_Size += 1;
    				return true;
    			}
    			else///if no free places, create new array
    			{
    				m_Arr = CreateNewArray(m_Arr);
    
    				in_place = SearchInsertPlace();
    				m_Arr[in_place] = #
    				return true;
    			}
    		}
    }

    this is the function that creates the new array:

    Code:
    Number** IntArrayList::CreateNewArray(Number** arr)
    {
    	m_ArraySize *= 2;
    	Number** newArray = new Number*[m_ArraySize];
    		for(int i=0 ; i<m_ArraySize ; i++)
    			newArray[i]=NULL;
    
    	for(int i = 0 ; i < m_Size ; i++ )/// creating new array.
    		{
    			newArray[i] = m_Arr[i];
    			m_Arr[i]=NULL;
    	    }
    	//for(int i = 0 ; i < m_Size ; i++)/// delete old array.
    	//	delete m_Arr[i];
    	delete []m_Arr;
    
    	return newArray;
    }
    thanks!
    Attached Images Attached Images  

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Heap Corruption during run time.

    In your add function, you're passing in a Number& and storing the address. Since we have to guess here, I'd guess that the number you're passing in is a local that goes out of scope, so you're storing and trying to delete the address of an object that no longer exists.

  3. #3
    Join Date
    May 2013
    Posts
    5

    Re: Heap Corruption during run time.

    thanks, so I don't need to delete the old one?

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Heap Corruption during run time.

    You shouldn't store the address of a local object that goes out of scope. If you're doing something like

    Code:
    Number num;
    myList.add(num);
    you're writing buggy code. As soon as num goes out of scope, you have an invalid pointer.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Heap Corruption during run time.

    Why not just use the STL std::vector?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    May 2013
    Posts
    5

    Re: Heap Corruption during run time.

    can't use the stl. it's a homework, so they want us to write everything by ourselves to practice.

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

    Re: Heap Corruption during run time.

    Quote Originally Posted by EdwardKes View Post
    can't use the stl. it's a homework, so they want us to write everything by ourselves to practice.
    But with the questions you're asking, why was this given as an exercise, especially with a complex language such as C++? I bet that 100% of the students will get something wrong.

    The only persons who can write a dynamic array class properly are intermediate (and even that may not be enough) to advanced C++ programmers. I have yet to see a student or beginner write a dynamic array class properly. The student versions are either always buggy, or have such glaring holes that are easily demonstrated by simple test programs.

    What I'm saying is that I hope that the teacher sees the attempts and points out the mistakes.

    Regards,

    Paul McKenzie

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

    Re: Heap Corruption during run time.

    Quote Originally Posted by EdwardKes View Post
    Hello, I am having a problem with my code. I am trying to delete an array, after I created a bigger one, and copied all the values from the previous to the new one,
    You should really post all of this code instead of just describing it. We don't know if that code also has problems.

    In addition, where is your test program? Another thing that students or beginners tend to do is write test programs that will avoid the bugs in the code, giving the false impression that the code "works".

    Regards,

    Paul McKenzie

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Heap Corruption during run time.

    What exactly did the assignment require? Were any of the class definitions/methods provided or have you devised your own?

    In a c++ class I wouldn't expect a member function to have a definition of

    Code:
    Number** IntArrayList::CreateNewArray(Number** arr)
    I would expect a private function with a definition something like

    Code:
    void IntArrayList:: ResizeArray()
    In add you have
    Code:
    m_Arr = CreateNewArray(m_Arr);
    If I understand your class (and it is always better and of much more use to post the whole program as suggested by Paul), m_Arr is a member variable of the class - so why is CreateNewArray taking as argument a variable to which it has access and returning a pointer to a pointer which is then assigned back to the class member variable? How is m_Arr defined in the class?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Heap Corruption during run time.

    Please tell your teacher that they need to stop giving you guys idiotic restrictions that only serve to make you work AGAINST established industry standards. It's teachers like that which end up giving us improperly prepared recruits where the first thing we have to do when they come to work for us is weeks of trying to stomp out bad coding practices.

  11. #11
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Heap Corruption during run time.

    Quote Originally Posted by EdwardKes View Post
    Code:
    bool IntArrayList::add(Number& num)
    {
    	if(m_Arr[0] == NULL)///If array is empty put the first element at 0 index
    	{
    		m_Arr[0] = &num;
    		m_Size += 1;
    		return true;
    	}
    	else
    		{
    			int in_place = SearchInsertPlace();///find the place where you store another element
    			if(in_place != -1)///there is still place available
    			{
    				m_Arr[in_place] = &num;
    				m_Size += 1;
    				return true;
    			}
    			else///if no free places, create new array
    			{
    				m_Arr = CreateNewArray(m_Arr);
    
    				in_place = SearchInsertPlace();
    				m_Arr[in_place] = &num;
    				return true;
    			}
    		}
    }
    There are so many things wrong with this both from a technical POV as a design POV that I'm not even sure I know where to start.

    lets have a start
    technical: why are you testing for m_arr[0] being NULL when the obvious check is if size == 0
    design: why does your class even assume there will always be an allocated array, it should handle "empty" better by not requiring an allocated array when no such array has been needed yet.

    technical: m_Arr[0] = &num;
    why are you storing a pointer to a local stackbased variable ?

    design: why does an add function return true, when it makes more sense to return an index to the added array element or a iterator to the added element.

    technical SearchInsertPlace();
    it's unclear if this function only does a search or also makes room for the add. The name seems to indicate it doesnt. Byt in the "there is still place" branch there doesn't seem to be any code that takes care of moving all the elements up by one.
    design: this also assumes that the search functions knows that there isn't any more room, so either it's name is in approproate, or you have gaping functionality holes here.

    design: every possible branch in your code seems to return true, so what's the point of always returning the same value ?



    I can't even tell if all of this is a matter of incomplete/confusing assignment given by the teacher, or you not properly following said assignment.

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

    Re: Heap Corruption during run time.

    I agree. The assignment is not meant for beginners, and possibly not even for intermediate programmers. Why teachers in beginner or close to beginner classes seem to always give these type of assignments is beyond me, unless the teacher is trying to prove a larger point when it comes to C++.

    As I stated, I bet that 100% of the students in this class makes some sort of mistake, even ones that the teacher cannot detect (but some of us here would see at first glance). All assignments like this do is give the student who thinks they got a working program believe that the program actually is really working or usable, and not wrought with issues, mistakes, etc..

    Regards,

    Paul McKenzie

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Heap Corruption during run time.

    Quote Originally Posted by OReubens View Post
    Please tell your teacher that they need to stop giving you guys idiotic restrictions that only serve to make you work AGAINST established industry standards. It's teachers like that which end up giving us improperly prepared recruits where the first thing we have to do when they come to work for us is weeks of trying to stomp out bad coding practices.
    I agree. IMO part of the 'problem' is that whilst students are taught how to write correct c++ syntax, they are not taught properly how to design a program - what the classes should be, what are the required functions, what are the desired interfaces of the classes/functions etc. Also some teachers try to provide non-trivial assignments that use the knowledge that has been taught so far which can involve programming 'contortions' to do something that is either already present in the language (including the STL) or which in the real world would be programmed using a feature of the language that has not yet been covered on the course so the students don't know about it - or worse have been told not to use it. Also some c++ courses do not cover the STL or templates in any great detail so students who have passed the course (and often have a certificate) don't even know all of the facilities available that can be used - never mind how best to use them! No wonder so many new recruits have bad coding practices. I feel sorry for the students as some are being badly let down by their course tutors. I often wonder exactly what knowlege these 'teachers' have themselves. I once came across a c 'teacher' who was literally teaching from a book and whose knowledge of c was one chapter ahead of what was being taught!

    IMO what is needed is a proper formal accreditation scheme for c/c++ teachers (similar to Microsoft MCT).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

    Re: Heap Corruption during run time.

    IMO this is the reason why Java is more readily taught in schools over C++. The Java student is never assigned anything like this. Not only that, the Java student is taught to use what is available in the JDK, therefore the backflips and gymnastics that teachers enforce onto a C++ beginner practically never occurs for the Java student.

    In short, the Java student is creating usable programs, while the C++ student is wasting time creating buggy "IntArrayLists".

    Regards,

    Paul McKenzie

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Heap Corruption during run time.

    Quote Originally Posted by Paul McKenzie View Post
    IMO this is the reason why Java is more readily taught in schools over C++. The Java student is never assigned anything like this. Not only that, the Java student is taught to use what is available in the JDK, therefore the backflips and gymnastics that teachers enforce onto a C++ beginner practically never occurs for the Java student.

    In short, the Java student is creating usable programs, while the C++ student is wasting time creating buggy "IntArrayLists".
    Probably because Java/JDK is taught integrated as a 'whole' - whereas c++ tends to be taught as four distinct parts 'the c part of c++, classes, STL and Templates. Many courses just do the 'c' part and elementary classes - with proper class design, STL and templates only getting a cursory mention. IMO c++ should be taught the same way as Java - as an integrated whole with students using - and encouraged to use - all of the available features as needed.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

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