[YLUG] Pointer help correction

Roger Leigh rleigh at whinlatter.ukfsn.org
Mon Jan 14 00:30:57 GMT 2008


"Dominic Hibbs" <dominic.hibbs at gmail.com> writes:

>     #define BIGINT 1
>     #define DOUBLE 2
>     #define STRING 4
>     #define ARRAY  8
>     #define INITIALISED 16

Why not use an enum?  You gain type-safety and the intent is clearer.

>     struct sdescript
>     {
>       char *   sptr;    // pointer to string
>       int salloc;    // allocated space for string
>       int slength;    // actual length of string - always <= salloc
>     };

Why do you need to handle length and allocation of strings?  In C++,
you can simply use the string class from the Standard Library, which
already has these features:

#include <string>

std::string foo;

Do you need to invent another string type?

>     union varval {
>       int *bi;        // BIGINT
>       double *df;        // DOUBLE
>       sdescript *sd;    // STRING
>     };
>
>     struct variable {    //
>       union varval  value;    //
>         char * varname;
>         int varlength;
>         char bitflags;
>     };
>
>
> and 
>
>  variable v;
>   *v.value.bi = 10;
>   v.varname = "@%";
>   v.bitflags = BIGINT;
>   *MOC = v;

This code is not correct.  No storage was allocated for v.value.bi, or
v.varname.

If your goal (again, we have no clue what this is) is to have a
general list of variables of different types, there are cleaner ways
to do this.  An example of how you might do this (no code--just
general structures).

#include <string>
#include <map>

# Generic value
class value_base
{
};

template <typename T>
class value
{
  typedef T value_type;

  value_type value;
}

template <typename K = std::string, typename V = value_base*>
class variable_map
{
  typedef K key_type;
  typedef V value_type;

  std::map<key_type, value_type> vmap;
}

This creates a key-value mapping between variable names and values.
Values are generic.  You can have value<int>, value <double>,
value<std::string> and indeed any value you choose.  You could get and
set any variable you wanted, and use typeid() or dynamic_cast<> to ask
what it's type is.

There's no need for bitflags: the type of the value is held within the
value itself--you just need to ask.  This removes all the dangers of
unions in one go.

It also doesn't need error-prone handling of memory allocation.  You
can use std::tr1::shared_ptr to handle it automatically.

There's now also no list structure.  It's a map to give you a fast
O(log(n)) search complexity--no linear scan of a list.

The above example is not very good--there are plenty of real examples
if you have a look around; Boost is a good place to start.


Regards,
Roger

-- 
  .''`.  Roger Leigh
 : :' :  Debian GNU/Linux             http://people.debian.org/~rleigh/
 `. `'   Printing on GNU/Linux?       http://gutenprint.sourceforge.net/
   `-    GPG Public Key: 0x25BFB848   Please GPG sign your mail.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 188 bytes
Desc: not available
Url : http://mailman.lug.org.uk/pipermail/york/attachments/20080114/c3b3b9e2/attachment-0001.bin


More information about the York mailing list