Heap corruption with dynamic memory

This is a discussion on Heap corruption with dynamic memory within the C++ Programming forums, part of the General Programming Boards category; I am using visual studio running a win32 console application. I'm new to dynamic memory and i am trying to ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    5

    Heap corruption with dynamic memory

    I am using visual studio running a win32 console application.
    I'm new to dynamic memory and i am trying to think of some extra things for an easy assingment. Basically I am trying to allow an input for the array size. I know this requires dynamic memory and i have it set up for that. I get an error after the program finishes with
    " Windows has triggered a breakpoint in (my program).

    This may be due to a corruption of the heap, which indicates a bug in (my program) or any of the DLLs it has loaded."
    It then brings me to a breakpoint in the ostream, if i run it with 2 as my input, it gives me a different error.

    Here's my code.
    Code:
    #include <iostream>
    using namespace std;
    // ------------------Function Prototypes -----------------------------
    void EnterData (int *grades, int,int&);
    void OutputData (int *grades, int,int);
    //--------------------------------------------------------------------
    void main()
    {	
    	int x;
    	int *grades;
    	cout<<"How many students would you like to calculate? ";
    
    	cin>>x;;
    
    	grades=new int(x);
    	int total=0;
    	int	NUMSTUDENTS=x;
    	
    
    	// enter the grades information
    	EnterData (grades,NUMSTUDENTS, total);
    
    	// display the grades information
    	OutputData (grades,NUMSTUDENTS,total);
    	delete grades;
    	system ("pause");
    
    }// end main
    //--------------- Function definitions to follow --------------------------
    void EnterData(int *grades, int NUMSTUDENTS, int &total)
    /*	This function allows the user to enter the grades information into the data array.
    	Pre: The grades array has been allocated sufficient memory
    	Post: The grades entered by the user are returned within the array	*/
    {
    	int studentNumber;
    	
    	
    	cout<<"Please enter the grades for the "<<NUMSTUDENTS<<" students...\n\n";
    	for (studentNumber = 0; studentNumber<NUMSTUDENTS; studentNumber++)
    	{
    		cout<<"Enter grade #"<<studentNumber+1<<": ";//counter, inputs the data
    		cin>>grades[studentNumber];//into each array component starting at zero
    		total+=grades[studentNumber];//total for finding average
    	}
    }// end EnterData
    //--------------------------------------------------------------------------
    void OutputData(int *grades, int NUMSTUDENTS, int total)
    /*	This function displays the grades to the user
    	Pre: The grades array has already been filled with data
    	Post: The grades are displayed to the user				*/
    {
    	int studentNumber;
    
    	cout<<"The grades for the "<<NUMSTUDENTS<<" students are:\n\n";
    	for (studentNumber = 0; studentNumber<NUMSTUDENTS; studentNumber++)
    	{
    		cout<<"Student #"<<studentNumber+1<<": ";
    		cout<<grades[studentNumber]<<endl;
    	}
    	cout<<"The average is: "<<double((total/NUMSTUDENTS))<<endl;
    }// end OutputData
    Thanx for any help. Hope its not to easy.
    Last edited by Head In Jar Man; 01-13-2009 at 08:57 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    grades=new int(x);
    This allocates one int with the value of x.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    totally just realized this as you replied. Why does it run anyways though? I mean, shouldn't it give me a compile error if i try to assign values to an array segment that doesn't exist?
    Last edited by Head In Jar Man; 01-13-2009 at 09:20 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why shouldn't it? If you have a pointer, you need to know yourself how many items it points at. C++ is not supposed to check your pointer accesses at runtime.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Because the compiler does not know what you do at runtime; it only knows what you do at compile-time.
    Also consider using std::vector and reading http://www.research.att.com/~bs/bs_faq2.html#whitespace and http://cpwiki.sourceforge.net/Void_main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by anon View Post
    Why shouldn't it? If you have a pointer, you need to know yourself how many items it points at. C++ is not supposed to check your pointer accesses at runtime.
    He's suggesting that it shold be able to detect array access, for something not allocated as an array, at compile time. This could perhaps have been possible if C++ had different types of pointers, one for a pointer to a single object, and one for a pointer to an array, but the language doesn't do that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    But boost::array triggers an assert if you try to write or read a non-existing element in the array, though. I find that of help.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by Elysia View Post
    But boost::array triggers an assert if you try to write or read a non-existing element in the array, though. I find that of help.
    Yes that's all well and good, but again that's not at compile time. He's wondering why it doesn't fail to compile due to using (x) instead of [x], and the answer is that the language just doesn't make it possible to detect that as any kind of error, because both are perfectly valid and useful in their own way.

    Sure, using something like a vector makes it impossible to make this exact mistake though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In theory, the compiler could attempt to remember what is going on with allocated memory and how large it is. But this would ONLY work for situations where the number of objects allocated with new is a constant - in the above case that is true, but the absolutely most common case of using new to allocate memory and then using the pointer as an array is when the number of elements is variable. And that case is much harder for the compiler to understand.

    Code-checkers, such as lint and coverity are sometimes able to follow these things better, but certainly do not catch EVERYTHING.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heap corruption errors
    By VirtualAce in forum C++ Programming
    Replies: 0
    Last Post: 07-15-2006, 04:46 PM
  2. Dynamic memory allocation
    By amdeffen in forum C++ Programming
    Replies: 21
    Last Post: 04-29-2004, 08:09 PM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. delete dynamic memory
    By VanJay011379 in forum C++ Programming
    Replies: 1
    Last Post: 07-18-2002, 12:44 PM
  5. heap memory management
    By jdinger in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2002, 10:23 PM

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