CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 1999
    Posts
    2

    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!



  2. #2
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    418

    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

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