|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: May 2004
Messaggi: 14
|
[c] perdita di valori in una struttura
Ciao!!
Uitlizzo una struttura ("tre") formata da diversi campi: indice: lista frequenza degli indici: double Uso una funzione ricorsiva che mi restituisce il risultato in una variabile di tipo struttura "tre". Tale funzione legge da un'altra struttura dati ("tab")raggiungibile tramite puntatori i dati che mi servono. Se inizializzo a 0 il campo frequenza (di "tre") tutto funziona, ma se inizializzo e poi cambio il valore del campo frequenza (con il valore letto e aggiornato da tab) si perdono tutti i valori. Per allocare "tre" utilizzo la malloc tat = (TRE*)malloc(sizeof(TATRE) + (n-1) *sizeof(int) + n *sizeof(TRE*)); con n che riporta il numero di volte che devo effettuare la lettura dalla struttura tab (se devo leggere 4 dati da tab, la prima volta varrrà 4 poi 3 fino a 0)o in altri termini quanti dati devo aggiungere a indice. Qualcuno mi sa dare un consiglio che riesca a levarmi da questo pasticcio... ???? .... |
|
|
|
|
|
#2 | |
|
Senior Member
Iscritto dal: Jul 2000
Città: Amsterdam
Messaggi: 217
|
Re: [c] perdita di valori in una struttura
Quote:
Sorry metti piu' codice....con anche le definizioni delle strutture dati, sia TRE che TATRE che TAB..... |
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Feb 2004
Città: TREVISO
Messaggi: 902
|
non ho capito praticamente una mazza, però vediamo se qualcosa l'ho intuito.
la tua struttura "tre" penso sia una struttura ricorsiva (in quanto hai detto che un elemento è di tipo "lista"), quindi una cosa di questo tipo: Codice:
typedef struct TRE{
double frequenza_indici;
struct TRE* next;
} TRE;
Infatti, l'approccio per allocare un nodo della struttura "TRE" dovrebbe essere questo: Codice:
TRE* testa; TRE* coda; testa=(TRE*)malloc(sizeof(TRE)); coda=testa->next; /*quindi allocazione della coda....*/ se la struttura non è ricorsiva, allora non riesco a capire cos'è il tipo "lista" da te indicato per il campo "indice" della struttura....dovresti essere più chiaro, magari spiegando più dettagliatamente cosa fa la funzione ricorsiva che usi....
__________________
|
|
|
|
|
|
#4 |
|
Junior Member
Iscritto dal: May 2004
Messaggi: 14
|
Ho cercato di semplificare, ma a quanto pare con un pessimo risultato.
Il problema è che il codice è terribilmente complesso (è l'Apriori quello preso in rete) provo a spiegarmi meglio: io ho una serie di dati (tipo tract contenuto in taset) a cui accedo (prima ho detto lista ma non è il termine più appropriato) tramite puntatori ( (*tract)->item[posizione) e cambia da uno all'altro incrementando o decrementando tract (*--tract)... oppure utilizzando un indice tract[indice])) (viene passato alla funzione taset->tract) poi usa una variabile tat (che è un nodo di tipo tatree) recursivamente si costruisce questi nodi. In originale prendeva solo i valori presenti in tract. Per la mia tesi ho dovuto aggiungere nella struttura taset un campo che semplicemente (anche se il discorso totale è molto più complesso) riporta un numero double (quello che per semplicità ho chiamato frequenza e che nell’allegato invece sarà weight_tran). Il problema nasce dal momento in cui cerco di portare tale valore nel nodo: eseguendo l’assegnazione tat->weight_tran = com (variabile di comodo che memorizza il valore corretto della frequenza). L’esecuzione continua, ma i risultati sono completamente errati. Se io lascio tutto come prima, cioè anche con il calcolo di com, MA NON eseguo più l’assegnazione tat->weight_tran. Tutto funziona ed ottengo i risultati desiderati (ovviamente, a meno del peso!). In allegato metto le strutture dati utilizzate e il codice …. typedef struct { /* --- a transaction --- */ double weight_tran; /*peso della transazione*/ int cnt; /* number of items */ int items[1]; /* item identifier vector */ } TRACT; typedef struct { /* --- a transaction set --- */ ITEMSET *itemset; /* underlying item set */ int max; /* maximum number of items per t.a. */ int vsz; /* size of transaction vector */ int cnt; /* number of transactions */ int total; /* total number of items */ TRACT **tracts; /* transaction vector */ } TASET; typedef struct _tatree { /* --- a transaction tree (node) --- */ int cnt; /* number of transactions */ int max; /* size of largest transaction */ int size; /* node size (number of children) */ int items[1]; /* next items in rep. transactions */ double weight_tran; /*peso delle transazioni*/ } TATREE; TATREE* _create (TRACT **tracts, int cnt, int index) { /* --- recursive part of tat_create() */ int i, k, t; /* loop variables, buffer */ int item, n; /* item and item counter */ TATREE *tat; /* created transaction tree */ TATREE **vec; /* vector of child pointers */ double weight_com; assert(tracts /* check the function arguments */ && (cnt >= 0) && (index >= 0)); if (cnt <= 1) { /* if only one transaction left */ n = (cnt > 0) ? (*tracts)->cnt -index : 0; tat = (TATREE*)malloc(sizeof(TATREE) +(n-1) *sizeof(int)); if (!tat) return NULL; /* create a transaction tree node */ tat->cnt = cnt; /* and initialize its fields */ tat->size = -n; tat->max = n; (*tracts)->weight_tran= weight_com; while (--n >= 0) tat->items[n] = (*tracts)->items[index +n]; return tat; } for (k = cnt; (--k >= 0) && ((*tracts)->cnt <= index); ) tracts++; /* skip t.a. that are too short */ n = 0; item = -1; /* init. item and item counter */ for (tracts += i = ++k; --i >= 0; ) { t = (*--tracts)->items[index]; /* traverse the transactions */ if (t != item) { item = t; n++; weight_com =(*tracts)->weight_tran;} } /* count the different items */ #ifdef ARCH64 /* adapt to even item number */ i = (n & 1) ? n : (n+1); /* so that pointer addresses are */ #else /* multiples of 8 on 64 bit systems */ i = n; /* on 32 bit systems, however, */ #endif /* use the exact number of items */ tat = (TATREE*)malloc(sizeof(TATREE) + (i-1) *sizeof(int) + n *sizeof(TATREE*)); if (!tat) return NULL; /* create a transaction tree node */ tat->cnt = cnt; /* and initialize its fields */ tat->size = n; tat->max = 0; tat->weight_tran=0; //inizializzazione del peso della transazione if (n <= 0) return tat; /* if t.a. are fully captured, abort */ vec = (TATREE**)(tat->items +i); item = tracts[--k]->items[index]; weight_com = tracts[k]->weight_tran; for (tracts += i = k; --i >= 0; ) { t = (*--tracts)->items[index]; /* traverse the transactions, */ if (t == item) continue; /* but skip those with the same item */ tat->items[--n] = item; item = t; weight_com=(*tracts)->weight_tran; //tat->weight_tran=weight_com; vec[n] = _create(tracts+1, k-i, index+1); if (!vec[n]) break; /* note the item identifier */ t = vec[n]->max +1; if (t > tat->max) tat->max = t; k = i; /* recursively create subtrees */ } /* and adapt the section end index */ if (i < 0) { /* if child creation was successful */ tat->items[--n] = item; /* node the last item identifier */ // tat->weight_tran=weight_com; vec[n] = _create(tracts, k+1, index+1); if (vec[n]) { /* create the last child */ t = vec[n]->max +1; if (t > tat->max) tat->max = t; return tat; /* return the created */ } /* transaction tree */ } for (i = tat->size; --i > n; ) tat_delete(vec[i]); free(tat); /* on error delete created subtrees */ return NULL; /* and the transaction tree node */ } /* _create() */problema |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 02:19.



















