Thread: Structs/Unions/pointers/arrays: Help me clean this up a bit!

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Structs/Unions/pointers/arrays: Help me clean this up a bit!

    Hello, this is my first post on these forums but I have been a lurker over the years as a student and professional programmer...

    Anyway, here is my dilemma, thanks in advance for taking the time to help:

    I want to have a static length list of pointers to different data types (there are three at the moment with plans for expansion in the future). But I also need to retain the size of the data at the pointer location because I will have to read and write to it. As far as I understand, this means that (void *) is out unless I do something like below.

    The purpose of this code is to generate a list of pointers to variables that range from 16 to 64 bits and will be read from/written to via a serial port 16-bits at a time. I need to retain the size of the data so that I know when to stop reading and writing.

    Here is my code snippet currently (I want it to be better!):

    Code:
    typedef struct{    void * VarLoc;
        int    VarSize;
    }DebugStruct;
    
    
    DebugStruct DebugVars[] = {
        {(void *)&varA,sizeof(varA)},
         {(void *)&varB,sizeof(varB)},
         {(void *)&sweep_run,sizeof(sweep_run)}
    };
    I want to be able to easily add another entry, re-compile and download to my DSP.

    Here are two general guidelines:
    1. Memory conscious (This code will be going on a <1USD part, memory space is limited)
    2. It has to be as easy to modify as possible. I want to make a list of variables that will be commonly needed via the serial port during implementation, but I will probably forget some and need to make some last minute changes in the lab. I can not fumble around with clumsy implementations (like above :P).
    An ideal solution would be to use only a pointer and determine the length of the data when needed.

    Finally, I tried using a UNION of the different pointer types but my compiler initializes the value to the type listed first in the UNION declaration. I am not smart enough to circumvent that and simultaneously comply with the guidelines above.

    I do not need production level code in a reply, a quick snippet, suggestions, criticism or a link to some resources would be fine.

    Thanks for your time.
    Last edited by wiznillyp; 11-02-2011 at 10:01 PM. Reason: Confusing

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The solution you have is probably as good as you'll get, apart from the less-than-attractive list. Depending on the chip you use and it's memory alignment restrictions, you may be able to save space by making size a char, if you know size will only ever be 16-64 bytes. But I wouldn't worry too much about size until you know it's an issue, i.e. until you get the chip, compile the code for it and realize it's too big to fit.

    To make the list a little easier on the eyes, you can drop the cast to (void *) since a void * can accept any pointer type. You could use a macro to expand the variable name into the two parts of the struct. Macros can make it more difficult to debug, since the compiler errors don't jive with the source code you're seeing (the macro gets replaced by the preprocessor before compilation):
    Code:
    #define STRUCT_ENTRY(var)     {&var, sizeof(var)}
    
    typedef struct{
        void * VarLoc;
        int    VarSize;
    }DebugStruct;
     
    DebugStruct DebugVars[] = {
        DEBUG_STRUCT(varA),
        DEBUG_STRUCT(varB),
        DEBUG_STRUCT(sweep_run)
    };
    The above code is untested, so you may need to tweak it a bit.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Thank you. The MACRO idea totally escaped me! I think that should be easy enough, since only programmers will be able to change it, it will really minimize the fat finger error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with arrays, pointers, and structs.
    By RexInTheCity in forum C Programming
    Replies: 5
    Last Post: 03-29-2010, 03:30 PM
  2. Arrays, Structs, Pointers, and unexpected crashes
    By KairosDrasis in forum C Programming
    Replies: 2
    Last Post: 04-13-2009, 12:37 AM
  3. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM
  4. Passing pointers to two-dimensional arrays of structs
    By dr.neil.stewart in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 10:25 AM
  5. Replies: 7
    Last Post: 12-29-2001, 11:25 PM