memcpy et stl incompatibles ?
Bonjour a tous,
Je viens vous embeter avec une petite colle que je n'ai toujours pas resolue. Il y a sans doute une erreur grosse comme une maison, mais impossible de la trouver...
Il ne s'agit pas d'utiliser memcpy pour recopier des structures stl, ou des choses compliquees. Le probleme se pose lors de l'appel d'une methode stl sur un tableau recopie en utilisant la methode memcpy.
Voici les deux fichiers composant mon projet :
Photon.h : une classe simple contenant 3 flottants
Code:
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
|
#ifndef PHOTON_H
#define PHOTON_H
class Photon
{
public :
float position[3];
float direction[3];
float power[3];
float &x;
float &y;
float &z;
Photon() : x(position[0]), y(position[1]), z(position[2])
{
position[0] = 0.0f;
position[1] = 0.0f;
position[2] = 0.0f;
}
Photon(Photon& p) : x(position[0]), y(position[1]), z(position[2])
{
position[0] = p.x;
position[1] = p.y;
position[2] = p.z;
}
Photon& operator=(const Photon& p)
{
position[0] = p.x;
position[1] = p.y;
position[2] = p.z;
return *this;
}
};
#endif // PHOTON_H |
main.c : le code qui me pose une colle
Code:
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include <iostream>
#include <algorithm>
#include <functional>
#include "Photon.h"
using namespace std;
#define ARRAY_SIZE 10
template <class T> class CompareAlongDimension : public std::binary_function<T*, T*, bool>
{
public:
int dimension;
CompareAlongDimension( int d ) : dimension(d) {}
bool operator()(T const & A, T const & B) { return A.position[dimension] < B.position[dimension]; }
} ;
void showPhotonArray(Photon* array, int axis){
for(unsigned int i = 0; i < ARRAY_SIZE; i++)
{
cout << array[i].position[axis] << " ";
}
cout << endl;
}
int main(int argc, char** argv)
{
Photon* array = new Photon[ARRAY_SIZE];
Photon* copy = new Photon[ARRAY_SIZE];
for(unsigned int i=0; i<ARRAY_SIZE; i++)
{
array[i].x = (float)rand()/(RAND_MAX+1);
array[i].y = (float)rand()/(RAND_MAX+1);
array[i].z = (float)rand()/(RAND_MAX+1);
}
//memcpy(copy, array, sizeof(Photon)*ARRAY_SIZE);
for(unsigned int i=0; i<ARRAY_SIZE; i++)
{
copy[i] = array[i];
}
cout << "Tirage Aleatoire original : " << endl;
showPhotonArray(array, 0);
sort(array, array+ARRAY_SIZE, CompareAlongDimension<Photon>(0) );
cout << "Tirage Aleatoire trie : " << endl;
showPhotonArray(array, 0);
cout << endl;
cout << "Copie du tirage originale : " << endl;
showPhotonArray(copy, 0);
sort(copy, copy+ARRAY_SIZE, CompareAlongDimension<Photon>(0) );
cout << "Copie du tirage triee: " << endl;
showPhotonArray(copy, 0);
return 0;
} |
L'application cree 10 Photons aleatoires, puis les reorganise en fonction de leur coordonnee suivant l'axe X. J'utilise pour cela stl::sort. Hors, si je copie le tirage aleatoire original, puis cherche a le trier de la meme facon, des choses magiques se passent...
Si j'utilise une boucle for pour faire la recopie du tableau, j'obtiens la sortie logique :
Code:
1 2 3 4 5 6 7 8 9
| Tirage Aleatoire original :
0.00125122 0.808716 0.350281 0.746582 0.71048 0.0149841 0.147308 0.445679 0.00891113 0.571167
Tirage Aleatoire trie :
0.00125122 0.00891113 0.0149841 0.147308 0.350281 0.445679 0.571167 0.71048 0.746582 0.808716
Copie du tirage originale :
0.00125122 0.808716 0.350281 0.746582 0.71048 0.0149841 0.147308 0.445679 0.00891113 0.571167
Copie du tirage triee:
0.00125122 0.00891113 0.0149841 0.147308 0.350281 0.445679 0.571167 0.71048 0.746582 0.808716 |
Mais si j'utilise memcpy pour faire la recopie... le tri est fait n'importe comment :
Code:
1 2 3 4 5 6 7 8 9
| Tirage Aleatoire original :
0.00125122 0.808716 0.350281 0.746582 0.71048 0.0149841 0.147308 0.445679 0.00891113 0.571167
Tirage Aleatoire trie :
0.00125122 0.00891113 0.0149841 0.147308 0.350281 0.445679 0.571167 0.71048 0.746582 0.808716
Copie du tirage originale :
0.00125122 0.808716 0.350281 0.746582 0.71048 0.0149841 0.147308 0.445679 0.00891113 0.571167
Copie du tirage triee:
0.00125122 0.0149841 0.00891113 0.746582 0.147308 0.350281 0.445679 0.571167 0.808716 0.746582 |
Je n'arrive pas du tout a voir de quoi cela pourrait venir... Pouvez vous m'aider ?
Merci !
Tan'