AceCommon  1.4.5
Arduino library for low-level common functions and features with no external dependencies
Public Member Functions | List of all members
ace_common::PrintStrN Class Reference

An alternate implementation of PrintStr that allocates the character array in the heap, instead of the stack. More...

#include <PrintStr.h>

Inheritance diagram for ace_common::PrintStrN:
Inheritance graph
[legend]
Collaboration diagram for ace_common::PrintStrN:
Collaboration graph
[legend]

Public Member Functions

 PrintStrN (uint16_t size)
 Create an instance with an internal buffer of size on the heap.
 
 ~PrintStrN ()
 Delete the internal buffer on the heap. More...
 
- Public Member Functions inherited from ace_common::PrintStrBase
size_t write (uint8_t c) override
 Write a single character into the internal buffer.
 
size_t write (const uint8_t *buf, size_t size) override
 Write the buf string of size into the internal buffer.
 
void flush () override
 Clear the internal buffer.
 
const char * cstr () const
 Return the NUL terminated c-string buffer. More...
 
const char * getCstr () const
 Backwards compatible version of cstr(). More...
 
size_t length () const
 Return the length of the internal c-string buffer.
 

Additional Inherited Members

- Protected Member Functions inherited from ace_common::PrintStrBase
 PrintStrBase (uint16_t size, char *buf)
 Constructor. More...
 
- Protected Attributes inherited from ace_common::PrintStrBase
char *const buf_
 This is the pointer to the character array buffer. More...
 

Detailed Description

An alternate implementation of PrintStr that allocates the character array in the heap, instead of the stack.

This allows the creation of PrintStr which are much larger than the available stack size. For an ESP8266 for example, the maximum stack size is about 4kB, even though there is 80kB of static RAM available.

The name PrintStrN was the best I could think of that was relatively short, similar to PrintStr, and conveyed the idea that the memory is allocated on the heap, which allows the size parameter to be a runtime value, instead of a compile-time value.

If this object is created dynamically at the beginning of a function, the character array buffer will be created on the heap. At the end of the function, the object will be automatically destroyed, and destruuctor will automatically reclaim the character array on the heap. If no other heap allocation is performed, then this object should cause no heap fragmentation, just like the PrintStr object which uses only the stack.

Definition at line 257 of file PrintStr.h.

Constructor & Destructor Documentation

◆ ~PrintStrN()

ace_common::PrintStrN::~PrintStrN ( )
inline

Delete the internal buffer on the heap.

This object itself should never be created on the heap, so we don't need to make the destructor virtual.

Definition at line 267 of file PrintStr.h.


The documentation for this class was generated from the following file: