[GiNaC-list] hash tables and sets

Richard B. Kreckel kreckel at ginac.de
Tue Oct 10 23:25:43 CEST 2006


Dear Marko,

You wrote:

>I have another very basic question. Suppose I have an ordinary C
>structure (I will use C in my example, rather than Objective C, to
>keep it simple) containing some fields e.g.
>
>typedef struct {
>    int a, b;
>    char *name;
>} mystruct;
>
>Now I want to add a field to it (at compile time, of course),
>containing a set of expressions, using std::set<ex, ex_is_less>, as
>you suggested. How would I declare this field? What headers do I need
>to import?
>  
>

#include <ginac/ginac.h>

typedef struct {
int a, b;
char *name;
std::set<GiNaC::ex, GiNaC::ex_is_less> expressionset;
} mystruct;

>Furthermore I have two functions, "make_instance" and "free_instance."
>The function "make_instance" calls "malloc" to allocate the structure,
>as in
>
>inst = (mystruct *)malloc(sizeof(mystruct));
>inst->a = inst->b = 0;
>inst->name = NULL;
>
>What do I have to do to allocate and initialize a new set of
>expressions, e.g. what goes on the right side of
>
>inst->set = /* ??? */
>  
>

Nothing. As recommended in an earlier email, use the placement new 
istead: new(inst) mystruct. This way, the set ctor will have initialized 
expressionset properly. Note, that the compiler has automatically 
equipped mystruct with a set of default ctors.

>The function "free_instance" frees the name field, the set, and the
>structure itself, as in
>
>if(inst->name!=NULL){ free(inst->name); }
>/* free inst->set */
>free(inst);
>
>What do I need to put on the second line (the one that frees the set).
>  
>

Again, nothing. Since the object was allocated with placement new, you 
would have to explicitly call the dtor inst->~mystruct(). Again, the 
compiler has automatically equpped mystruct with a dtor.

-richy.

-- 
Richard B. Kreckel
<http://www.ginac.de/~kreckel/>



More information about the GiNaC-list mailing list