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 6605217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:15:28+00:00 2026-05-25T19:15:28+00:00

I’d like a function to return the size in bytes of an object for

  • 0

I’d like a function to return the size in bytes of an object for fundamental types. I’d also like it to return the total size in bytes of an STL container. (I know this is not necessarily the size of the object in memory, and that’s okay).

To this end, I’ve coded a memorysize namespace with a bytes function such that memorysize::bytes(double x) = 8 (on most compilers).

I’ve specialized it to correctly handle std::vector<double> types, but I don’t want to code a different function for each class of the form std::vector<ANYTHING>, so how do I change the template to correctly handle this case?

Here’s the working code:

#include <iostream>
#include <vector>

// return the size of bytes of an object (sort of...)
namespace memorysize
{

  /// general object
  template <class T>
  size_t bytes(const T & object)
  {
    return sizeof(T);
  }

  /// specialization for a vector of doubles
  template <>
  size_t bytes<std::vector<double> >(const std::vector<double> & object)
  {
    return sizeof(std::vector<double>) + object.capacity() * bytes(object[0]);
  }

  /// specialization for a vector of anything???

}


int main(int argc, char ** argv)
{

  // make sure it works for general objects
  double x = 1.;
  std::cout << "double x\n";
  std::cout << "bytes(x) = " << memorysize::bytes(x) << "\n\n";

  int y = 1;
  std::cout << "int y\n";
  std::cout << "bytes(y) = " << memorysize::bytes(y) << "\n\n";

  // make sure it works for vectors of doubles
  std::vector<double> doubleVec(10, 1.);
  std::cout << "std::vector<double> doubleVec(10, 1.)\n";
  std::cout << "bytes(doubleVec) = " << memorysize::bytes(doubleVec) << "\n\n";

  // would like a new definition to make this work as expected
  std::vector<int> intVec(10, 1);
  std::cout << "std::vector<int> intVec(10, 1)\n";
  std::cout << "bytes(intVec) = " << memorysize::bytes(intVec) << "\n\n";

  return 0;
}

How do I change the template specification to allow for the more general std::vector<ANYTHING> case?

Thanks!

  • 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-05-25T19:15:28+00:00Added an answer on May 25, 2026 at 7:15 pm

    Modified your code accordingly:

    /// specialization for a vector of anything
    template < typename Anything >
    size_t bytes(const std::vector< Anything > & object)
    {
      return sizeof(std::vector< Anything >) + object.capacity() * bytes( object[0] );
    }
    

    Note that now you have a problem if invoking bytes with an empty vector.

    Edit: Scratch that. If I remember your previous question correctly, then if you get a vector of strings then you would like to take into account the size taken by each string. So instead you should do

    /// specialization for a vector of anything
    template < typename Anything >
    size_t bytes(const std::vector< Anything > & object)
    {
      size_t result = sizeof(std::vector< Anything >);
    
      foreach elem in object
           result += bytes( elem );
    
      result += ( object.capacity() - object.size() ) * sizeof( Anything ).
    
      return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.