Query on memory allocation using new operator
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!
Re: Query on memory allocation using new operator
Hi,
method 2 is correct, method 1 will allocate size for 40 structures (mydata is 4 byte long).
Martin
Martin