TrizoLakai
29/08/2005, 10h59
J'ai fais un fichier test que j'ai compilé pour essayé l'allocation dynamique d'un tableau à 2 dimensions. Le voici :
#include <iostream>
using namespace std;
int main(){
FILE *carte;
unsigned int **map;
unsigned int taille_x;
unsigned int taille_y;
unsigned int tableau[10][10];
/* Chargement de la carte */
carte = fopen("../../DATA/map/map1.dat", "r");
fscanf(carte, "%d %d\n", &taille_x, &taille_y);
/* Allocation dynamique du tableau map */
map = new unsigned int*;
for(int i = 0; i < taille_x; ++i)
map[i] = new unsigned int[taille_y];
for(int i = 0; i < taille_y; ++i){
for(int j = 0; j < taille_x; ++j){
fscanf(carte, "%d ", &map[j][i]);
tableau[j][i] = map[j][i];
}
}
fclose(carte);
/* Affichage de la carte */
for(int j = 0; j < taille_y; ++j){
for(int i = 0; i < taille_x; ++i){
cout << map[i][j] << " ";
}
cout << endl;
}
system("Pause");
delete map;
for(int i = 0; i < taille_x; ++i)
delete[] map[i];
return 0;
}
Ce code fonctionne parfaitement. Mais quand je l'implente à mon mini projet de RPG. Il y a une erreur. J'ai regardé à partir de quand. Et cette erreur survien lorsque l'on essais de mettre un nombre dans le tableau au dessus de map[4][0]. Je ne comprend pas pourquoi. Voici le code :
void LoadMap(SDL_Rect &Rect_Placement){
FILE *carte;
carte = fopen("./DATA/map/map1.dat", "r");
fscanf(carte, "%d %d\n", &taille_x_map, &taille_y_map);
map = new unsigned int*;
for(int i = 0; i < taille_x_map; ++i)
map[i] = new unsigned int[taille_y_map];
for(int j = 0; j < taille_y_map; ++j){
for(int i = 0; i < taille_x_map; ++i){
map[i][j] = 0;
}
}
fclose(carte);
carte = fopen("./DATA/map/map1d.dat", "r");
for(int j = 0; j < taille_y_map; ++j){
for(int i = 0; i < taille_x_map; ++i){
fscanf(carte, "%d ", &donnee[i][j]);
}
}
fclose(carte);
for(int j = 0; j < taille_y_map; ++j){
for(int i = 0; i < taille_x_map; ++i){
if(donnee[i][j]==2){
Rect_Placement.x = 4+i*30;
Rect_Placement.y = j*32-30;
}
}
}
}
Voici la ligne qui l'appel dans (dans une source séparée) :
LoadMap(Rect_Placement);
Merci pour votre aide. Je suis vraiment bloqué et je ne comprend pas d'où viens le problème :00000032:
Merci encore.
#include <iostream>
using namespace std;
int main(){
FILE *carte;
unsigned int **map;
unsigned int taille_x;
unsigned int taille_y;
unsigned int tableau[10][10];
/* Chargement de la carte */
carte = fopen("../../DATA/map/map1.dat", "r");
fscanf(carte, "%d %d\n", &taille_x, &taille_y);
/* Allocation dynamique du tableau map */
map = new unsigned int*;
for(int i = 0; i < taille_x; ++i)
map[i] = new unsigned int[taille_y];
for(int i = 0; i < taille_y; ++i){
for(int j = 0; j < taille_x; ++j){
fscanf(carte, "%d ", &map[j][i]);
tableau[j][i] = map[j][i];
}
}
fclose(carte);
/* Affichage de la carte */
for(int j = 0; j < taille_y; ++j){
for(int i = 0; i < taille_x; ++i){
cout << map[i][j] << " ";
}
cout << endl;
}
system("Pause");
delete map;
for(int i = 0; i < taille_x; ++i)
delete[] map[i];
return 0;
}
Ce code fonctionne parfaitement. Mais quand je l'implente à mon mini projet de RPG. Il y a une erreur. J'ai regardé à partir de quand. Et cette erreur survien lorsque l'on essais de mettre un nombre dans le tableau au dessus de map[4][0]. Je ne comprend pas pourquoi. Voici le code :
void LoadMap(SDL_Rect &Rect_Placement){
FILE *carte;
carte = fopen("./DATA/map/map1.dat", "r");
fscanf(carte, "%d %d\n", &taille_x_map, &taille_y_map);
map = new unsigned int*;
for(int i = 0; i < taille_x_map; ++i)
map[i] = new unsigned int[taille_y_map];
for(int j = 0; j < taille_y_map; ++j){
for(int i = 0; i < taille_x_map; ++i){
map[i][j] = 0;
}
}
fclose(carte);
carte = fopen("./DATA/map/map1d.dat", "r");
for(int j = 0; j < taille_y_map; ++j){
for(int i = 0; i < taille_x_map; ++i){
fscanf(carte, "%d ", &donnee[i][j]);
}
}
fclose(carte);
for(int j = 0; j < taille_y_map; ++j){
for(int i = 0; i < taille_x_map; ++i){
if(donnee[i][j]==2){
Rect_Placement.x = 4+i*30;
Rect_Placement.y = j*32-30;
}
}
}
}
Voici la ligne qui l'appel dans (dans une source séparée) :
LoadMap(Rect_Placement);
Merci pour votre aide. Je suis vraiment bloqué et je ne comprend pas d'où viens le problème :00000032:
Merci encore.