Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8997491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:53:48+00:00 2026-06-15T23:53:48+00:00

I have such code #include <cstdlib> class Foo { int m_data; public : Foo()

  • 0

I have such code

#include <cstdlib>

class Foo
{
    int m_data;

public :    
    Foo() : m_data(0) { }

    /*~Foo()
    {
    }*/

    static void* operator new[](const size_t size)
    {
        return malloc(size);
    }

    static void operator delete[](void* data)
    {
        free(data); 
    }
};

int main()
{
    Foo* objects = new Foo[5];

    delete [] objects;
}

In this case I receive value of size in operator new overloading as 20 bytes as I wanted (sizeof(int) * 5). But if I uncomment the destructor I get size as 24 bytes. Yeah, I now that these extra bytes is used to store the size of allocated memory and equals to sizeof(size_t). I can’t understand why I get them only if I implement destructor explicitly. If I don’t do it, the compiler should do the exact same thing or I missing something?

I’ve tried that on MSVS 2010 and 2012. Compiled for Win32.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:53:49+00:00Added an answer on June 15, 2026 at 11:53 pm

    “Extra bytes” requested by new[] from operator new[] are not used to “store the size of allocated memory”, as you seem to believe. They are used to store the number of elements in the array, so that the delete[] will know how many destructors to call. In your example destructors are trivial. There’s no need to call them. So, there’s no need to allocate these extra bytes and store the element count.

    The “size of allocated memory” (i.e. the size of the block in bytes) is a completely different story. It is stored and retrieved independently by a lower-level allocator – the malloc/free in your example.

    In other words, in general case a memory block allocated by new[] has two sets of extra bytes in front of the actual data: the block size in bytes (introduced by malloc) and the element count (introduced by new[]). The second one is optional, as your example demonstrates. The first one is typically always present, as it is unconditionally allocated by malloc. I.e. your malloc call will physically allocate more than 20 bytes even if you request only 20. These extra bytes will be used by malloc to store the block size in bytes.

    The latter happens in your example as well. You simply don’t see it since it happens inside malloc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an example code: #include <quadmath.h> int main() { __float128 foo=123; cosq(foo); return
i have following code #include <iostream> #include <cstdlib> using namespace std; int main() {
There is such code: #include <iostream> int main(){ unsigned int* wsk2 = new unsigned
I have such code with 2 structures: #include <list> using namespace std; struct Connection;
I have a program that has this code : #include<stdio.h> main(){ int input; char
I have such situation in boost and C++: // Somewhere: typedef boost::signals2::signal<void (int KeyCode)>
I have such code, with downloaded from i-net class for db. I'm interested, how
Please have a look on this code: #include <unistd.h> #include <stdlib.h> #include <stdio.h> int
So we have code like: #include cpptk.h #include <stdio.h> using namespace Tk; void hello()
I have such code which works well: #include <algorithm> #include <iostream> char x[11]= ABCDEFGHIJ;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.