🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: fr.comp.lang.c
Article #15340

Re: Tableau: différence entre pile et tas

#15340
From: dab
Date: Thu, 29 Sep 2005 15:03
139 lines
4661 bytes
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;
	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));

	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);

	/* 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);
		}
		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.h:

#ifndef GRID_POINT_H
#define GRID_POINT_H

#define NB_NEIGHBOR 6

#define NB_MAX_GRID_POINT 50

typedef struct {
	unsigned long id;
	float longitude, latitude;
	char flag;
	float dist;
	unsigned long Neighbor1, Neighbor2, Neighbor3, Neighbor4, Neighbor5;
} t_grid_point;

typedef struct {
	unsigned long id;
	float dist;
} t_sorted_grid_point;

void computeNeighbors(t_grid_point* gridPointTable[],
			int gridPointTableSize,
			t_sorted_grid_point distTable[][NB_NEIGHBOR]);

#endif

#include <stdio.h>
#include <math.h>

Le fichier grid_point.c:

#include "grid_point.h"

void computeNeighbors(t_grid_point* gridPointTable[],
			int gridPointTableSize,
			t_sorted_grid_point distTable[][NB_NEIGHBOR]) {

	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;
		}
	}
	
}


Merci d'avance

David
Technical Details
Message-ID: <1128006219.590857.313500@g47g2000cwa.googlegroups.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!g47g2000cwa.googlegroups.com!not-for-mail
References: <1127923112.347881.79740@z14g2000cwz.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!postnews.google.com!g47g2000cwa.googlegroups.com!not-for-mail
From: dab@acri-st.fr
Newsgroups: fr.comp.lang.c
Subject: Re: Tableau: différence entre pile et tas
Date: 29 Sep 2005 08:03:39 -0700
Organization: http://groups.google.com
Lines: 140
Message-ID: <1128006219.590857.313500@g47g2000cwa.googlegroups.com>
References: <1127923112.347881.79740@z14g2000cwz.googlegroups.com>
NNTP-Posting-Host: 213.244.27.2
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1128006225 17288 127.0.0.1 (29 Sep 2005 15:03:45 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Thu, 29 Sep 2005 15:03:45 +0000 (UTC)
In-Reply-To: <1127923112.347881.79740@z14g2000cwz.googlegroups.com>
User-Agent: G2/0.2
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.0; fr-FR; rv:1.7.10) Gecko/20050717 Firefox/1.0.6,gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: g47g2000cwa.googlegroups.com; posting-host=213.244.27.2;
   posting-account=nfO__g0AAAB3e6qRD0Nckfeo-0DvYDH1