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

  • SEARCH
  • Home
  • 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 9024155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:56:11+00:00 2026-06-16T05:56:11+00:00

I’d like to calculate the amount of memory in bytes that an object uses.

  • 0

I’d like to calculate the amount of memory in bytes that an object uses. Given

struct A
{
    float data[16];
    int index;
};

struct B
{
    A a;
};

Is the following the correct way to do this?

template <class Type>
size_t footprint();

template <>
size_t footprint<A>()
{
    return sizeof(float) * 16 + sizeof(int);
}

template <>
size_t footprint<B>()
{
    return footprint<A>();
}

I’m not sure about footprint() since I’ve heard that compilers may add extra information just to store member variables, and I’m not sure about footprint() since it refers to a class object. Does that require some memory as well?

EDIT: Okay, say the situation changed such that we’re not using a static array but an actual pointer:

#include <iostream>

using namespace std;

struct A
{
    A(int size_)
    {
        data = new float[size_];
        size = size_;
    }

    ~A()
    {
        delete [] data;
    }

    float* data;
    int size;
};

struct B
{
    B() : a(16) {}

    A a;
};

size_t footprint(const A& a)
{
    return sizeof(float) * a.size + sizeof(int);
}

size_t footprint(const B& b)
{
    return footprint(b.a);
}

int main()
{
    A a(16);
    B b;

    cout << "sizeof(A) = " << sizeof(A) << endl;
    cout << "sizeof(B) = " << sizeof(B) << endl;
    cout << "footprint(a) = " << footprint(a) << endl;
    cout << "footprint(b) = " << footprint(b) << endl;
}

Here you would need to actually have a special sizeof function (here called footprint) right?

  • 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-16T05:56:12+00:00Added an answer on June 16, 2026 at 5:56 am

    The correct way to do this is with sizeof(A) or sizeof(B). Adding up the sizes of the members is not correct because the compiler is free to put in extra space, called padding, to properly align things. sizeof accounts for this padding.

    You also expressed concerns over your array decaying into a pointer. This is not the case with sizeof. sizeof(an array) will give the total number of bytes that array takes up, provided it is still in array form and hasn’t decayed into a pointer. The fact of the matter is that the standard explicitly disallows this decay to happen in sizeof, so you’re safe there:

    C++11 N3485 § 5.3.3/4 [emphasis mine]:

    The lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are not
    applied to the operand of sizeof.

    sizeof(A) == 16 * sizeof(float) + sizeof(int) + sizeof(additional padding)

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

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.