Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Cleaver macro (string concatenation?) or function to optimize size of array by turning it into a struct/linked list?

Cleaver macro (string concatenation?) or function to optimize size of array by turning it into a struct/linked list?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshardwarequestioncode-review
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    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}}
    };

    A L 2 Replies Last reply
    0
    • A arnold_w

      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}}
      };

      A Offline
      A Offline
      arnold_w
      wrote on last edited by
      #2

      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}}
      };

      1 Reply Last reply
      0
      • A arnold_w

        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}}
        };

        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups