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

 Delphi Discussion :

Problème de violation d'accès avec Array


Sujet :

Delphi

  1. #1
    Membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juillet 2009
    Messages
    56
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2009
    Messages : 56
    Points : 44
    Points
    44
    Par défaut Problème de violation d'accès avec Array
    Bonjour,

    J'ai un souci dans mon programme et je n'arrive pas à utiliser
    les tableaux (arrays) car j'ai une erreur sur violation d'accès
    à chaque fois que je veux dimensionner Ptab1^.

    J'ai utilisé SetLength(Ptab1^,0) pour initialiser, cela ne marche pas,
    idem quand j'essaie de remplir les champs dans un array.

    Voici mon code :

    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
     
     
     
    Ttab1 = record
    	id_PTxt: string;
        id_DefIndex: string;
        sDefIndex: string;
        id_SubIndex: string;
        sSubIndex: string;
        valSubIndex: string;
    end;
     
    PPTxt = ^Ttab1;
    TabIPTxt = Array of Ttab1;
    PPTxts = ^TabIPTxt;
     
    var
      sValEnv,sTxtEnvoi,sValEnv2,sTxtEnvoi2,elt,idPTxt,parSel:string;
      iPT,CptPT:integer;
      Ptab1: PPTxts;
     
    Begin
    CptPT := 0;
    Repeat
        With Ptab1^[CptPT+1] Do
        Begin
    		id_PTxt:=form1.StringGrid5.cells[0,iPT];
            id_DefIndex:=form1.StringGrid5.cells[1,iPT];
            sDefIndex:=form1.StringGrid5.cells[2,iPT];
            id_SubIndex:=form1.StringGrid5.cells[1,iPT];
            sSubIndex:=form1.StringGrid5.cells[1,iPT];
            alSubIndex:=form1.StringGrid5.cells[1,iPT];
        End;
     
        elt := stringgrid5.cells[6,iPT]+' : '+stringgrid5.cells[5,iPT];
        form2.ComboBox1.AddItem(elt,Sender);
        inc(iPT,1);
        inc(CptPT,1);
    Until(form1.stringgrid5.cells[0,iPT]<>idPtab1);
    L'erreur intervient à la ligne "With Ptab1^[CptPT+1] Do"

    Pouvez-vous m'apporter votre aide svp ?

    Merci.

  2. #2
    Modérateur
    Avatar de tourlourou
    Homme Profil pro
    Biologiste ; Progr(amateur)
    Inscrit en
    Mars 2005
    Messages
    3 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Biologiste ; Progr(amateur)

    Informations forums :
    Inscription : Mars 2005
    Messages : 3 858
    Points : 11 301
    Points
    11 301
    Billets dans le blog
    6
    Par défaut
    J'ai utilisé SetLength(Ptab1^,0) pour initialiser
    il est préférable d'initialiser à la taille voulue...

    Les tableaux dynamiques sont 0-based, dimensionnés par SetLength, effectivement.

    Quel est le besoin d'avoir un pointeur sur tableau ? Un tableau dynamique est déjà un pointeur en soi :
    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
     
    Ttab1 = record
    	id_PTxt: string;
        id_DefIndex: string;
        sDefIndex: string;
        id_SubIndex: string;
        sSubIndex: string;
        valSubIndex: string;
    end;
     
    PPTxt = ^Ttab1;
    TabIPTxt = Array of Ttab1;
    
     
    var
      sValEnv,sTxtEnvoi,sValEnv2,sTxtEnvoi2,elt,idPTxt,parSel:string;
      iPT,CptPT:integer;
      Tab1: TabIPTxt; 
    Begin
    SetLength(Tab1, MaxLines);  
    CptPT := 0;
    Repeat
        With Tab1[CptPT+1] Do
        Begin
    		id_PTxt:=form1.StringGrid5.cells[0,iPT];
            id_DefIndex:=form1.StringGrid5.cells[1,iPT];
            sDefIndex:=form1.StringGrid5.cells[2,iPT];
            id_SubIndex:=form1.StringGrid5.cells[1,iPT];
            sSubIndex:=form1.StringGrid5.cells[1,iPT];
            alSubIndex:=form1.StringGrid5.cells[1,iPT];
        End;
     
        elt := stringgrid5.cells[6,iPT]+' : '+stringgrid5.cells[5,iPT];
        form2.ComboBox1.AddItem(elt,Sender);
        inc(iPT,1);
        inc(CptPT,1);
    Until(form1.stringgrid5.cells[0,iPT]<>idPtab1) or (CptPT+1>MaxLines-1); 
    Delphi 5 Pro - Delphi 11.3 Alexandria Community Edition - CodeTyphon 6.90 sous Windows 10 ; CT 6.40 sous Ubuntu 18.04 (VM)
    . Ignorer la FAQ Delphi et les Cours et Tutoriels Delphi nuit gravement à notre code !

  3. #3
    Membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juillet 2009
    Messages
    56
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2009
    Messages : 56
    Points : 44
    Points
    44
    Par défaut
    Salut Tourlourou,

    Super, ton code fonctionne très bien
    Merci beaucoup pour ta réactivité, j'apprends
    et j'ai perdu une journée à chercher.

    Le pointeur n'était pas forcément requis, je voulais
    juste récupérer une base sous forme de tableau.

    Résolu.

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

Discussions similaires

  1. problème de violation d'accès avec createprocess
    Par sevyc64 dans le forum API, COM et SDKs
    Réponses: 5
    Dernier message: 01/04/2014, 14h37
  2. Réponses: 2
    Dernier message: 17/12/2008, 22h30
  3. [vb 2005]Problème de violation d'accès concurentiel
    Par estelledany dans le forum Windows Forms
    Réponses: 3
    Dernier message: 14/06/2006, 17h14
  4. Violation d'accès avec les composants Word 97/ 2000
    Par edechaux dans le forum Composants VCL
    Réponses: 3
    Dernier message: 07/03/2006, 09h48
  5. Problème de violation d'accès
    Par Oluha dans le forum Bases de données
    Réponses: 11
    Dernier message: 31/05/2005, 10h26

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