Article View: fr.comp.lang.c
Article #15341Re: Tableau: diff�rence entre pile et tas
From: Yves ROMAN
Date: Thu, 29 Sep 2005 15:25
Date: Thu, 29 Sep 2005 15:25
135 lines
4959 bytes
4959 bytes
<dab@acri-st.fr> a �crit dans le message de news:
1128006219.590857.313500@g47g2000cwa.googlegroups.com...
> Du coup, je cale sur mon probl�me.
>
> J'alloue mes table dans mon main avec malloc.
> Je passe mes tables en param�tre d'une fonction.
> Je modifie la table distTable dans ma fonction.
> Au retour de ma fonction, je re�oie:
> segmentation fault (core dumped)
> quand j'essaye de lire le contenu de la table modifi�e.
>
> J'ai pass� la journ�e dessus sans succ�s:
> Si vous avez une id�e ou une suggestion, ne vous genez pas.
> Voici mon code:
> Le fichier main.c:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> # include "grid_point.h"
>
> int main(int argc, char* argv[]) {
> t_grid_point** gridPointTable = NULL;
Pour quoi un vecteur de pointeurs puisque tu n'utilise que gridPointTable[0]
(mais c'est peut-�tre uniquement pour le programme de test)
> t_sorted_grid_point** distTable = NULL;
Ici, tu d�finis un vecteur de pointeurs. Mais tu vas allouer une matrice.
D�finis plut�t
t_sorted_grid_point* distTable = NULL;
> int nbGridPoint = 0, inc, inc2;
>
> /* init data struct */
> if (DEBUG != 0)
> printf("Memory allocation\n");
>
> gridPointTable = malloc(NB_MAX_GRID_POINT*sizeof(t_grid_point *));
>
> if ((DEBUG != 0) & (gridPointTable == NULL))
> printf("Error during allocate: gridPointTable\n");
>
> distTable =
> malloc(NB_MAX_GRID_POINT*NB_NEIGHBOR*sizeof(t_sorted_grid_point));
et ici tu alloues la place pour une matrice d'�lements et non un vecteur de
pointeurs
> if ((DEBUG != 0) & (distTable == NULL))
> printf("Error during allocate: distTable\n");
>
> gridPointTable[nbGridPoint] = malloc(sizeof(t_grid_point));
> gridPointTable[nbGridPoint]->id = 271823;
> gridPointTable[nbGridPoint]->longitude = -0.051574;
> gridPointTable[nbGridPoint]->latitude = 1.003093;
> nbGridPoint++;
>
> if (DEBUG != 0)
> printf("end of read\n");
>
> /* compute neighbors */
> printf("gridPointTable=> Pointeur: %x, Objet point�: %x\n",
> gridPointTable, *gridPointTable);
> printf("distTable=> Pointeur: %x, Objet point�: %x\n", distTable,
> *distTable);
> computeNeighbors(gridPointTable, nbGridPoint, distTable);
Remarque: cela suppose que la fonction ne modifie pas nbGridPoint
> /* display distance table: ID*/
> printf("\nID neighbors: \n");
> for(inc=0; inc<nbGridPoint; inc++) {
> printf("Id:%u\t", gridPointTable[inc]->id);
> for(inc2=0; inc2<NB_NEIGHBOR; inc2++) {
> printf("%d\t", distTable[inc][inc2].id);
Avec la d�finition de distTable, le compilateur va g�rer �a comme un vecteur
de pointeurs. Mais tu ne l'a pas allou� ainsi (allocation de chaque ligne).
La formule pour l'utiliser comme matrice est :
printf("%d\t", distTable[inc*NB_NEIGHBOR+inc2].id);
> }
> printf("\n");
> }
>
> /* delete all grid point */
> for(inc=0; inc<nbGridPoint; inc++) {
> free(gridPointTable[inc]);
> }
>
> free(gridPointTable);
>
> free(distTable);
>
> closeGPFile();
>
> return 0;
> }
[...]
> Le fichier grid_point.c:
>
> #include "grid_point.h"
>
> void computeNeighbors(t_grid_point* gridPointTable[],
> int gridPointTableSize,
> t_sorted_grid_point distTable[][NB_NEIGHBOR]) {
Ici tu d�clares bien une matrice
>
> int inc, inc2;
>
> printf("gridPointTable=> Pointeur: %x, Objet point�: %x\n",
> gridPointTable, *gridPointTable);
> printf("distTable=> Pointeur: %x, Objet point�: %x\n", distTable,
> *distTable);
>
> /* init struct with 0 */
> for(inc=0; inc<NB_MAX_GRID_POINT; inc++) {
> for(inc2=0; inc2<NB_NEIGHBOR; inc2++) {
> distTable[inc][inc2].id = 0;
> distTable[inc][inc2].dist = 180;
Ici tu traites bien une matrice, mais la syntaxe serait la m�me pour une
vecteur de pointeurs
> }
> }
>
> }
Technical Details
Message-ID:
<dhh0u1$nn7$1@s1.news.oleane.net>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!novia!newsfeed.icl.net!proxad.net!proxad.net!oleane.net!oleane!not-for-mailReferences:
<1127923112.347881.79740@z14g2000cwz.googlegroups.com> <1128006219.590857.313500@g47g2000cwa.googlegroups.com>Show Full Headers
Complete Article Headers
X-Google-Thread: f9822,9ee84542aed426f5 X-Google-Attributes: gidf9822,public X-Google-Language: FRENCH,ASCII Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!novia!newsfeed.icl.net!proxad.net!proxad.net!oleane.net!oleane!not-for-mail From: "Yves ROMAN" <yves.roman@NO.unilog.SPAM.fr> Newsgroups: fr.comp.lang.c Subject: Re: Tableau: diff�rence entre pile et tas Date: Thu, 29 Sep 2005 17:25:23 +0200 Organization: Guest of France Telecom's news reading service Lines: 137 Message-ID: <dhh0u1$nn7$1@s1.news.oleane.net> References: <1127923112.347881.79740@z14g2000cwz.googlegroups.com> <1128006219.590857.313500@g47g2000cwa.googlegroups.com> NNTP-Posting-Host: cerbere.unilog.fr X-Trace: s1.news.oleane.net 1128007425 24295 194.3.185.2 (29 Sep 2005 15:23:45 GMT) X-Complaints-To: abuse@oleane.net NNTP-Posting-Date: Thu, 29 Sep 2005 15:23:45 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original