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 :

address out of bounds


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Par défaut address out of bounds
    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.

  2. #2
    Expert confirmé
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    char ***allocate3DCharArray ( int d1, int d2, int d3 )
    {
    .....
                _3DCharArray[i] = (char **)malloc( d2*sizeof(char *) );

  3. #3
    Membre éclairé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Par défaut
    Ah yes, merci bien...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 15
    Dernier message: 31/03/2007, 16h32
  2. adress 0x.. out of bounds
    Par kamouminator dans le forum C
    Réponses: 4
    Dernier message: 13/11/2006, 19h33
  3. index out of bound
    Par toure32 dans le forum Delphi
    Réponses: 1
    Dernier message: 07/11/2006, 15h51
  4. [Struts]Bean populate & array index out of bound
    Par djoukit dans le forum Struts 1
    Réponses: 7
    Dernier message: 02/11/2006, 11h03
  5. [Débutant]pb de fichier - array index out of bounds exception
    Par TheBlue dans le forum Collection et Stream
    Réponses: 5
    Dernier message: 12/06/2006, 20h24

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