Bonjour,

Me re voilà. J'ai un encore un souci...

Le debugger me dit que l'adresse passée à l'argument 1 que je passe à la fonction strcpy dans la fonction initializeString est hors limite.

En effet, il affiche une drôle d'adresse.

Quand je choisi x=1, y=2 et z=20, main me retourne 0. Tout c'est bien passé. Si j'augmente une de ces valeurs, j'arrive à l'erreur dite précédemment.

Si l'adresse que je donne est hors limite, cela veut dire que c'est quelque part dans l'allocation que quelque chose ne c'est pas bien faite.

Je pense faire ce qu'il faut mais à priori ce n'est pas le cas. Je tourne en rond depuis quelques heures. Si un regard frais pouvait m'aider...

Voici sans plus attendre le code source :

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
void desallocate2DCharArray( char **_2DCharArray, int d1 )
{
    if ( _2DCharArray != NULL )
    {
        int i;
        for ( i=0 ; i<d1 ; i++ )
        {
            free(_2DCharArray[i]);
        }
 
        free(_2DCharArray);
    }
 
    else perror("An error has occured in the desallocate2DCharArray function ");
}
 
void desallocate3DCharArray( char ***_3DCharArray, int d1, int d2 )
{
    if ( _3DCharArray != NULL )
    {
        int i;
        for ( i=0 ; i<d1 ; i++ )
        {
            int j;
            for ( j=0 ; j<d2 ; j++)
            {
                free(_3DCharArray[i][j] );
            }
        }
 
        desallocate2DCharArray( *_3DCharArray, d1 );
    }
 
    else perror("An error has occured in the desallocate2DCharArray function ");
}
 
char ***allocate3DCharArray ( int d1, int d2, int d3 )
{
    char ***_3DCharArray = (char ***)malloc( d1*sizeof(char**) );
 
    if ( _3DCharArray != NULL )
    {
        int i;
        for ( i=0 ; i<d1 ; i++ )
        {
            _3DCharArray[i] = (char **)malloc( d2*sizeof(char) );
 
            if ( _3DCharArray[i] == NULL )
            {
                perror("\n An error has occured in the desallocate2DCharArray ");
                desallocate2DCharArray( *_3DCharArray, i );
                return NULL;
            }
        }
 
        for ( i=0 ; i<d1 ; i++ )
        {
            int j;
            for ( j=0 ; j<d2 ; j++ )
            {
                _3DCharArray[i][j] = (char*)malloc( d3*sizeof(char) );
 
                if ( _3DCharArray[i][j] == NULL )
                {
                    perror("\n An error has occured in allocate3DCharArray function");
                    desallocate3DCharArray( _3DCharArray, i, j );
                    return NULL;
                }
            }
        }
    }
 
    else
    {
        perror("\n An error has occured in the allocate3DCharArray function ");
        free(_3DCharArray);
        return NULL;
    }
 
    return _3DCharArray;
}
 
void initializeString( char *string, const char *initialisationsString, int stringLength )
{
    if ( string != NULL && initialisationsString != NULL )
    {
        if ( stringLength >= strlen(initialisationsString) )
        {
            strcpy( string, initialisationsString );
        }
 
        else exit(-1);
    }
 
    else perror("\n An error has occured in the initialiseString function ");
}
 
void initialize3DCharArray( char ***_3DCharArray, int d1, int d2, int d3, const char *initialisationsString )
{
    if ( _3DCharArray != NULL && initialisationsString != NULL )
    {
        int i;
        for ( i=0 ; i<d1 ; i++)
        {
            int j;
            for ( j=0 ; j<d2 ; j++ )
            {
                initializeString( _3DCharArray[i][j], initialisationsString, d3 );
            }
        }
    }
 
    else exit(-1);
}
 
void display3DCharArray( char ***_3DCharArray, int d1, int d2 )
{
    if ( _3DCharArray != NULL )
    {
        int i;
        for ( i=0 ; i<d1 ; i++ )
        {
            int j;
            for ( j=0 ; j<d2 ; j++ )
            {
                printf("\n _3DCharArray[%d][%d] = %s", i, j, _3DCharArray[i][j] );
            }
        }
    }
 
    else perror("\n An error has occured in the display3DCharArray function ");
}
 
    int x=2;
    int y=2;
    int z=20;
    char ***ptr = allocate3DCharArray( x, y, z );
    initialize3DCharArray( ptr, x, y, z, "chaine" );
    display3DCharArray( ptr, x, y );
    desallocate3DCharArray( ptr, x, y );
 
      return 0;
}
Merci d'avance une fois de plus.