element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog C++ write your own stream class - part 1a: the cost of objects can be zero (embedded friendly C++)
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 12 Nov 2025 7:48 PM Date Created
  • Views 85 views
  • Likes 4 likes
  • Comments 3 comments
  • Modern C++
  • c++
Related
Recommended

C++ write your own stream class - part 1a: the cost of objects can be zero (embedded friendly C++)

Jan Cumps
Jan Cumps
12 Nov 2025

I wrote a little output stream class in a previous post:  C++ write your own stream class - part 1: output stream initial design (embedded friendly C++) .

In this post, I'm adding an input stream, and an input / output stream that's subclass based on the previous 2. Goal is to show that using C++, objects, and derived children, and using streaming, has exact the same cost as C code.

C++ design: stream data with objects

This is a C++ example that uses a class hierarchy, to (hopefully) show that classes, when used wisely, come for free.

image

#include <string>

class uartostream {
public:
  uartostream() {}

  uartostream& operator << (const char* msg) {
    // write to UART
    volatile char c = msg[0]; //this is the example code
    return *this;
  }
  uartostream& operator << (const std::string& msg) {
    // write to UART
    return *this;
  }
};

class uartistream {
public:
  uartistream() {}
  
  uartistream& operator >> (std::string& msg) {
    // read from UART
    return *this;
  }  
  uartistream& operator >> (char* msg) {
      // read from UART
    return *this;
  }
};

class uartiostream: public uartistream, public uartostream {
};


int main() {
  uartiostream u;
  u << "5" ;  
}

This is example code. The goal is to show how expensive it is to instantiate an uartiostream object, and to stream a char string to it.

I did a few things to make it possible to compare it to plain C code:

  • I used a char array, because that's a data type that also can be used in C. Sheep vs sheep.
  • in the method that gets called, I avoided that the optimiser removes the code, by setting a variable to volatile. In that case, the compiler doesn't optimise that variable away, and keeps the code lines where it is used in place (although it is alklowed to optimise those code lines.

C code for performance comparison

The simplest C equivalent is this:

int main() {
  volatile char c = "5"[0];  
}

I tried to make the smallest C code that performs the same function as the C++ code. Assign the first char of a char array (the same "5" as in the C++ code) to a volatile variable of type char.

Verdict

The GCC compiler generates exactly the same assembler for both cases - C single line and C++ with embedded aware inheritance. I tested it for ARM and RISCV:

#ARM 32 (same for the C and C++ program)
main:
        mov     r3, #53
        mov     r0, #0
        sub     sp, sp, #8
        strb    r3, [sp, #7]
        add     sp, sp, #8
        bx      lr
        
#RISC-V 32 (same for the C and C++ program)
main:
        addi    sp,sp,-16
        li      a5,53
        sb      a5,15(sp)
        li      a0,0
        addi    sp,sp,16
        jr      ra

This shows that you can use C++ and objects in embedded design. And that the cost of abstraction can be low (or in this case zero).

image image
  • Sign in to reply
Parents
  • DAB
    DAB 2 days ago

    Nice update Jan.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • DAB
    DAB 2 days ago

    Nice update Jan.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube