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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:21:02+00:00 2026-05-30T05:21:02+00:00

I have a constructor for my Arduino-code which is something like the following: class

  • 0

I have a constructor for my Arduino-code which is something like the following:

class X {
  private:
    char* _name;
  public:
    X(char*);
}

X::X(char* name) {
  _name = name;
}

My question is: do I need to allocate a char buffer instead of just relying on the string pointer that was passed? I am not operating on the string (other than to shorten it). I don’t seem to be running into any issues yet, but I wanted to verify.

This is specific to arduino, but responses on C and C++ would be welcomed as well.

  • 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-30T05:21:03+00:00Added an answer on May 30, 2026 at 5:21 am

    It depends.

    Basically, with the way the code is now, you are requiring that name have a lifetime greater than the lifetime of the instance of the class. If name is always a string literal, i.e., X(“foo”), then this is acceptable. Otherwise, the caller would have to allocate the string, which is an odd and error prone contract to require.

    I would suggest duplicating the string always in the constructor, unless it is clear that it is always expected to be string literal.

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

Sidebar

Related Questions

I have a constructor like as follows: public Agent(){ this.name = John; this.id =
I have a constructor question for C#. I have this class: public partial class
I have a constructor and a property in the class: private IMyCollectionObjects _myCollectionObjects; public
Is it okay to have default constructor which sets some default values like: public
I have a constructor signature like this public NavigationLink(Func<String> getName, Func<UrlHelper, String> getURL, Func<bool>
I have a constructor which has a non-interface dependency: public MainWindowViewModel(IWorkItemProvider workItemProvider, WeekNavigatorViewModel weekNavigator)
I have a constructor like: public Blah(string a, string b) { } public Blah(string
I have multiple constructor in a Java class. public class A{ public A(){...} public
I have a constructor in a class that does some kind of logic: public
I have a constructor signature that looks like this. public LocateEditorViewModel( ILocateRepository locateRepository, int

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.