 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Laurent Hamery Guest
|
Posted: Wed May 05, 2004 12:06 am Post subject: tableau dynamique...?? |
|
|
bonjour,
je suis pas encore tres doue avec les pointers et la je bloque completement
je voudrais que ma function retourne un tableau de int dont la taille n`est
pas declaree explicitement.
mais apres le realloc je ne comprends plus du tt ce qui se passe .. si qqun
pouvait m`expliquer
voici mon code :
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;
ifstream fileToRead;
int i;
int * findnLoop(const char * description) {
cout << "nLoop in : " << endl;
int iStart = 23;
int iLoop = 0;
char tmpChar;
int nLoopTmp;
int* findnLoopPtr = (int*)malloc(sizeof(int)*0); //est ce bien propre ??
char* tmpCharPtr = 0;
while(description[iStart] != ' ') {
if(description[iStart] == ']') break;
else if(description[iStart] != ' ') {
tmpChar = description[iStart];
tmpCharPtr = &tmpChar;
nLoopTmp = atoi(tmpCharPtr);
cout << "nLoopTmp : " << nLoopTmp << endl;
findnLoopPtr = (int *) realloc(findnLoopPtr,
sizeof(char)*(iLoop+1));
findnLoopPtr[iLoop] = nLoopTmp;
iLoop++;
}
iStart++;
}
for(int iLoop2=0; iLoop2
findnLoopPtr[iLoop2] << endl;
return findnLoopPtr;
}
main() {
char *polyDescription = "PointsGeneralPolygons [1 3 1 5 9 5 8 7] [4 4 4
4 4 4]";
findnLoop(polyDescription);
return 0;
}
a la premiere sortie j`ai bien nLoopTmp qui prend successivement les valeurs
1 3 1 5 9 5 8 7
mais apres le realloc j`ai findnLoopPtr[iLoop2] qui prends 4 mauvaises
valeurs (ss doute des adresses) et puis 9 5 8 7
pourriez vous m`expliquer tt ce qui ne va pas la dedans .... ???
|
|
| Back to top |
|
 |
Pierre Maurette Guest
|
Posted: Wed May 05, 2004 5:18 am Post subject: Re: tableau dynamique...?? |
|
|
"Laurent Hamery" <lhamery (AT) yahoo (DOT) fr> typa:
| Quote: | bonjour,
Bonjour,
findnLoopPtr = (int *) realloc(findnLoopPtr,
sizeof(char)*(iLoop+1));
|
findnLoopPtr = (int *) realloc(findnLoopPtr, sizeof(int)*(iLoop+1));
(int à la place de char) Ça dexvrait rouler.
D'après la norme, votre malloc(0) est correct, puisque le résultat est
défini par l'implémentation mais avec choix entre NULL et un pointeur
valide. Et realloc(NULL, ..) se comporte comme un alloc(..).
Mais pourquoi pas tout simplement:
int* ptr = NULL;
....
if(ptr == NULL) alloc();
else realloc();
Votre problème, c'est du C. Dans ces cas-là, le mieux pour la paix des
ménages n'est-il pas de le maquiller en code C et de poster sur
fr.comp.lang.c ?
--
Pierre
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|