Cleaver macro (string concatenation?) or function to optimize size of array by turning it into a struct/linked list?
-
I am working with an ARM microcontroller and I'm programming in normal C programming language. I have a big array of struct, but a lot of the struct elements are unused and I would like to turn the array into an optimized struct/linked list instead. What would CLEAVER_MACRO_OR_PERHAPS_FUNCTION be in this case?
struct mySmallStruct1_s {
void* ptrToNextElement;
int myArray[1];
};struct mySmallStruct2_s {
void* ptrToNextElement;
int myArray[2];
};struct mySmallStruct3_s {
void* ptrToNextElement;
int myArray[3];
};struct myBigStruct_s {
struct mySmallStruct1_s mySmallStruct1;
struct mySmallStruct2_s mySmallStruct2;
struct mySmallStruct3_s mySmallStruct3;
};// This compiles fine, but I want something more reader-friendly
struct myBigStruct_s myBigStruct = {
{(void*)&myBigStruct.mySmallStruct2, {0}},
{(void*)&myBigStruct.mySmallStruct3, {0, 0}},
{(void*)0, {0, 0, 0}}
};// What should CLEAVER_MACRO_OR_PERHAPS_FUNCTION be to make this compile?
struct myBigStruct_s myBigStruct2 = {
{CLEAVER_MACRO_OR_PERHAPS_FUNCTION(2), {0}},
{CLEAVER_MACRO_OR_PERHAPS_FUNCTION(3), {0, 0}},
{NULL, {0, 0, 0}}
}; -
I am working with an ARM microcontroller and I'm programming in normal C programming language. I have a big array of struct, but a lot of the struct elements are unused and I would like to turn the array into an optimized struct/linked list instead. What would CLEAVER_MACRO_OR_PERHAPS_FUNCTION be in this case?
struct mySmallStruct1_s {
void* ptrToNextElement;
int myArray[1];
};struct mySmallStruct2_s {
void* ptrToNextElement;
int myArray[2];
};struct mySmallStruct3_s {
void* ptrToNextElement;
int myArray[3];
};struct myBigStruct_s {
struct mySmallStruct1_s mySmallStruct1;
struct mySmallStruct2_s mySmallStruct2;
struct mySmallStruct3_s mySmallStruct3;
};// This compiles fine, but I want something more reader-friendly
struct myBigStruct_s myBigStruct = {
{(void*)&myBigStruct.mySmallStruct2, {0}},
{(void*)&myBigStruct.mySmallStruct3, {0, 0}},
{(void*)0, {0, 0, 0}}
};// What should CLEAVER_MACRO_OR_PERHAPS_FUNCTION be to make this compile?
struct myBigStruct_s myBigStruct2 = {
{CLEAVER_MACRO_OR_PERHAPS_FUNCTION(2), {0}},
{CLEAVER_MACRO_OR_PERHAPS_FUNCTION(3), {0, 0}},
{NULL, {0, 0, 0}}
};The following compiles fine, but can someone think of something cleaner?
#define SIZEOF(__ARG__) ((__ARG__) == 1) ? (sizeof(struct mySmallStruct1_s)) : \
((__ARG__) == 2) ? (sizeof(struct mySmallStruct1_s) + sizeof(struct mySmallStruct2_s)) : \
(sizeof(struct mySmallStruct1_s) + sizeof(struct mySmallStruct2_s) + sizeof(struct mySmallStruct3_s))#define CLEAVER_MACRO(__ARG__) ((void*)((uint32_t)&(myBigStruct2) + SIZEOF((__ARG__))))
struct myBigStruct_s myBigStruct2 = {
{CLEAVER_MACRO(2), {0}},
{CLEAVER_MACRO(3), {0, 0}},
{NULL, {0, 0, 0}}
}; -
I am working with an ARM microcontroller and I'm programming in normal C programming language. I have a big array of struct, but a lot of the struct elements are unused and I would like to turn the array into an optimized struct/linked list instead. What would CLEAVER_MACRO_OR_PERHAPS_FUNCTION be in this case?
struct mySmallStruct1_s {
void* ptrToNextElement;
int myArray[1];
};struct mySmallStruct2_s {
void* ptrToNextElement;
int myArray[2];
};struct mySmallStruct3_s {
void* ptrToNextElement;
int myArray[3];
};struct myBigStruct_s {
struct mySmallStruct1_s mySmallStruct1;
struct mySmallStruct2_s mySmallStruct2;
struct mySmallStruct3_s mySmallStruct3;
};// This compiles fine, but I want something more reader-friendly
struct myBigStruct_s myBigStruct = {
{(void*)&myBigStruct.mySmallStruct2, {0}},
{(void*)&myBigStruct.mySmallStruct3, {0, 0}},
{(void*)0, {0, 0, 0}}
};// What should CLEAVER_MACRO_OR_PERHAPS_FUNCTION be to make this compile?
struct myBigStruct_s myBigStruct2 = {
{CLEAVER_MACRO_OR_PERHAPS_FUNCTION(2), {0}},
{CLEAVER_MACRO_OR_PERHAPS_FUNCTION(3), {0, 0}},
{NULL, {0, 0, 0}}
};You are making hard work of it, you have a C11 compiler :-) Unionize the struct pointers .. they are all pointers to different types
typedef union {
void* void_ptr;
struct mySmallStruct1_s* SmallStruct1_ptr;
struct mySmallStruct2_s* SmallStruct2_ptr;
struct mySmallStruct3_s* SmallStruct3_ptr;
} smallstruct_ptr;struct mySmallStruct1_s {
smallstruct_ptr ptrToNextElement;
int myArray[1];
};struct mySmallStruct2_s {
smallstruct_ptr ptrToNextElement;
int myArray[2];
};struct mySmallStruct3_s {
smallstruct_ptr ptrToNextElement;
int myArray[3];
};struct myBigStruct_s {
struct mySmallStruct1_s mySmallStruct1;
struct mySmallStruct2_s mySmallStruct2;
struct mySmallStruct3_s mySmallStruct3;
};struct myBigStruct_s myBigStruct = {
{ .ptrToNextElement.SmallStruct2_ptr = &myBigStruct.mySmallStruct2, .myArray[0] = 0 },
{ .ptrToNextElement.SmallStruct3_ptr = &myBigStruct.mySmallStruct3, .myArray[0] = 0, .myArray[1] = 0 },
{ .ptrToNextElement.void_ptr = 0,.myArray[0] = 0,.myArray[1] = 0, .myArray[2] = 0 }
};struct myBigStruct_s myBigStruct1 = {
{ .ptrToNextElement.SmallStruct2_ptr = &myBigStruct1.mySmallStruct2,.myArray[0] = 1 },
{ .ptrToNextElement.SmallStruct3_ptr = &myBigStruct1.mySmallStruct3,.myArray[0] = 1,.myArray[1] = 2 },
{ .ptrToNextElement.void_ptr = 0, .myArray[0] = 1,.myArray[1] = 2,.myArray[2] = 3 }
};In vino veritas