How can I test if a value is assigned ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nd3r
    New Member
    • Oct 2006
    • 6

    How can I test if a value is assigned ?

    I have a problem in my code.
    Let's say we have a variable named X, and X must be tested if any value is assigned to it, and if not, then X is assigned.
    I need to tell you that X is in fact a structure and I thought that the easiest way to do the test is to insert another field:
    typedef struct X
    {
    field 1;
    field 2;
    int test; ->>the test variable, which is 0 if X.field_1 is not assigned and 1 if X.field_1 is assigned.
    }
    The only problem is that I think this would end up in using too much memory.
    So please tell me what should I do.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    This will use less memory

    Code:
    typedef struct X
    {
        int FieldInUse;
        union{
            field 1;
            field 2;
        } fields;
    }
    However it makes use of field 1 and field 2 mutually exclusive because the memory used for field 1 and field 2 is overlayed intot he same location because of the union.

    Comment

    • Starasoris
      New Member
      • Oct 2006
      • 11

      #3
      I think that your best bet is to initialise the fields when the struct is instantiated. if only positive values are valid, init it to -1. If any value is valid then you may just need another field.
      If you are thinking that you may need one test field for each field, you don't. You can use Bit Fields

      unsigned field1set : 1;
      unsigned field2set : 1;
      etc.
      It will probably be 4 bytes extra per structure unless you have more than 32 fields.

      If you had hundreds of structures in an array and you wanted to be really scabby with memory, you could set up another array of unsigned ints and store whether a field has been set in each bit. ie. if there were 4 fields and 500 structures, your array would be 250 bytes long. Half a byte per struct. But that sort of thing should only be done in hard core scab code like in games.

      hope this helped.

      Originally posted by nd3r
      I have a problem in my code.
      Let's say we have a variable named X, and X must be tested if any value is assigned to it, and if not, then X is assigned.
      I need to tell you that X is in fact a structure and I thought that the easiest way to do the test is to insert another field:
      typedef struct X
      {
      field 1;
      field 2;
      int test; ->>the test variable, which is 0 if X.field_1 is not assigned and 1 if X.field_1 is assigned.
      }
      The only problem is that I think this would end up in using too much memory.
      So please tell me what should I do.

      Comment

      • nd3r
        New Member
        • Oct 2006
        • 6

        #4
        Thanks a lot guys. I guess I have some points of start in continuing my work. If any of you knows another solution besides inserting a field in structure, e.g. using some special function or creating another test variable, then please tell me. As I said I need the most efficient way in this matter.
        Thank you again!

        Comment

        Working...