Can string use a smart pointer?

Hi,

Is it correct to say that when a copy of the String object is made, another copy of the string is allocated? Instead can it use a smart pointer to increment the reference to the char* pointer? In many instances I have a readonly String object on the stack and I want to store it in a global variable for later use, I don’t want to make a copy of the char array, only a copy the String object. How do I achieve that currently?

Thanks!

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with the Arduino project itself

I thought String is part of the Arduino project and the question is about the internal implementation, I wanted Arduino developers to respond, so maybe move it back please?

Take a look in WString.h / WString.cpp and all will be revealed to you. Such is the beauty of the open-source paradigm.

Do you know the answer?

So I don't see any smart pointers. That's why I wanted to ask what the Arduino developers think about making strings immutable. @UKHeliBob did you think my question is about how to program using String class? It's not.

The STL (and hence std::unique_ptr, etc) is not supported on AVR-based boards which make up a large fraction of the Arduino ecosystem.

One answer to your need is to use a cString instead of a String instance…

Implement a custom one.

I wanted to use standard Arduino API, otherwise of course it's not an issue

For the convenience of interested parties, I'll share links to the canonical implementation of the Arduino core String class:

About the "WString" vs "String" filename difference, there is now a deprecated wrapper header that provides backwards compatibility:

1 Like

Good idea. Please post the GitHub link to the code when you've done so.

Taskfun/BPtr.h at main · glutio/Taskfun · GitHub

/*
  BPtr - a smart pointer implementation
*/
template<typename T>
class BPtr {
private:
  T* ptr;
  int* refCount;

public:
  // Constructor
  explicit BPtr(T* p = nullptr)
    : ptr(p), refCount(new int(1)) {
  }

  // Copy constructor
  BPtr(const BPtr& other)
    : ptr(other.ptr), refCount(other.refCount) {
    (*refCount)++;
  }

  // Assignment operator
  BPtr& operator=(const BPtr& other) {
    if (this != &other) {
      // Decrease the reference count of the current object
      (*refCount)--;

      // If the reference count becomes zero, delete the object and refCount
      if (*refCount == 0) {
        delete ptr;
        delete refCount;
      }

      // Assign the new object and increase its reference count
      ptr = other.ptr;
      refCount = other.refCount;
      (*refCount)++;
    }
    return *this;
  }

  // Destructor
  ~BPtr() {
    (*refCount)--;

    if (*refCount == 0) {
      delete ptr;
      delete refCount;
    }
  }

  // Dereference operator
  T& operator*() const {
    return *ptr;
  }

  // Arrow operator
  T* operator->() const {
    return ptr;
  }

  // Comparison operator
  bool operator ==(const BPtr& other) const {
    return ptr == other.ptr;
  }

  // Conversion operator to bool
  explicit operator bool() const {
    return ptr != nullptr;
  }
};

c-Strings are listed in the Arduino’s doc as one way to deal with text strings if you want that excuse :wink:

string

[Data Types]

Description

Text strings can be represented in two ways. you can use the String data type, which is part of the core as of version 0019, or you can make a string out of an array of type char and null-terminate it. This page described the latter method. For more details on the String object, which gives you more functionality at the cost of more memory, see the String object page.

Maybe you find it interesting to look into the SafeString-library which offers referencing from chararrays and pointers to char-arrays.

Not so much in using it but maybe how the library is written
https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/docs/html/index.html

best regards Stefan

That library is really written just like the String class with a c-string backend but without the possibility of dynamically increasing the buffer size dynamically.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.