Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Assert unique values for a struct member during compile.

Posted on 2014-03-01
6
221 Views
Last Modified: 2014-03-03
I have an embedded application where I need to guarantee that a structure member has a unique value in a table of those structures. I need to ensure that the startingPosition is always unique.

const dataLogDefinition dataLogDefinitions PPAGE_E5 = {
		version:  PROTOCOL_VERSION,
		sizeOfEntry:  sizeof(dataLogField),
		entries: {
				 {.startingPosition = offsetof(CoreVar, IAT), .size = bitSize(CoreVar, IAT), .name = "IAT", .description = "Variable 1", 0,0},
				 {.startingPosition = offsetof(CoreVar, CHT), .size = bitSize(CoreVar, CHT), .name = "CHT", .description = "Variable 2", 0,0},
		}
};

Open in new window


Any ideas would be appreciated.
0
Comment
Question by:sean-keys
  • 3
  • 3
6 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 39897547
Hi Sean,

Is the table static?  How many items are in the table?  Is uniqueness enough or do they also need to be sequential, non-descending, random, etc.
0
 

Author Comment

by:sean-keys
ID: 39897563
Hi,

Yes the table is static. The length will be variable but within reason 20-30 entries. They do not need to be sequential, I just need to make sure something does not end up in there twice.
0
 

Author Comment

by:sean-keys
ID: 39897577
https://git.libreems.org/libreems-suite/libreems-firmware/blob/dataLogDefProtocol/src/main/dataLogDefinitions.c

needs to describe the data here:

https://git.libreems.org/libreems-suite/libreems-firmware/blob/dataLogDefProtocol/src/main/inc/containerTypes.h#L72


The definitions need to remain seperate because they reside in flash while the other resides in RAM, which I have much less of.

Thanks!
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 39897697
Easy enough.  :)

Note the two sample lines:

                         {.startingPosition = offsetof(CoreVar, IAT), .size = bitSize(CoreVar, IAT), .name = "IAT", .description = "Variable 1", 0,0},
                         {.startingPosition = offsetof(CoreVar, CHT), .size = bitSize(CoreVar, CHT), .name = "CHT", .description = "Variable 2", 0,0},

The first line contains the string "IAT" and the constant IAT.  IAT is defined in the header file.
The second line has the same characteristics, except that the values are "CHT" and CHT.

I suggest that you simply enumerate the items that you need.


enum
{
  SP_NULL=0,      // zero
  SP_IAT,
  SP_CHT,
  SP_TPS,
  SP_EGO,
  SP_MAP,
  SP_AAP,
  SP_BRV,
  SP_MAT,
  SP_MAX
};
0
 

Author Closing Comment

by:sean-keys
ID: 39900630
I guess the "right" way to do this would be to have a script generate both C source files. It seems enum, is as good as it gets :)
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 39900646
Well, for the specific application that you've shown, I don't think that a random/unique value is sufficient.  I believe that the value is also an index into the array of values returned from the PIC controller.  

You'll want to initialize the value to the correct index value.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question