IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C Discussion :

memory corruption error


Sujet :

C

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 28
    Points : 11
    Points
    11
    Par défaut memory corruption error
    Bonjour
    je suis desole de faire un enieme post sur un probleme d'allocation memoire mais la j'avoue que je seche et toute aide sera la biienvenue.

    Voila:
    j'ai un programme possédant une fonction qui appelle une autre fonction et ceci au sein d'une boucle de traitement:

    code de la fonction appelante:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    int ** induct_graphs;
    for (...maboucle)
    {
    induct_graphs = heuristic(k, temp, decomp, decomp);
    result = test(induct_graphs);
    if (result == 0)
       return 0;
    }
    De façon résumé la fonction heuristic est la suivante:
    code de la fonction appelé
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    int ** heuristic(...)
    {
    int ** graphs;
    ************************************
    graphs = calloc(1, sizeof(int*));
    alloc_test_2d_table(graphs); //test si le calloc de la matrice s'est bien déroulé
    graphs[0] = calloc(2, sizeof(int));
     
    ... traitement sur graphs ....
     
    return graphs;
    }
    Tout se passe bien pour un grand nombre de tour de boucle mais au bout d'un moment j'obtiens une erreur "memory corruption" (pas d'erreur ou de warning a la compil) ... au niveau des ************************************. c'est a dire que le calloc m'indique une erreur de corruption mémoire.
    Je ne comprends pas trop car il me semblait que la variable grph de ma fonction heuristic était libéré à la sortie de la fonction...

    Je ne vois pas ... si vous avez une idée je vous en remercie!!

  2. #2
    Membre habitué
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Juillet 2005
    Messages
    147
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juillet 2005
    Messages : 147
    Points : 165
    Points
    165
    Par défaut
    Salut,

    Si tu travailles sous linux, tu peux essayer d'exécuter ton programme avec l'outil Valgrind, qui te permettra d'avoir des infos sur la mémoire que tu utilises, que tu libères (ou pas ), et des erreurs d'allocation mémoire que tu pourrais faire. S'il indique des erreurs, et que tu ne vois pas trop la cause, envoie-nous le log, avec le code en archive si tu veux.
    Si tu n'es pas sous linux, je ne connais pas d'outils pour windows ou mac, mais ça doit exister, quelqu'un te renseignera surement.

    Bon courage
    Avec les ordinateurs, 99% des bugs proviennent de l'interface chaise-clavier...

    Comment ça 1Km n'est pas égal à 1024m ???

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 28
    Points : 11
    Points
    11
    Par défaut
    Merci pour ton aide, je vais essayé valgrind car je suis bien sous linux . Je vais egalemen faire d'autres essais mais franchement je comprends pas ce qui se passe...

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 28
    Points : 11
    Points
    11
    Par défaut
    Rahhh je viens de tester avec valgrind , le programme s'execute correctement lorsque j'utilise cet outil je vais aller voir le log... au cas ou je le met dessous mais je dois avouer que je ne comprends pas tout

    ==11366== Memcheck, a memory error detector.
    ==11366== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
    ==11366== Using LibVEX rev 1804, a library for dynamic binary translation.
    ==11366== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
    ==11366== Using valgrind-3.3.0-Debian, a dynamic binary instrumentation framework.
    ==11366== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
    ==11366==
    ==11366== My PID = 11366, parent PID = 8876. Prog and args are:
    ==11366== ./main_RAVD
    ==11366==
    --11366--
    --11366-- Command line
    --11366-- ./main_RAVD
    --11366-- Startup, with flags:
    --11366-- -v
    --11366-- --tool=memcheck
    --11366-- --leak-check=full
    --11366-- --num-callers=40
    --11366-- --log-file=valgrind2.log
    --11366-- Contents of /proc/version:
    --11366-- Linux version 2.6.24-24-generic (buildd@palmer) (gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu4)) #1 SMP Tue Jul 7 19:46:39 UTC 2009
    --11366-- Arch and hwcaps: X86, x86-sse1-sse2
    --11366-- Page sizes: currently 4096, max supported 4096
    --11366-- Valgrind library directory: /usr/lib/valgrind
    --11366-- Reading syms from /lib/ld-2.7.so (0x4000000)
    --11366-- Reading debug info from /lib/ld-2.7.so...
    --11366-- ... CRC mismatch (computed aa5c1346 wanted 05177b25)
    --11366-- object doesn't have a symbol table
    --11366-- Reading syms from /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD (0x8048000)
    --11366-- Reading syms from /usr/lib/valgrind/x86-linux/memcheck (0x38000000)
    --11366-- object doesn't have a dynamic symbol table
    --11366-- Reading suppressions file: /usr/lib/valgrind/default.supp
    --11366-- Reading syms from /usr/lib/valgrind/x86-linux/vgpreload_core.so (0x401E000)
    --11366-- Reading syms from /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so (0x4020000)
    --11366-- Reading syms from /lib/tls/i686/cmov/libc-2.7.so (0x4039000)
    --11366-- Reading debug info from /lib/tls/i686/cmov/libc-2.7.so...
    --11366-- ... CRC mismatch (computed af7c73db wanted ca2b0f8f)
    --11366-- object doesn't have a symbol table
    --11366-- REDIR: 0x40ab620 (rindex) redirected to 0x4023710 (rindex)
    --11366-- REDIR: 0x40ab250 (strlen) redirected to 0x40239d0 (strlen)
    --11366-- REDIR: 0x40ac510 (mempcpy) redirected to 0x4024490 (mempcpy)
    --11366-- REDIR: 0x40a6930 (calloc) redirected to 0x4021b70 (calloc)
    --11366-- REDIR: 0x40a8650 (realloc) redirected to 0x4022b10 (realloc)
    --11366-- REDIR: 0x40ad3a0 (strchrnul) redirected to 0x4023df0 (strchrnul)
    --11366-- REDIR: 0x40a6c20 (malloc) redirected to 0x4022a50 (malloc)
    --11366-- REDIR: 0x40a8460 (free) redirected to 0x40225f0 (free)
    --11366-- REDIR: 0x40ac9a0 (memcpy) redirected to 0x4024aa0 (memcpy)
    ==11366== Invalid write of size 4
    ==11366== at 0x804AE0B: test_graph_multipode (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804B10B: test_ravd (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804B616: test_k_ballons_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804BBA9: main (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== Address 0x497cc5c is 0 bytes after a block of size 12 alloc'd
    ==11366== at 0x4021BDE: calloc (vg_replace_malloc.c:397)
    ==11366== by 0x804B0AF: test_ravd (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804B616: test_k_ballons_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804BBA9: main (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    --11366-- REDIR: 0x40ac4b0 (memset) redirected to 0x4023d50 (memset)
    ==11366==
    ==11366== ERROR SUMMARY: 28 errors from 1 contexts (suppressed: 11 from 1)
    ==11366==
    ==11366== 28 errors in context 1 of 1:
    ==11366== Invalid write of size 4
    ==11366== at 0x804AE0B: test_graph_multipode (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804B10B: test_ravd (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804B616: test_k_ballons_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804BBA9: main (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== Address 0x497cc5c is 0 bytes after a block of size 12 alloc'd
    ==11366== at 0x4021BDE: calloc (vg_replace_malloc.c:397)
    ==11366== by 0x804B0AF: test_ravd (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804B616: test_k_ballons_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804BBA9: main (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    --11366--
    --11366-- supp: 11 dl-hack3-1
    ==11366==
    ==11366== IN SUMMARY: 28 errors from 1 contexts (suppressed: 11 from 1)
    ==11366==
    ==11366== malloc/free: in use at exit: 199,424 bytes in 10,841 blocks.
    ==11366== malloc/free: 187,781 allocs, 176,940 frees, 14,535,944 bytes allocated.
    ==11366==
    ==11366== searching for pointers to 10,841 not-freed blocks.
    ==11366== checked 64,692 bytes.
    ==11366==
    ==11366==
    ==11366== 16 bytes in 1 blocks are definitely lost in loss record 4 of 14
    ==11366== at 0x4021BDE: calloc (vg_replace_malloc.c:397)
    ==11366== by 0x8048C6D: k_ballon_test_properties (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804B2B5: test_k_ballons_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804BBA9: main (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366==
    ==11366==
    ==11366== 16 bytes in 1 blocks are definitely lost in loss record 5 of 14
    ==11366== at 0x4021BDE: calloc (vg_replace_malloc.c:397)
    ==11366== by 0x804B25C: test_k_ballons_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804BBA9: main (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366==
    ==11366==
    ==11366== 16 bytes in 1 blocks are definitely lost in loss record 6 of 14
    ==11366== at 0x4021BDE: calloc (vg_replace_malloc.c:397)
    ==11366== by 0x804B246: test_k_ballons_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804BBA9: main (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366==
    ==11366==
    ==11366== 204 bytes in 2 blocks are definitely lost in loss record 7 of 14
    ==11366== at 0x4022B8E: realloc (vg_replace_malloc.c:429)
    ==11366== by 0x80486E4: reallocate_table (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x8048A6F: decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804BB09: main (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366==
    ==11366==
    ==11366== 1,668 bytes in 144 blocks are definitely lost in loss record 9 of 14
    ==11366== at 0x4021BDE: calloc (vg_replace_malloc.c:397)
    ==11366== by 0x804B0AF: test_ravd (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804B616: test_k_ballons_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804BBA9: main (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366==
    ==11366==
    ==11366== 12,132 bytes in 1,011 blocks are definitely lost in loss record 12 of 14
    ==11366== at 0x4021BDE: calloc (vg_replace_malloc.c:397)
    ==11366== by 0x8048796: reallocate_matrix (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x8049F9A: heuristic_3 (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804B5FC: test_k_ballons_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804BBA9: main (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366==
    ==11366==
    ==11366== 184,668 (33,908 direct, 150,760 indirect) bytes in 1,202 blocks are definitely lost in loss record 14 of 14
    ==11366== at 0x4022B8E: realloc (vg_replace_malloc.c:429)
    ==11366== by 0x804873E: reallocate_matrix (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x80488AA: copy_table (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804896C: sum_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x80489DC: sum_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x80489DC: sum_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x80489DC: sum_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804B9ED: main (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366==
    ==11366== LEAK SUMMARY:
    ==11366== definitely lost: 47,960 bytes in 2,362 blocks.
    ==11366== indirectly lost: 150,760 bytes in 8,477 blocks.
    ==11366== possibly lost: 0 bytes in 0 blocks.
    ==11366== still reachable: 704 bytes in 2 blocks.
    ==11366== suppressed: 0 bytes in 0 blocks.
    ==11366== Reachable blocks (those to which a pointer was found) are not shown.
    ==11366== To see them, rerun with: --leak-check=full --show-reachable=yes
    --11366-- memcheck: sanity checks: 197 cheap, 9 expensive
    --11366-- memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use
    --11366-- memcheck: auxmaps_L1: 0 searches, 0 cmps, ratio 0:10
    --11366-- memcheck: auxmaps_L2: 0 searches, 0 nodes
    --11366-- memcheck: SMs: n_issued = 144 (2304k, 2M)
    --11366-- memcheck: SMs: n_deissued = 0 (0k, 0M)
    --11366-- memcheck: SMs: max_noaccess = 65535 (1048560k, 1023M)
    --11366-- memcheck: SMs: max_undefined = 0 (0k, 0M)
    --11366-- memcheck: SMs: max_defined = 21 (336k, 0M)
    --11366-- memcheck: SMs: max_non_DSM = 144 (2304k, 2M)
    --11366-- memcheck: max sec V bit nodes: 0 (0k, 0M)
    --11366-- memcheck: set_sec_vbits8 calls: 0 (new: 0, updates: 0)
    --11366-- memcheck: max shadow mem size: 2608k, 2M
    --11366-- translate: fast SP updates identified: 2,543 ( 89.1%)
    --11366-- translate: generic_known SP updates identified: 168 ( 5.8%)
    --11366-- translate: generic_unknown SP updates identified: 141 ( 4.9%)
    --11366-- tt/tc: 5,498 tt lookups requiring 5,587 probes
    --11366-- tt/tc: 5,498 fast-cache updates, 2 flushes
    --11366-- transtab: new 2,730 (60,436 -> 839,921; ratio 138:10) [0 scs]
    --11366-- transtab: dumped 0 (0 -> ??)
    --11366-- transtab: discarded 0 (0 -> ??)
    --11366-- scheduler: 19,736,548 jumps (bb entries).
    --11366-- scheduler: 197/243,551 major/minor sched events.
    --11366-- sanity: 198 cheap, 9 expensive checks.
    --11366-- exectx: 769 lists, 100 contexts (avg 0 per list)
    --11366-- exectx: 195,404 searches, 195,308 full compares (999 per 1000)
    --11366-- exectx: 44,234 cmp2, 53 cmp4, 0 cmpAll
    --11366-- errormgr: 21 supplist searches, 1,125 comparisons during search
    --11366-- errormgr: 39 errlist searches, 59 comparisons during search

  5. #5
    Membre habitué
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Juillet 2005
    Messages
    147
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juillet 2005
    Messages : 147
    Points : 165
    Points
    165
    Par défaut
    Salut,

    Valgrind te signale bien des erreurs, et des fuites de mémoires. Je te conseille d'essayer de corriger d'abord les erreurs, tu as la ligne de la fonction qui les provoque, et c'est souvent un mauvais accès à la mémoire (dépassement de buffer, libération de pointeur non valide, etc). Une fois qu'il n'y a plus d'erreur, tu pourras corriger les memory leaks.

    Si tu ne trouves pas, postes ton code et je regarderai.

    @+
    Avec les ordinateurs, 99% des bugs proviennent de l'interface chaise-clavier...

    Comment ça 1Km n'est pas égal à 1024m ???

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    115
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 115
    Points : 104
    Points
    104
    Par défaut
    Je ne comprends pas trop car il me semblait que la variable grph de ma fonction heuristic était libéré à la sortie de la fonction...
    la variable oui mais pas la mémoire allouée dynamiquement

    d'ailleurs :

    ==11366== LEAK SUMMARY:
    ==11366== definitely lost: 47,960 bytes in 2,362 blocks.
    ==11366== indirectly lost: 150,760 bytes in 8,477 blocks.

  7. #7
    Membre confirmé
    Inscrit en
    Juillet 2005
    Messages
    512
    Détails du profil
    Informations forums :
    Inscription : Juillet 2005
    Messages : 512
    Points : 641
    Points
    641
    Par défaut
    Dans la fonction heuristic de la memoire est alloué sur le tas,
    mais quand libère tu cette mémoire ?

  8. #8
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 28
    Points : 11
    Points
    11
    Par défaut
    Bonjour,
    je vous remercie pour vos reponse ...
    En fait je fais un:

    dans la fonction appellante (la fonction qui appelle heuristic).
    il me semblait que cela permettait de liberer la memoire d'induct_graph et donc de graph (memoire ayant été alloué dans heuristic).

    je ne vois pas comment libérer cette espace alloué puisque je renvoit ma matrice graph je ne peux libérer la mémoire qu'au niveau de la fonction appellante. (meme avec free , cela ne marche pas ).

    Merci en tout cas pour votre aide, je continue de chercher mais j'apprecie votre disponibilité.

  9. #9
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    115
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 115
    Points : 104
    Points
    104
    Par défaut
    Si je ne me trompe pas tu ne désalloue que

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    graphs = calloc(1, sizeof(int*));
    et pas

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    graphs[0] = calloc(2, sizeof(int));
    d'ailleurs :

    ==11366== 16 bytes in 1 blocks are definitely lost in loss record 4 of 14
    ==11366== at 0x4021BDE: calloc (vg_replace_malloc.c:397)
    ==11366== by 0x8048C6D: k_ballon_test_properties (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804B2B5: test_k_ballons_decomposition (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    ==11366== by 0x804BBA9: main (in /home/cha/Bureau/src/programmes_C/RAVD/main_RAVD)
    Pas de free de certains calloc, ce qui correspondrait à ce que je dis.

    Tu alloues un tableau de x*32 bits pour stocker tes pointeurs. C'est ce que tu désalloues par contre tu alloues de la mémoire pour ces pointeurs, il faut aussi la désallouer avant de faire le free d'induct_graphs.

  10. #10
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 28
    Points : 11
    Points
    11
    Par défaut
    Merci beaucoups je n'avais pas vu effectivement je vais essayé et je vous tiens au courant ...
    en tout cas c bien sympa toute cette aide ... (je crois que l'apprentissage du java / et du C++) m'ont un peu fait perdre certaines notions de C ... ARF)

    Merci je vous tiens au jus

  11. #11
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 28
    Points : 11
    Points
    11
    Par défaut
    Bon j'ai essayé de faire un peu de ménage dans mon code mai c 'est pire... en meme temps je m'y attendais un peu..
    Je vous joint mon code (main et util) ; j'ai un probleme au niveau de la fonction de reallocation (reallocate_matrix) je travaille sur une matrice temporaire et j'aimerai que la matrice m que je passe en paramêtre (et qui doit etr réallouée) pointe sur cette matrice temporaire (utilisée pour le realloc).
    Bref si vous pouviez m'apporter un eclaircissement....

    util.h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
     
    #include <stdio.h>
    #include <stdlib.h>
     
     
    void alloc_test_2d_table(int **table);
     
    int * reallocate_table(int *table, int n);
     
    // int **
    void reallocate_matrix(int ***m, int old_line_size,int new_line_size, int new_row_size);
     
    int ** expand_matrix_line(int ** m, int line_number, int new_line_size);
     
    void free_matrix (int **matrix, int size);
     
    void copy_table(int *temp, int temp_size, int ** result, int result_size);
     
    void sum_decomposition(int k, int sum_member, int temporary_sum, int final_sum, int i, int *temp, int ** decomp ,int *cpt);
     
    void decomposition(int ** decomp, int n);
     
    int k_ballon_test_property_3(int * k_ballon, int ** decomp_n);
     
    int k_ballon_test_property_4(int * k_ballon, int nb_vertices);
     
    int k_ballon_test_property_5_6(int * k_ballon, int k, int nb_vertices, int ** decomp_n);
     
    int k_ballon_test_properties(int ** k_ballons, int k, int nb_vertices, int ** decomp_n);
     
    int ** heuristic_1(int k, int * k_ballon,int * decomp_n_i, int decomp_size);
     
    int ** heuristic_2(int k, int * k_ballon,int * decomp_n_i, int decomp_size);
     
    int ** heuristic_3(int k, int * k_ballon,int * decomp_n_i, int decomp_size);
     
    int cat_RAVD_test(int a, int b);
     
    int test_graph_multipode(int * graph, int number_of_pod);
     
    int test_ravd(int ** induct_graphs);
     
    void test_k_ballons_decomposition(int ** k_ballons, int k, int nb_vertices, int ** decomp_n,int method);
     
    int test_util();
    pour util.c
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
     
    /**
     * \file util.c
     * \version 0.2
     * \date 20 mai 2009
     *
     */
     
    #include "util.h" 
    #include <dirent.h>
     
    /**
     * \fn void check_pointer(void * t)
     * \brief Verify the correct allocation of a table (pointer)
     * \param t new allocated table (pointer), can't be null
     */
    void check_pointer(void * t)
    {
    	printf("debug dans check pointer\n");
    // 	printf ("%d\n",&t);
    	if ( t == NULL )
    	{
    		printf("not enough memory");
    		exit (1);
    	}
     
    }
     
    /**
     * \fn int * reallocate_table(int *table, int n)
     * \brief Reallocate a table of integer
     * \param table table to reallocate
     * \param n new size of t&he table
     * \return the reallocated table
     */
    int * reallocate_table(int *table, int n)
    {
    	printf("debug dans realloc table\n");
    	int *temp = realloc ( table, (n * sizeof(int)) );
    	check_pointer(temp);
    	return temp;
    }
     
    /**
     * \fn int ** reallocate_matrix(int **m, int new_line_size, int new_row_size)
     * \brief Reallocate a matrix of integer
     * \param m the matrix to reallocate
     * \param old_line_size the old number of lines of the matrix
     * \param new_line_size the new number of lines of the matrix
     * \param new_row_size the new number of rows of the matrix
     * \return the reallocated matrix
     */
    void reallocate_matrix(int ***m, int old_line_size, int new_line_size, int new_row_size)
    {
    	int ** temp =realloc((*m), new_line_size * sizeof(int*) );
    	printf("debug dans reallocate matrix %d \n",temp[0][0]);
    	printf("*************************************\n");
    	printf("old line size = %d\n",old_line_size);
    	for (int i = 1; i < old_line_size; i++)
    	{
    		for(int j = 0; j < 5; j++)
    		{
    			printf("%d ",*m[i][j]);
    		}
    		printf("\n");
    	}
    	printf("*************************************\n");
    // 	check_pointer(temp);
     
    	for (int i = old_line_size; i < new_line_size; i++)
    	{
    		temp[i] = calloc( new_row_size, sizeof(int) );
    		printf("hello\n");
    		check_pointer(temp[i]);
    	}
    	temp[1][0]=1;
    // 	*m = temp;
    // 	*m = &temp;
     
    	m=temp;
     
    	printf("debug dans reallocate matrix %d \n",*m[1][0]);
    	printf("*************************************\n");
    	printf("new line size = %d\n",new_line_size);
    	for (int i = 1; i < new_line_size; i++)
    	{
    		for(int j = 0; j < 5; j++)
    		{
    			printf("%d ",temp[i][j]);
    //  			printf("%d ",*m[i][j]);
    		}
    		printf("ligne : %d \n",i);
    	}
     
    	for (int i = 1; i < new_line_size; i++)
    	{
    		for(int j = 0; j < 5; j++)
    		{
    // 			printf("%d ",temp[i][j]);
      			printf("%d ",*m[i][j]);
    		}
    		printf("ligne : %d \n",i);
    	}
    	printf("*************************************\n");
     
    	printf("\n");
    // 	printf("adresse 1 %d\n",&m);
    	printf ("bjour\n");
    // return m;
    // 	return temp;
    }
     
    /**
     * \fn void free_matrix (int ** matrix, int size)
     * \brief free a matrix
     * \param matrix matrix to free
     * \param line_number a line of the matrix
     * \param new_line_size new size of the matrix line
     */
    int ** expand_matrix_line(int ** m, int line_number, int new_line_size)
    {
    	printf("debug dans epand matrix\n");
    	int ** temp = m;
    	temp[line_number] = realloc ( m[line_number], ((new_line_size) * sizeof(int)) );
    	check_pointer(temp[line_number]);
    	return temp;
    }
     
    /**
     * \fn void free_matrix (int ** matrix, int size)
     * \brief free a matrix
     * \param matrix matrix to free
     * \param size number of matrix line
     */
    void free_matrix (int ** matrix, int size)
    {
    	printf("debug dans free matrix\n");
    	for ( int i = 0; i < size; i++ )
    	{
    		free(matrix[i]);
    	}
    	free(matrix);
    }
     
    /**
     * \fn void copy_table(int * temp, int temp_size, int ** result, int result_size)
     * \brief Copy the value of table "temp" in the matrice "result" (last line)
     * \param temp source table
     * \param temp_size size of the table to copy
     * \param result destination matrix
     * \param result_size number of line of the matrix
     * \return a matrix
     */
    void copy_table(int *temp, int temp_size, int ** result, int result_size)
    {
    	printf("debug dans copy table\n");
    // 	printf ("%d\n",temp_size);
    // 	printf ("%d\n",result_size);
    // 	result = 
    	printf ("result size %d\n",result_size);
    	reallocate_matrix(&result, result_size -1 ,result_size, temp_size );
    // 	printf ("a toi de jouer: %d \n",&result);
    	printf ("taille de la matrice result: %d \n",result_size);
    // 	printf("salut mon gars %d\n",result[result_size-1][0]);
    	if (result_size==4)
    	{
    /*		for ( int i = 0; i < 1; i++ )
    		{
    		printf("%d ",result[0][i]);
    	}
    		printf("\n");*/
    // 		printf("taille de resultat : %d \n",result_size);
    		for ( int i = 0; i < temp_size; i++ )
    		{
    			printf("%d ",result[result_size-3][i]);
    		}
    		printf("\n");
     
     
    		for ( int i = 0; i < temp_size; i++ )
    		{
    			printf("%d ",result[result_size-2][i]);
    		}
    		printf("\n");
     
    		printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    		for ( int i = 0; i < temp_size; i++ )
    		{
    		result[result_size-1][i] = 1;
    		}
    		printf("\n");
     
     
    	}
    	for ( int i = 0; i < temp_size; i++ )
    	{
    		printf("%d ",temp[i]);
    	}
    	printf("\n");
     
    	for ( int i = 0; i < temp_size; i++ )
    	{
    		result[result_size-1][i] = temp[i];
    	}
    }
     
    /**
     * \fn void sum_decomposition(int k, int sum_member, int temporary_sum, int final_sum, int i, int *temp, int ** decomp ,int *cpt)
     * \brief Compute recursively the decomposition of an integer n (ie:final_sum) in k parts 
     * \param k number of parts of the decomposition
     * \param sum_member eventual member of the sum 
     * \param temporary_sum temporary sum must be less than final_sum
     * \param final_sum equivalent to n 
     * \param i position of the number sum_member in the sum
     * \param temp temporary table use to store a new decomposition
     * \param decomp represent the result matrix
     * \param cpt count the number of decomposition
     * \return a matrix
     */
    void sum_decomposition(int k, int sum_member, int temporary_sum, int final_sum, int i, int *temp, int ** decomp ,int *cpt)
    {
    	printf("debug dans sumdecomposition\n");
    	int member = sum_member;
    	int c = i;
    	//for the last value of the sum
    	if ( k == 1 )
    	{
    		if ( (final_sum - temporary_sum) < member )
    		{
    			temp[c] = final_sum - temporary_sum;
    			temp[0] = c + 1;  //temp[0] represent the number of elemements of the sum
    			*cpt = *cpt + 1;
    			copy_table ( temp, temp[0], decomp, *cpt );
    			printf("yep\n");
    		}
    	}
    	else
    	{
    		while ( member > 1 )
    		{
    			member--;
    			temporary_sum = temporary_sum + member;
    			if (temporary_sum < final_sum)
    			{
    				temp[c] = member;
    				c++;//TODO
    				printf("yep\n");
    				sum_decomposition(k - 1, member + 1 , temporary_sum, final_sum, c, temp, decomp, cpt); //sorted list of decomposition
    				temporary_sum = temporary_sum-member;
    				c--;
    			}
    			else
    			{
    				temporary_sum = temporary_sum - member;
    			}
    		}
    	}
    }
    et voici le code pour le main
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
     
    /**
     * \file main.c
     * \brief k-balloons decomposition test program.
     * \date 20 mai 2009
     *
     * Main program for testing k-ballons decomposition.
     *
     */
     
    #include "util.c"
     
     
    /**
     * \fn int main()
     * \brief Main program for testing k-balloon decomposition
     * \return System code
     */
    int main()
    {
    	int k;
    	int n;
    	int nb_vertices_on_branch;
    	int ** k_ballons;
    	int *temp;
    // 	int **decomp;
    	int cpt = 1;
     
    	remove("candidate");
    	remove("rejected");
    	remove("final_result");
     
    	int y=13;
    	int z=14;
    // 	int m=3;
     
    	//Loop for multi-test
    	for (k = 4; k < 5; k++)
    	{
    		for (nb_vertices_on_branch = y; nb_vertices_on_branch < z; nb_vertices_on_branch++)
    		{
    			n = nb_vertices_on_branch + 2;
     
    			cpt = 1;
    			k_ballons = calloc(1, sizeof(int*));
    			check_pointer(k_ballons);
    			k_ballons[0] = calloc (1, sizeof(int)); 
    			check_pointer(k_ballons[0]);
    			temp = calloc(1, sizeof(int*));
    			temp = reallocate_table(temp,k+1);
    			printf("debug purpose\n");
    			k_ballons[0][0] = 23;
    			sum_decomposition(k,nb_vertices_on_branch,0,nb_vertices_on_branch,1,temp,k_ballons,&cpt);
    			k_ballons[0][0] = cpt - 1;
    			printf("debug purpose\n");
    			//BEGIN DEBUG
    			int z,w=0;
    			for (z = 1; z < k_ballons[0][0]+1;z++)
    			{
    				for (w = 1; w < k_ballons[z][0]; w++)
    				{
    					printf ("%d ",k_ballons[z][w]);
    				}
    				printf("\n");
    			}
    			printf("\n");
    			//END DEBUG
    		}
    	}
    	remove("candidate");
     
    	return 1;
    }
    merci d'avance a tous

  12. #12
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    115
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 115
    Points : 104
    Points
    104
    Par défaut
    Tu as quoi comme soucis car là c'est un peu vague

    Au niveau de ta fonction, je trouve ça personnellement un peu lourd de faire un alloc ou realloc sur un pointeur passé en paramètre (enfin pointeur de pointeur et dans ton cas un triple). J'aimerai bien avoir un avis d'ailleurs sur ce point passer un pointeur de pointeur ou utiliser le return ?

    Sinon l'utilisation du realloc n'est pas très sûre à mon goût, pourquoi tu n'utilises pas le pointeur renvoyé par ton realloc. Tu continue d'utiliser ton pointeur de départ or la réallocation a très bien pu déplacer la zone mémoire et donc **tmp peut être différent de *m.

  13. #13
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 28
    Points : 11
    Points
    11
    Par défaut
    Bonjour,
    pour etre plus precis , j'essaie de faire une realloc sure de ma matrice m passé en parametre de la fonction reallocate_matrix.

    J'ai essayé de passer par un temporaire ou de faire plus directement:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    int ** temp =realloc((*m), new_line_size * sizeof(int*) );
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    (*m) = realloc((*m), new_line_size * sizeof(int*) );
    Apres quelque test je me suis rendu compte de deux choses:
    - lorsque je passe par temp , pas de souci la realloc est bien réalisée mais je n'arrive pas a faire pointer m sur temp (j'utilise m dans la suite du programme). temp est bien la matrice que j'attend mais pas moyen que m pointe sur temp.
    - Lorsque je fais la realoc direct sur m (le second code), j'ai pas d'erreur au niveau du realloc (pas de warning , rien) mais si je veux par exemple voir *m[1][0] et bien erreur de segmentation.

    Bref j'espere avoir ete plus clair sur ce probleme ...

    Merci
    a plus (ahhhh le new du C++ ...)

  14. #14
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 28
    Points : 11
    Points
    11
    Par défaut
    a oui j'oublié entre return/pointerur de pointeur ... et bien je veux bien aussi des retours la dessus. On m'avait dis que l'un ou l'autre etait pratiqué et que ce n'etait pas plus sale d'utiliser un moyen ou l'autre mais bon...
    Je trouve ça un peu lourd aussi les pointeurs de pointeurs de ... mais je souhaitais utiliser cette solution (c'est sur que ce n'est pas du sucre syntaxique) mais qui me semble plus clair pour identifier d'eventuelles fuite memoires ou oublies de desallocation.

  15. #15
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    115
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 115
    Points : 104
    Points
    104
    Par défaut
    - lorsque je passe par temp , pas de souci la realloc est bien réalisée mais je n'arrive pas a faire pointer m sur temp (j'utilise m dans la suite du programme). temp est bien la matrice que j'attend mais pas moyen que m pointe sur temp.
    Dans ton code, tu ne fais rien pour que m pointe sur temp, il est donc normal que ces deux pointeurs puissent diverger

    m contient l'adresse de la zone avant réallocation, temp l'adresse après et cette dernière n'est pas forcément celle d'origine. Je ne comprends pas vraiment le
    pas moyen que m pointe vers temp
    C'est à toi de refaire pointer m sur temp.

    Quand tu dis que realloc est bien réalisé comment le sais-tu, je n'ai pas vu ou pas fait attention dans ton code à des tests sur le pointeur retourné. Pour le deuxième code, le realloc a pu échouer et donc forcément seg fault quand tu essaies d'accéder à m[0][1].

  16. #16
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 28
    Points : 11
    Points
    11
    Par défaut
    merci pour la rapidite de la reponse
    En ce qui concerne la verif du realloc elle n'est pas presente dans le code que j'ai collé ici mais je fait bien cette verification via une fonction dedié (check_pointer).

    Par contre en ce qui concerne m et temp , effectivement , ll faut que je refasse pointer m sur temp ce que j'ai essayé de faire avec m=&temp; mais cela ne fonctionne pas et je ne ais pas si cela vient de la syntaxe ou autre .

  17. #17
    Membre confirmé
    Inscrit en
    Juillet 2005
    Messages
    512
    Détails du profil
    Informations forums :
    Inscription : Juillet 2005
    Messages : 512
    Points : 641
    Points
    641
    Par défaut
    Dans le main
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    	int ** k_ballons;
     
    	k_ballons = calloc(1, sizeof(int*));
     
    	k_ballons[0] = calloc (1, sizeof(int));
     
            k_ballons[0][0] = 23;
     
           /* ... */
     
    	for (z = 1; z < k_ballons[0][0]+1;z++)
    	 {
              for (w = 1; w < k_ballons[z][0]; w++)
    	    {
    		printf ("%d ",k_ballons[z][w]);
    	    }
    ou est allouée la mémoire des elements de k_ballons superieur à [0][0] ?
    J'ai pas tous passé en détail mais je ne trouve pas l'allocation.

    (la libération non plus d'ailleur)

  18. #18
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    115
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 115
    Points : 104
    Points
    104
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    int ** temp =realloc((*m), new_line_size * sizeof(int*) );
    J'aurais donc tendance à écrire *m = temp

  19. #19
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 28
    Points : 11
    Points
    11
    Par défaut
    ou est allouée la mémoire des elements de k_ballons superieur à [0][0] ?
    J'ai pas tous passé en détail mais je ne trouve pas l'allocation.
    en fait k_ballon est une matrice qui est realloué de façon dynamique gace a la fonction reallocate_matrix c'est pour cela que je n'initialise que les premier element de k_ballon.


    Ensuite, j'avais testé le fameux m=*temp; mais je viens de me rendre compte qu'il y a une difference entre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    printf("%d ",(*m)[i][j]);
    et
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    printf("%d ",*m[i][j]);
    Probleme de lexer ou incompréhension de ma part? l'hypothese 2 me semble meilleurs...

    ce qui corrige une partie de l'erreur, j'avance tout doucement mais j'avance ... enfin j'esaie .
    Quoi qu'il en soit merci a tous si vous avez d'autres pistes de reflexion je suis toujours sur le pont.

  20. #20
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 28
    Points : 11
    Points
    11
    Par défaut
    AHHHHHH j'ai trouve je penses , en tout cas le code fonctionne.
    Je vous le met ici au cas...

    Grosso modo l'objectif est de calculer la decomposition d'un nombre n en une somme de k parts.(ici les parts sont triées par ordre decroissant et sans doublon).

    Voila

    Merci a tous mais ne m'oubliez pas je risque de revenir ... merci de votre dispo et de votre patiente!!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
     
    /**
     * \file util.c
     * \brief Util program for testing k-ballons decomposition.
     * \date 20 mai 2009
     *
     */
     
    #include "util.h" 
    #include <dirent.h>
     
    /**
     * \fn void check_pointer(void * t)
     * \brief Verify the correct allocation of a table (pointer)
     * \param t new allocated table (pointer), can't be null
     */
    void check_pointer(void * t)
    {
    	if ( t == NULL )
    	{
    		printf("not enough memory");
    		exit (1);
    	}
     
    }
     
    /**
     * \fn int * reallocate_table(int *table, int n)
     * \brief Reallocate a table of integer
     * \param table table to reallocate
     * \param n new size of t&he table
     * \return the reallocated table
     */
    int * reallocate_table(int *table, int n)
    {
    	int *temp = realloc ( table, (n * sizeof(int)) );
    	check_pointer(temp);
    	return temp;
    }
     
    /**
     * \fn int ** reallocate_matrix(int **m, int new_line_size, int new_row_size)
     * \brief Reallocate a matrix of integer
     * \param m the matrix to reallocate
     * \param old_line_size the old number of lines of the matrix
     * \param new_line_size the new number of lines of the matrix
     * \param new_row_size the new number of rows of the matrix
     * \return the reallocated matrix
     */
    void reallocate_matrix(int ***m, int old_line_size, int new_line_size, int new_row_size)
    {
    	int ** temp = realloc((*m), new_line_size * sizeof(int*) );	
    	check_pointer(temp);
    	for (int i = old_line_size; i < new_line_size; i++)
    	{
    		temp[i] = calloc( new_row_size, sizeof(int) );
    		check_pointer(temp[i]);
    	}
    	*m = temp;
    }
     
    /**
     * \fn void free_matrix (int ** matrix, int size)
     * \brief free a matrix
     * \param matrix matrix to free
     * \param line_number a line of the matrix
     * \param new_line_size new size of the matrix line
     */
    int ** expand_matrix_line(int ** m, int line_number, int new_line_size)
    {
    	int ** temp = m;
    	temp[line_number] = realloc ( m[line_number], ((new_line_size) * sizeof(int)) );
    	check_pointer(temp[line_number]);
    	return temp;
    }
     
    /**
     * \fn void free_matrix (int ** matrix, int size)
     * \brief free a matrix
     * \param matrix matrix to free
     * \param size number of matrix line
     */
    void free_matrix (int ** matrix, int size)
    {
    	for ( int i = 0; i < size; i++ )
    	{
    		free(matrix[i]);
    	}
    	free(matrix);
    }
     
    /**
     * \fn void copy_table(int * temp, int temp_size, int ** result, int result_size)
     * \brief Copy the value of table "temp" in the matrice "result" (last line)
     * \param temp source table
     * \param temp_size size of the table to copy
     * \param result destination matrix
     * \param result_size number of line of the matrix
     * \return a matrix
     */
    void copy_table(int *temp, int temp_size, int *** result, int result_size)
    {
    	reallocate_matrix(result, result_size -1 ,result_size, temp_size );
    	for ( int i = 0; i < temp_size; i++ )
    	{
    		(*result)[result_size-1][i] = temp[i];
    	}
    }
     
    /**
     * \fn void sum_decomposition(int k, int sum_member, int temporary_sum, int final_sum, int i, int *temp, int ** decomp ,int *cpt)
     * \brief Compute recursively the decomposition of an integer n (ie:final_sum) in k parts 
     * \param k number of parts of the decomposition
     * \param sum_member eventual member of the sum 
     * \param temporary_sum temporary sum must be less than final_sum
     * \param final_sum equivalent to n 
     * \param i position of the number sum_member in the sum
     * \param temp temporary table use to store a new decomposition
     * \param decomp represent the result matrix
     * \param cpt count the number of decomposition
     * \return a matrix
     */
    void sum_decomposition(int k, int sum_member, int temporary_sum, int final_sum, int i, int *temp, int *** decomp ,int *cpt)
    {
    	int member = sum_member;
    	int c = i;
    	//for the last value of the sum
    	if ( k == 1 )
    	{
    		if ( (final_sum - temporary_sum) < member )
    		{
    			temp[c] = final_sum - temporary_sum;
    			temp[0] = c + 1;  //temp[0] represent the number of elemements of the sum
    			*cpt = *cpt + 1;
    			copy_table ( temp, temp[0], decomp, *cpt );
    		}
    	}
    	else
    	{
    		while ( member > 1 )
    		{
    			member--;
    			temporary_sum = temporary_sum + member;
    			if (temporary_sum < final_sum)
    			{
    				temp[c] = member;
    				c++;//TODO
    				sum_decomposition(k - 1, member + 1 , temporary_sum, final_sum, c, temp, decomp, cpt); //sorted list of decomposition
    				temporary_sum = temporary_sum-member;
    				c--;
    			}
    			else
    			{
    				temporary_sum = temporary_sum - member;
    			}
    		}
    	}
    }
    et pour le main (pas grand chose de changer je passe juste une adresse lors de l'appel de sum_decomposition) ... d'ailleurs je ne met pas util.h c pas tres interessant:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
     
    /**
     * \file main.c
     * \brief k-balloons decomposition test program.
     * \date 20 mai 2009
     *
     * Main program for testing k-ballons decomposition.
     *
     */
     
    #include "util.c"
     
     
    /**
     * \fn int main()
     * \brief Main program for testing k-balloon decomposition
     * \return System code
     */
    int main()
    {
    	int k;
    	int n;
    	int nb_vertices_on_branch;
    	int ** k_ballons;
    	int *temp;
    // 	int **decomp;
    	int cpt = 1;
     
    	remove("candidate");
    	remove("rejected");
    	remove("final_result");
     
    	int y=13;
    	int z=16;
    // 	int m=3;
     
    	//Loop for multi-test
    	for (k = 4; k < 5; k++)
    	{
    		for (nb_vertices_on_branch = y; nb_vertices_on_branch < z; nb_vertices_on_branch++)
    		{
    			n = nb_vertices_on_branch + 2;
     
    			cpt = 1;
    			k_ballons = calloc(1, sizeof(int*));
    			check_pointer(k_ballons);
    			k_ballons[0] = calloc (1, sizeof(int)); 
    			check_pointer(k_ballons[0]);
    			temp = calloc(1, sizeof(int*));
    			temp = reallocate_table(temp,k+1);
    			k_ballons[0][0] = 23;
    			sum_decomposition(k,nb_vertices_on_branch,0,nb_vertices_on_branch,1,temp,&k_ballons,&cpt);
    			k_ballons[0][0] = cpt - 1;
    			//BEGIN INFORMATION
    			int z,w=0;
    			for (z = 1; z < k_ballons[0][0]+1;z++)
    			{
    				for (w = 1; w < k_ballons[z][0]; w++)
    				{
    					printf ("%d ",k_ballons[z][w]);
    				}
    				printf("\n");
    			}
    			printf("\n");
    			//END INFORMATION
    		}
    	}
    	remove("candidate");
     
    	return 1;
    }
    tout ca pour ça ... pffff ...

Discussions similaires

  1. Réponses: 1
    Dernier message: 14/06/2009, 00h10
  2. [C] Malloc, memory Corruption
    Par publicStaticVoidMain dans le forum C
    Réponses: 2
    Dernier message: 18/05/2009, 01h39
  3. Réponses: 0
    Dernier message: 18/09/2007, 19h07
  4. memory corruption
    Par GLSpirit dans le forum C++
    Réponses: 2
    Dernier message: 23/05/2007, 08h42
  5. Réponses: 2
    Dernier message: 13/02/2007, 12h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo