Click here to Skip to main content
15,886,919 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionCorruption of the Heap Error Pin
Foothill14-Feb-11 10:40
professionalFoothill14-Feb-11 10:40 
AnswerRe: Corruption of the Heap Error Pin
David Crow14-Feb-11 11:55
David Crow14-Feb-11 11:55 
GeneralRe: Corruption of the Heap Error Pin
Foothill14-Feb-11 14:26
professionalFoothill14-Feb-11 14:26 
GeneralRe: Corruption of the Heap Error Pin
David Crow14-Feb-11 17:11
David Crow14-Feb-11 17:11 
GeneralRe: Corruption of the Heap Error Pin
ShilpiP14-Feb-11 19:13
ShilpiP14-Feb-11 19:13 
AnswerRe: Corruption of the Heap Error Pin
ShilpiP14-Feb-11 19:11
ShilpiP14-Feb-11 19:11 
AnswerRe: Corruption of the Heap Error Pin
Niklas L14-Feb-11 21:17
Niklas L14-Feb-11 21:17 
AnswerRe: Corruption of the Heap Error Pin
Stefan_Lang15-Feb-11 6:52
Stefan_Lang15-Feb-11 6:52 
Foothill wrote:
#include

is missing the filename to be included, e. g.
#include <io.h>

Foothill wrote:
int Iarray[0];

This technically declares an array of 0 integers. However, I believe for some obscure reasons compilers are obliged to always allocate at least one element for an array, so in the end your struct will contain exactly one int. What you probably meant is (also see below):
int *Iarray;

Foothill wrote:
IntArray* A = (IntArray*)new int(sizeof num);

As has been pointed out, this allocates memory for just one int and initializes it with 2. Also you are blindly casting the result of the allocation to IntArray*. You cannot equate the struct you defined with the array you defined within that struct even though the compiler will permit this.

First, sizeof delivers the size of the memory required to hold a variable of a particular type, in this case short. If you want the value that a variable holds, just write its name, num, instead.

Second, new takes several parameters, but it's syntax is special:
new int
allocates memory for one variable of type int, and returns a pointer to its address.
new int(5)
allocates memory for one variable of type int, and initializes it with the value 5, then returns a pointer to its address.
new int[5]
allocates memory for a block of five variables of type int, and returns a pointer to the start of that block, holding the first element.

In all these cases, the return type is int*. So you could for instance assign any of the above expressions to A->Iarray, without the need for a type cast. The following line for example would allocate a block of 20 integers and assign its address to A->Iarray:
A->Iarray = new int[num];

However, that would assume you have a variable A of type struct IntArray, which you could dynamically allocate with
IntArray *A = new IntArray;

Of course, you could just as well define A as a local variable like this:
IntArray A;

You would need to write A.Iarray instead of A->Iarray then of course, since A is no longer declared as a pointer.
Foothill wrote:
for ( short i = 0 ; i < 10 ; ++i )

You should use the correct limit for the loop, i. e. num instead of 10, assuming that you indeed correctly allocated an array of num elements.
Foothill wrote:
cout << A->Iarray[j]," ";

The '<<' operator should be used for each element that you want to print out individually, until the end of the operation; writing cout << "hello " << "world"; will give you the same result as cout << "hello "; cout << "world";, but in the first case you need the second <<! What you probably wanted to write was
cout << A->Iarray[j] << ", ";

Foothill wrote:
delete A;

With the incorrect use of new above, this line is correct, and nothing will happen. But if you correct your allocation statement as indicated above, you need to specifically tell the compiler that it is an array that you want to delete, not an individual object. You do this by using delete [] instead of delete:
delete [] A->Iarray;

Sorry to repeat some of the corrections already given, but I thought it won't hurt to put everything in one place. Wink | ;)
GeneralRe: Corruption of the Heap Error Pin
Niklas L15-Feb-11 10:54
Niklas L15-Feb-11 10:54 
GeneralRe: Corruption of the Heap Error Pin
Stefan_Lang15-Feb-11 22:10
Stefan_Lang15-Feb-11 22:10 
GeneralRe: Corruption of the Heap Error [modified] Pin
Niklas L15-Feb-11 22:37
Niklas L15-Feb-11 22:37 
GeneralRe: Corruption of the Heap Error Pin
Stefan_Lang15-Feb-11 22:51
Stefan_Lang15-Feb-11 22:51 
GeneralRe: Corruption of the Heap Error [modified] Pin
Niklas L15-Feb-11 23:13
Niklas L15-Feb-11 23:13 
GeneralRe: Corruption of the Heap Error Pin
Niklas L16-Feb-11 6:57
Niklas L16-Feb-11 6:57 
GeneralRe: Corruption of the Heap Error Pin
Stefan_Lang16-Feb-11 22:22
Stefan_Lang16-Feb-11 22:22 
GeneralRe: Corruption of the Heap Error Pin
Niklas L16-Feb-11 23:12
Niklas L16-Feb-11 23:12 
GeneralRe: Corruption of the Heap Error Pin
Stefan_Lang17-Feb-11 1:10
Stefan_Lang17-Feb-11 1:10 
GeneralRe: Corruption of the Heap Error Pin
Niklas L17-Feb-11 1:34
Niklas L17-Feb-11 1:34 
GeneralRe: Corruption of the Heap Error Pin
Stefan_Lang17-Feb-11 1:42
Stefan_Lang17-Feb-11 1:42 
GeneralRe: Corruption of the Heap Error Pin
Foothill15-Feb-11 11:16
professionalFoothill15-Feb-11 11:16 
QuestionHow to use CMFCVisualManagerOffice2007 in dialog App? Pin
stephen_young14-Feb-11 3:24
stephen_young14-Feb-11 3:24 
AnswerRe: How to use CMFCVisualManagerOffice2007 in dialog App? Pin
David Crow14-Feb-11 3:43
David Crow14-Feb-11 3:43 
GeneralRe: How to use CMFCVisualManagerOffice2007 in dialog App? Pin
stephen_young14-Feb-11 6:14
stephen_young14-Feb-11 6:14 
QuestionMulti Language Support Pin
T.RATHA KRISHNAN14-Feb-11 2:44
T.RATHA KRISHNAN14-Feb-11 2:44 
AnswerRe: Multi Language Support Pin
loyal ginger14-Feb-11 10:16
loyal ginger14-Feb-11 10:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.