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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:31:23+00:00 2026-05-22T18:31:23+00:00

I have his code: int setAttrib(const string& name, int components){ // here I don’t

  • 0

I have his code:

int setAttrib(const string& name, int components){    
    // here I don't even touch 'name'
    if(components == 2) return 3;
    else return 1;
}

And I call the function this way:

setAttrib("position", 3);

I’m profiling memory with xcode profiler and in the function call std::string is making an allocation.
Why is that?

EDIT:

What’s the best way to avoid that allocation? since I’m calling that function a lot, and in about 10 seconds of time I end up allocating about 10MB in that line.

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-22T18:31:24+00:00Added an answer on May 22, 2026 at 6:31 pm

    You ask for a const string&, but pass in a const char*. The compiler thus needs to create a temporary object of the correct type.

    The fact that "position" is not an std::string but a char const* is more of a historical accident (inherited from C, when there was no string class in C++) than a design decision, but something to keep in mind nevertheless.

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

Sidebar

Related Questions

I have a colleague who insists that his code doesn't need comments, it's "self
I have a friend who has code like this in his wordpress files: Blockquote
I have a list of java object that have 4 members. int id; String
I want to do the following: The user should have his own sub site
I have a friend who is finishing up his masters degree in aerospace engineering.
I have a business user who tried his hand at writing his own SQL
I have an internal customer that only receives his email report from MS Report
I have one user who gets an error message when he closes his browser.
I have an ASP.NET home page where user provides his login ID. Depending on
I have an application where for each object the user can specify his own

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.