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

  • Home
  • SEARCH
  • 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 8399929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:26:09+00:00 2026-06-09T21:26:09+00:00

i’m developing a first C software for an embedded device. I would like write

  • 0

i’m developing a “first” C software for an embedded device.

I would like write code in a oo-style, so i started to use structs.

typedef struct sDrawable {
    Dimension size;
    Point location;
    struct sDrawable* parent;
    bool repaint;
    bool focused;
    paintFunc paint;
} Drawable;

typedef struct sPanel {
    Drawable drw;
    struct sDrawable* child;
    uint16_t bgColor;
} Panel;

I can’t and i don’t want use malloc, so i search the better way to instantiate my structs.
Is this acceptable?

Panel aPanel={0};

void Gui_newPanel(Panel* obj, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t bgColor) {
    // Drw is a macro to cast to (Drawable*)
    Drw(obj)->repaint = true;
    Drw(obj)->parent= NULL;
    Drw(obj)->paint = (paintFunc) &paintPanel;
    Drw(obj)->location=(Point){x, y};
    Drw(obj)->size=(Dimensione){w, h};
    obj->child = NULL;
    obj->bgColor = bgColor;
}

void Gui_init(){
    Gui_newPanel(&aPanel, 0, 0, 100, 100, RED);
}

UPDATE

In the first one, i have to initialize struct like Panel aPanel={0}; to pass its pointer to Gui_newPanel function, or i can only write Panel aPanel; ?
I have tested and both works, but maybe i’m only unlucky…

Or is this a better way?

Panel aPanel;

Panel Gui_newPanel(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t bgColor) {
    // Drw is a macro to cast to (Drawable*)
    Panel obj;
    Drw(&obj)->repaint = true;
    Drw(&obj)->parent= NULL;
    Drw(&obj)->paint = (paintFunc) &paintPanel;
    Drw(&obj)->location = (Point){x, y};
    Drw(&obj)->size = (Dimension){w, h};
    obj.child = NULL;
    obj.bgColor = bgColor;
    return obj;
}

void Gui_init(){
    aPanel=Gui_newPanel(0, 0, 100, 100, RED);
}

Which of these methods is preferable?

I am particularly interested in performance and memory usages.

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-06-09T21:26:10+00:00Added an answer on June 9, 2026 at 9:26 pm

    Returning structs by value can incur overhead of copying the struct members, although the compiler will often be smart enough to optimise this out. If you’re concerned as to the overhead, you should profile the relative performance of the two approaches.

    Also, check whether the target compiler for the device is actually able to support returning structs – some very old compilers don’t support returning or assignment of structs.

    If you have a reasonably modern compiler, you may be able to return a struct literal directly, which will be efficient:

    return (Panel) {
        .drw = {
            .size = {w, h},
            .location = {x, y},
            // ...
        },
        .child = NULL,
        .bgColor = bgColor
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported

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.