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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:55:28+00:00 2026-05-31T23:55:28+00:00

Given the following code: int i; … ostingstream os; os<<i; string s=os.str(); I want

  • 0

Given the following code:

int i;
...
ostingstream os;
os<<i;
string s=os.str();

I want to count the number of times of dynamic memory allocation when using ostringstream this way. How can I do that? Maybe through operator new?

Thank you.

  • 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-31T23:55:29+00:00Added an answer on May 31, 2026 at 11:55 pm

    Yes, and here is how you could do it:

    #include <new>
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <sstream>
    
    int number_of_allocs = 0;
    
    void* operator new(std::size_t size) throw(std::bad_alloc) {
      ++number_of_allocs;
      void *p = malloc(size);
      if(!p) throw std::bad_alloc();
      return p;
    }
    
    void* operator new  [](std::size_t size) throw(std::bad_alloc) {
      ++number_of_allocs;
      void *p = malloc(size);
      if(!p) throw std::bad_alloc();
      return p;
    }
    
    void* operator new  [](std::size_t size, const std::nothrow_t&) throw() {
      ++number_of_allocs;
      return malloc(size);
    }
    void* operator new   (std::size_t size, const std::nothrow_t&) throw() {
      ++number_of_allocs;
      return malloc(size);
    }
    
    
    void operator delete(void* ptr) throw() { free(ptr); }
    void operator delete (void* ptr, const std::nothrow_t&) throw() { free(ptr); }
    void operator delete[](void* ptr) throw() { free(ptr); }
    void operator delete[](void* ptr, const std::nothrow_t&) throw() { free(ptr); }
    
    int main () {
      int start(number_of_allocs);
    
      // Your test code goes here:
      int i(7);
      std::ostringstream os;
      os<<i;
      std::string s=os.str();
      // End of your test code
    
      int end(number_of_allocs);
    
      std::cout << "Number of Allocs: " << end-start << "\n";
    }
    

    In my environment (Ubuntu 10.4.3, g++), the answer is “2”.


    EDIT: Quoting MSDN

    The global operator new function is called when the new operator is used to allocate objects of built-in types, objects of class type that do not contain user-defined operator new functions, and arrays of any type. When the new operator is used to allocate objects of a class type where an operator new is defined, that class’s operator new is called.

    So every new-expression will invoke the global operator new, unless there is a class operator new. For the classes you listed, I believe that there is no class-level operator new.

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

Sidebar

Related Questions

So, given then following code type MyClass () = let items = Dictionary<string,int>() do
Given the following code: static member private getIntValue (_map:Map<string, int>) (_key:string) = if (_map.ContainsKey
Given the following code: int *a = NULL; a = calloc(1, sizeof(*a)); printf(%d\n, a);
In c#, Given the following code: public class Person { public int PersonID {
Given the following C# code: var product = new List<int>(); for (int n1 =
Given the following code: using (var client = new WebClient()) { string url =
given the following code: /* signatures */ int getParams(char params[MAX_PARAM_LEN][MAX_LINE_LEN]); int getVersion(const char params[MAX_PARAM_LEN][MAX_LINE_LEN],
Given the following code snippets, is there any appreciable difference? public boolean foo(int input)
Given the following code: class Screen; class WindowMgr { WindowMgr& relocateScreen( int r, int
Given the following code : public class Game { private Map<String,Coordinate> m_myHashMap; ... //

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.