🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Tableau: différence entre pile et tas

#15420
From: Pierre Maurette
Date: Wed, 28 Sep 2005 17:38
93 lines
4207 bytes
dab@acri-st.fr, le 28/09/2005, a �crit :
> Bonjour � tous
>
> J'ai un bout de code qui utilise des tables:
> #define NB_MAX_GRID_POINT 2621442
> #define NB_NEIGHBOR 6
>
> t_grid_point* gridPointTable[NB_MAX_GRID_POINT];
> t_sorted_grid_point distTable[NB_MAX_GRID_POINT][NB_NEIGHBOR];
>
> Pour commencer � developper, j'avais une valeur de 50 pour
> NB_MAX_GRID_POINT,
> mais maintenant que j'utilise la v�ritable valeur de
> NB_MAX_GRID_POINT,
> je me prend un segmentation fault � l'execution.
>
> Du coup, je me dis que c'est la taille de mon tableau en pile qui pose
> soucis,
> je vais donc allouer mon tableau dans le tas.
>
> et hop:
> gridPointTable = malloc(NB_MAX_GRID_POINT*sizeof(void *));
>
> distTable = malloc(NB_MAX_GRID_POINT*sizeof(void *));
> for(inc=0; inc<NB_MAX_GRID_POINT; inc++) {
> 	distTable[inc] = malloc(NB_NEIGHBOR*sizeof(t_sorted_grid_point));
> }
>
> Est-ce que je suis cens� utiliser mes tableaux diff�rements
> maintenant?
> Qu'est-ce que je dois prendre comme pr�caution suppl�mentaire?
Bien entendu, particuli�rement dans votre cas, tester *chaque* retour 
de malloc().

Pourquoi sizeof(void *) et non sizeof(t_grid_point*) ?

Ici, distTable n'est pas un tableau de dimension 2, mais un 
t_sorted_grid_point**, ou un tableau de dimension 1 de 
t_sorted_grid_point*. La diff�rence, c'est essentiellement que les 
"lignes" ne se suivent pas n�cessairement comme dans un tableau de 
dimension 2. En gardant ce point � l'esprit (arithm�tique des 
pointeurs, copie par transfert m�moire) �a devrait fonctionner.

On pourrait penser � un tableau � une dimension de taille
NB_MAX_GRID_POINT * NB_NEIGHBOR
et un acc�s genre
tab[i * NB_MAX_GRID_POINT + j]
Mais �a ne r�pond pas � votre question et peut �tre plus lent (quoi 
que..)

Mais � mon avis, pour travailler tranquille sur un vrai tableau, vous 
pouvez utiliser des typedef:

#define TAB_L 10
#define TAB_C 6

typedef int MonTab[TAB_L][TAB_C];
typedef MonTab* pMonTab;

int main(void)
{

    int i, j;

	pMonTab ptab = malloc(TAB_L * TAB_C * sizeof(int));
    #define TAB *ptab

    if(ptab != NULL)
	{
	for(i = 0; i < TAB_L; i++)
		for(j = 0; j < TAB_C; j++)
			TAB[i][j] = i + j;

	for(i = 0; i < TAB_L; i++)
		for(j = 0; j < TAB_C; j++)
			printf("%d\n", TAB[i][j]);

	}
	return 0;
}





Note - C'est dommage que vous donniez des exemples incomplets mais 
n�anmoins encombr�s du contexte de votre application. Pourquoi ne pas 
poster soit un code minimal (avec des tableaux de int par exemple), 
soit un code r�el compilable. C'est une remarque personnelle, j'aime 
bien jouer avec le code post�.

-- 
Pierre Maurette
Technical Details
Message-ID: <mn.e49a7d59f0935c1a.31483@laposte.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!nx01.iad01.newshosting.com!newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.icl.net!proxad.net!infeed-4.proxad.net!news18-e.free.fr!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!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.icl.net!proxad.net!infeed-4.proxad.net!news18-e.free.fr!not-for-mail
Subject: Re: Tableau: différence entre pile et tas
From: "Pierre Maurette" <maurettepierre@wanadoo.fr>
References: <1127923112.347881.79740@z14g2000cwz.googlegroups.com>
Newsgroups: fr.comp.lang.c
Message-ID: <mn.e49a7d59f0935c1a.31483@laposte.net>
X-Newsreader: MesNews/1.04.00.00
Date: Wed, 28 Sep 2005 19:38:23 +0200
MIME-Version: 1.0
X-Face: cmi$3R1_J{[}5K|8d'LDiJ$SsZ*gn,j*PhJ#KVGt+45,E_N9O}GrhhQ>fb&q<&lz=g%PEv' u"m)N|KrBMux?2:E?N7Uw3L#H>L#GwM}<qH-?#_Y$B9]$q1"~iUGtN#HVy?}7.`m&iKF>gY:.`W.qu z`1'W'h-AXPmB]#7~<+@H.<0M`-jBkCWbmkcT_*LBVD3|p1a?h1H;cHq{$-vbeP@Hdcqp5z~lwU7ZK `~
Reply-To: maurettepierre@wanadoo.fr
Content-Type: text/plain; charset="iso-8859-15"; format=flowed
Content-Transfer-Encoding: 8bit
X-Antivirus: avast! (VPS 0539-1, 27/09/2005), Outbound message
X-Antivirus-Status: Clean
Lines: 95
Organization: Guest of ProXad - France
NNTP-Posting-Date: 28 Sep 2005 19:36:59 MEST
NNTP-Posting-Host: 84.7.237.133
X-Trace: 1127929019 news18-e.free.fr 4354 84.7.237.133:3255
X-Complaints-To: abuse@proxad.net