Search code examples
cpointersstructdereference

Avoid dereferencing when working on same dataset with different structures


since now I've been reading Stackoverflow for a long time and I've learned a lot.

But now I have a problem, I couldn't find on Stackoverflow, even it should be kind of a "standard" question. So please forgive me if this topic has been answered already.

Problem:

I'am writing a module with defined interfaces for input and output structures. It should be some kind of a "multiplexer" with maybe three inputs and one output. The module should switch one of the inputs to the output (depending on some logic).

A working example is shown here:

#include <stdio.h>

typedef struct{
 short myVariable1;
 short myVariable2;
} myType;

struct input_type{
   myType Inp1;
   myType Inp2;
   myType Inp3;
};

struct output_type{
   myType Out1;
};

struct input_type input;
struct output_type output;

void main(){
   
   for (int i=0; i<10; i++){ // this for loop simulates a cyclic call of a function where all the inputs are written
       input.Inp1.myVariable1 = i;
       input.Inp2.myVariable1 = i*2;
       input.Inp3.myVariable1 = i*3;
       printf("Inp1: %d | Inp2: %d | Inp3: %d \n",input.Inp1.myVariable1,input.Inp2.myVariable1,input.Inp3.myVariable1);
       output.Out1 = input.Inp2;  // Actual routing is done here, but i want to avoid this copy by working on the same dataset (e.g. input.InpX)
       printf("Out: %d\n",output.Out1.myVariable1);
   }
}

In this snipped, the structures are simply copied every cycle. To avoid this step, I could do the following:

#include <stdio.h>

typedef struct{
 short myVariable1;
 short myVariable2;
} myType;

struct input_type{
   myType Inp1;
   myType Inp2;
   myType Inp3;
};

struct output_type{
   myType * Out1;
};

struct input_type input;
struct output_type output;

void main(){
   
   output.Out1 = &input.Inp2; // Actual routing is done here; But in this case, the output structure includes a pointer, therefore all other modules need to dereference Out1 with "->" or "*"
   
   for (int i=0; i<10; i++){ // this for loop simulates a cyclic call of a function where all the inputs are written
       input.Inp1.myVariable1 = i;
       input.Inp2.myVariable1 = i*2;
       input.Inp3.myVariable1 = i*3;
       printf("Inp1: %d | Inp2: %d | Inp3: %d \n",input.Inp1.myVariable1,input.Inp2.myVariable1,input.Inp3.myVariable1);
       
       printf("Out: %d\n",output.Out1->myVariable1);
   }
}

But in this case, the output structure is not compatible to the existing interface anymore. Access to Out1 would need dereferencing.

Is it possible to avoid copying the structures from one to another without changing my interface?

Thanks in advance for your answers! Rees.


Solution

  • Is it possible to avoid copying the structures from one to another without changing my interface?

    By "the existing interface", I take you to mean that you have code that consumes objects of this type ...

    struct output_type{
       myType Out1;
    };
    

    ... and you would like to avoid modifying that code.

    In that case, no, you cannot substitute

    struct output_type{
       myType * Out1;
    };
    

    . Moreover, it is inherent in the design of the former structure that populating the myType that is a direct member involves copying all the data you care about, whether on a per-member basis or on a whole-structure basis.

    At this point, I would recommend that you just stick with making those copies, until and unless you discover that doing so causes performance or memory usage to be unsatisfactory. Changing at this point would involve more than just syntactic changes: it would require careful review of all uses of struct output_type to find and mitigate any situations where the code relies on the properties of the original structure (such as to be confident of non-aliasing).