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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:08:29+00:00 2026-05-31T21:08:29+00:00

I have to make a decision which will be very important in future development

  • 0

I have to make a decision which will be very important in future development of my application, so it must be perfect.

The actual question:

For easiness sake it is better to have additional 20 empty bytes, but that seems extremely inefficient. How far is my program affected when I pass a 50 bytes big struct to a function ? =P

And why I am asking:

I run 64bit. That means 8 bytes per variable.
Currently stuff_s is 3*8=24 bytes big.

  • Just add three/four more variables, but bloat the struct size and slow down function calling.
  • or to choose sometime pretty complicated but safe additional bytes (and RAM space).

I will be using these stuff_ss to contain data objects of any kind in a tree, they can handle an object or module(that is something specific, ignore modules). Data object size can be different… They might even be several Gigabytes to usual structs used. Now I don’t want every damn struct to carry around additional 50 bytes. If that could be only one byte, or not more than 8… =(

I have a struct:

//
// stuff reference (either module or object)
//
struct stuff_s
{
    char s; // stuff: 'm' || 'o'
    union {
        struct {
            mfunc_t *fs;
            ppackage (*knockf)(ppackage p);
        } m;
        struct {
            void *h;
            size_t s;
            // HERE should the additional variables go.
        } o;
    };
};
  • 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-31T21:08:31+00:00Added an answer on May 31, 2026 at 9:08 pm

    Use references.

    C++:

    void foo(stuff_s &what)
    {
      what.o.s = 4;
    }
    
    int main()
    {
      stuff_s SetToFour;
      foo(SetToFour);
    }
    

    C:

    void foo(stuff_s *what)
    {
      if (what == NULL) return;
      what->o.s = 4;
    }
    
    int main(int argc, char *argv[])
    {
      stuff_s SetToFour;
      foo(&SetToFour);
    
      return 0; /* Because it's main in a strict C compiler. Ignore this. */
    }
    

    As to the design of your program, if your data is really, really unknowable, I would recommend using a void * pointer and an object type tag, or derive a class from a base class.

    C++:

    class base {
      public:
        int a;
        virtual void do_whatever();
        virtual int get_type() = 0; // Grab our type ID
    };
    
    class derived : public base {
      public:
        int b;
        float c;
        char *d;
        virtual void do_whatever() { c = 4.0F; }
        virtual int get_type() { return 1; }
    };
    
    struct stuff_s {
    //    ...in o
      base *futs;
    }
    
    // Later on...
    stuff_s foo;
    switch (foo.o.futs->get_type()) {
      case 0: // Base class
        break;
      case 1: // Class type 'derived'
        derived *a = dynamic_cast<derived *>(foo.o.futs); // Dynamic cast lets us take a pointer to a base type and make it into a pointer to a derived type. Using our tag ID that we get from the virtual function, we can determine exactly which type to do.
        a->b = 4;
        break;
    }
    // This is roughly the same amount of code as the C version would use
    

    If you really want I’ll provide a C example but this is getting overly long

    EDIT: I saw below that it has to be C compatible. So I’ll expand to include the C equivalent of that last code and also include passing by reference. Keep in mind this is conceptual: it will compile but it isn’t very useful.

    C:

    /* This is a generic object. It has a type ID and a pointer, that is all that is needed. It can hold anything. */
    struct generic_holder {
      int type;
      void *ptr;
    };
    
    /* This is an example struct it may point to. */
    struct holder_one {
      int a;
      int b;
      float c;
      char *d;
      int change_this;
    };
    
    /* This is another example struct. */
    struct holder_two {
      char best[50];
      char worst[50];
      int change_this;
    };
    
    struct stuff_s {
    /*    ...in .o */
      generic_holder data;
    };
    
    /* Forward-declaration */
    void set_to_four(stuff_s *foo);
    
    /* Later on... */
    int main(int argc, char* argv[])
    {
      stuff_s foo;
      holder_two test_struct = { "C++", "Lisp", 0 };  // Best, worst. Haha.
    
      foo.o.data.ptr = (void *)&test_struct; /* This makes it into a "generic pointer" by casting it to void */
    
      foo.o.data.type = 2; /* = 2 because we are using holder_two */
    
      /* We're going to use our function to set data in a generic object, passing by reference. This would work equally well on something of type holder_one, and can be expanded for data types you haven't thought of. */
      set_to_four(&foo);
    
      return 0;
    }
    
    /* This function will take a generic object and set the 'change_this' variable to 4 */
    void set_to_four(stuff_s *foo)
    {
      holder_one *ptr1;
      holder_two *ptr2;
    
      if (foo == NULL) return;            /* Obsessively check for invalid pointers */
      assert(foo.o.data.ptr != NULL);     /* Some people prefer to do checks only in debug, for speed. This does the same thing but only if debug is on. And it stops the program when it gets there if there is an error. It evaluates to nothing if it's a release build. */
    
      switch(foo.o.data.type) {
        case 1:
          ptr1 = (holder_one *)foo.o.data.ptr;
          ptr1->change_this = 4;
          break;
        case 2:
          ptr2 = (holder_two *)foo.o.data.ptr;
          ptr2->change_this = 4;
          break;
        default:
          break;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to make a decision of which database server to use for my
I have been given the tasks of speccing a mobile application, which will need
I have make a messaging system in which user can send messages to each
I have to make a graphical user interface application using the language of my
I have to make a connection to an XMLRPC site from a web application,
I have to make a decision regarding generalization vs polymorphism. Well the scenario is
I have a decision to make and I'm kicking it to the stackoverflow community.
I have a question! I have built a cgi file which is doing the
I need to make a design decision of how to approach an app which
I have developed a .net 3.5 c# web application and want to make some

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.