Bonjour,

Dans un programme que je suis en train de réaliser je cherche à rendre plus lisible mon code en mettant toutes les instructions d'allocation de mémoire (malloc & co) dans une même fonction que j'appelle plusieurs fois.
Mais voilà je me heurte à un problème. J'obtiens un 'segmentation fault' et je ne comprends absolulement pas pourquoi.

J'ai réécris la partie de code correspondante sans le contexte afin que cela soit plus compréhensible.

J'ai une variable de type char **
et je veux pouvoir allouer mon double tableau de la manière suivante :

Allocate(ma_variable, indice_premier_tableau, indice_second_tableau);

lorsque je veux que :
ma_variable[indice_premier_tableau][indice_second_tableau]
soit allouée

Mais bon comme il vaut mieux un bon bout de code qu'un long discours voilà le code que j'ai isolé du programme principale :

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 Allocate(char ***truc, int x, int y);
 
int main(void) {
 
        char **truc;
 
        Allocate(&truc, 0, 0);
        Allocate(&truc, 0, 1);
        Allocate(&truc, 0, 2);
        Allocate(&truc, 1, 0);
        Allocate(&truc, 1, 1);
        Allocate(&truc, 1, 2);
 
       // utilisation de truc ici
 
        return 0;
}
 
void Allocate(char ***truc, int x, int y) {
 
        static int lastx = -1;
 
        if(lastx != x) {
                if(x) {
                        *truc = realloc(*truc, sizeof(char *) * (x + 1));
                }
                else {
                        *truc = malloc(sizeof(char *));
               }
 
                lastx = x;
        }
 
        if(y) {
                *truc[x] = realloc(*truc[x], sizeof(char) * (y + 1));
        }
        else {
                *truc[x] = malloc(sizeof(char));
        }
 
}
Voici l'analyse de mon debugeur :

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
 
GNU gdb 6.8
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu"...
 
warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib64/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Core was generated by `./malloc_function'.
Program terminated with signal 11, Segmentation fault.
[New process 18083]
#0  0x00000000004006a8 in Allocate (truc=0x7fff59417568, x=1, y=0)
    at ./malloc_function.c:39
39			*truc[x] = malloc(sizeof(char));
(gdb)
le problème vient de la ligne 39, lors de la deuxième utilisation de la fonction
et je comprends pas pourquoi

pourriez-vous m'aider ?

merci,