Click to See Complete Forum and Search --> : Query on memory allocation using new operator


K H Ang
April 9th, 1999, 03:01 AM
Hi! I would like to know what is your opinion on the following codes:


typedef struct
{
unsigned char data1;
unsigned char data2;
unsigned short data3;
} mydata;


void main(void)
{
mydata* pData;

// So if I want an array of 10 * mydata structure
// Method 1
pData = (mydata*) new mydata[10 * sizeof(mydata)];

// Method 2
pData = (mydata*) new mydata[10];
}

So basically what I want is an array of 10 mydata structure, which method is correct and is method 1 allocating more memory than required ???

Thank you in advanced!

Martin Speiser
April 9th, 1999, 03:28 AM
Hi,

method 2 is correct, method 1 will allocate size for 40 structures (mydata is 4 byte long).

Martin

Martin