Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch.embedded > #30431

Using C preproc to uniquely identify structure members

From John Speth <johnspeth@yahoo.com>
Newsgroups comp.arch.embedded
Subject Using C preproc to uniquely identify structure members
Date 2021-04-10 10:51 -0700
Organization Aioe.org NNTP Server
Message-ID <s4soil$1e7o$1@gioia.aioe.org> (permalink)

Show all headers | View raw


I could use some advice for a problem that I've had on my mind for 
years. I'm looking for a black box function that will examine a memory 
block of a known structure and output a list of IDs that point to any 
members that have changed. I have a strong feeling that this problem has 
been solved by many people before me but I've never seen any solution. 
I'm looking for an elegant solution that requires little to no source 
code changes when a structure member is changed, added, or removed. 
Please follow my problem description below to get an idea of what I'm 
looking for.

I have a largish C structure containing data members of various types 
and sizes. The structure definition will probably have to change over 
the course of project development. Data packaged in the structure will 
arrive at some processing function. The processing function will always 
keep a copy of the previously processed structure so that a compare 
operation can be used to identify any structure members that have 
changed. There is such a large number of members that my quest is to 
automate as much as possible the ID and comparison operations. Not 
automating would require the programmer to redo the comparison which 
would be a giant if/elseif/else statement with customized comparison for 
each structure member. That would invite mistakes if not done accurately.

Below is what I've hacked out so far using the C preprocessor. It 
quickly points to various problems that makes the problem seem like too 
much work for the preprocessor (like, for example, it won't work for 
c[3]). Maybe a code generation step using perl or python would work 
better. I thought I'd check with the experts before working on it further.

#define	MEMBER(t,v) t v; size_t size_ ## v; size_t id_ ## v

typedef struct
{
   MEMBER(int,i);
   MEMBER(char,c);
   MEMBER(float,f);
} STRUCT;

STRUCT s;

s.size_i = sizeof(s.i); s.id_i = offsetof(STRUCT,i);
s.size_c = sizeof(s.c); s.id_c = offsetof(STRUCT,c);
s.size_f = sizeof(s.f); s.id_f = offsetof(STRUCT,f);

printf("I: Size = %d, ID = %d\n",s.size_i,s.id_i);
printf("C: Size = %d, ID = %d\n",s.size_c,s.id_c);
printf("F: Size = %d, ID = %d\n",s.size_f,s.id_f);

Thanks - JJS

Back to comp.arch.embedded | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Using C preproc to uniquely identify structure members John Speth <johnspeth@yahoo.com> - 2021-04-10 10:51 -0700
  Re: Using C preproc to uniquely identify structure members Don Y <blockedofcourse@foo.invalid> - 2021-04-10 13:19 -0700
  Re: Using C preproc to uniquely identify structure members Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2021-04-10 23:16 +0200
  Re: Using C preproc to uniquely identify structure members George Neuner <gneuner2@comcast.net> - 2021-04-10 18:12 -0400
    Re: Using C preproc to uniquely identify structure members Don Y <blockedofcourse@foo.invalid> - 2021-04-10 18:20 -0700
      Re: Using C preproc to uniquely identify structure members George Neuner <gneuner2@comcast.net> - 2021-04-12 13:06 -0400
        Re: Using C preproc to uniquely identify structure members Don Y <blockedofcourse@foo.invalid> - 2021-04-12 11:13 -0700
          Re: Using C preproc to uniquely identify structure members George Neuner <gneuner2@comcast.net> - 2021-04-12 21:51 -0400
            Re: Using C preproc to uniquely identify structure members Don Y <blockedofcourse@foo.invalid> - 2021-04-12 20:49 -0700
          Re: Using C preproc to uniquely identify structure members David Brown <david.brown@hesbynett.no> - 2021-04-13 13:07 +0200
  Re: Using C preproc to uniquely identify structure members John Speth <johnspeth@yahoo.com> - 2021-04-11 07:42 -0700
    Re: Using C preproc to uniquely identify structure members Don Y <blockedofcourse@foo.invalid> - 2021-04-11 12:56 -0700

csiph-web