Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Assert unique values for a struct member during compile.

Posted on 2014-03-01
6
Medium Priority
?
229 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
[X]
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
  • 3
  • 3
6 Comments
 
LVL 46

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 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 46

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

610 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