[英]Segmentation fault when commenting 2 free (Sokoban game)
I've been trying to solve a segmentation fault since yesterday, it's driving me crazy... So I've got this program that I use to implement the sokoban game, in which a map is given, and a plan like "NSEW" is given, meaning that I want the player to move north, then south, then etc... on the map given.从昨天开始我一直在尝试解决分段错误,这让我发疯了......所以我有这个程序来实现推箱子游戏,其中给出了地图,以及像“NSEW”这样的计划是给定的,这意味着我希望玩家在给定的地图上向北移动,然后向南移动,然后等等。
map* replay(map* map_loaded, int length, char* plan){
map* new_map=move(map_loaded,plan[0]);
map* old_map=deep_copy(new_map);
for (int i=1 ; i<length ; i++){
free(new_map->p_char);
free(new_map);
new_map=move(old_map,plan[i]);
free(old_map->p_char);
free(old_map);
old_map=deep_copy(new_map);
}
free(old_map->p_char);
free(old_map);
return new_map;
}
Map is a structure defined as following : Map 是一个定义如下的结构:
typedef struct map map;
struct map{
int width;
int height;
char* p_char;
};
The move function does one movement ; move 函数做一个动作;
The deep_copy makes a deep copy of a map struct : deep_copy 制作地图结构的深层副本:
map* deep_copy(map* map_loaded){
map* new_map=malloc(sizeof(map));
char* p_array=map_loaded->p_char;
int width=map_loaded->width;
int height=map_loaded->height;
new_map->width=width;
new_map->height=height;
char* p_new=malloc(sizeof(char)*width*height);
for (int i=0 ; i<width*height ; i++){
p_new[i]=p_array[i];
}
new_map->p_char=p_new;
return(new_map);
}
And what happens is that if I leave the code like that, if the length chosen ( the number of movements I want the player to do) is too high (depends on the map and the movements), when executing a replay.c program :会发生的情况是,如果我保留这样的代码,如果选择的长度(我希望玩家做的动作次数)太高(取决于地图和动作),则在执行 replay.c 程序时:
int main(int argc, char *argv[]){
if (argc != 4) {
fprintf(stderr, "You must provide a file name or enough elements!\n");
exit(EXIT_FAILURE);
}
map* map_loaded=load(argv[1]);
int length=atoi(argv[2]);
char* plan=argv[3];
map* new_map=replay(map_loaded,length,plan);
print_map(new_map);
free(new_map->p_char);
free(new_map);
free(map_loaded->p_char);
free(map_loaded);
return 1;
}
I get a segmentation fault... But if I do :我遇到了分段错误...但如果我这样做:
//free(old_map->p_char);
//free(old_map);
Everything works perfect !!一切正常! I really don't understand why... The problem is if I do those free valgrind tells me I don't have as many free as allocs, which is normal...我真的不明白为什么......问题是如果我做那些免费的valgrind告诉我我没有alloc那么多的免费,这是正常的......
I would really appreciate any help given... Thanks in advance if you've been brave enough to read me until this point !我真的很感激所提供的任何帮助......如果你有足够的勇气阅读我直到这一点,在此先感谢!
Edit : Here is my move function ;编辑:这是我的移动功能;
map* move(map* map_loaded, char dir){
int pos=Position(map_loaded);
int width=map_loaded->width;
map* new_map=deep_copy(map_loaded);
char* p_new_array=new_map->p_char;
switch(dir){
case 'N':
if (p_new_array[pos-width]=='#'){ // Si il y a un mur au dessus
return(map_loaded);
}
if ((p_new_array[pos-width]=='$' && p_new_array[pos-2*width]=='$')
||(p_new_array[pos-width]=='$' && p_new_array[pos-2*width]=='*')
||(p_new_array[pos-width]=='*' && p_new_array[pos-2*width]=='$')
||(p_new_array[pos-width]=='*' && p_new_array[pos-2*width]=='*')){ //s'il y a 2 caisses au dessus
return(map_loaded);
}
if ((p_new_array[pos-width]=='$' && p_new_array[pos-2*width]=='#') //s'il y a une caisse au niveau -1 et un mur au niveau -2
||(p_new_array[pos-width]=='*' && p_new_array[pos-2*width]=='#')){
return(map_loaded);
}
// On vérifie d'abord s'il y a une caisse à déplacer
if (p_new_array[pos-width]=='$' || p_new_array[pos-width]=='*'){
if (p_new_array[pos-2*width]=='.'){
p_new_array[pos-2*width]='*';
}
else {
p_new_array[pos-2*width]='$';
}
if (p_new_array[pos-width]=='*'){
p_new_array[pos-width]='.';
}
else{
p_new_array[pos-width]=' ';
}
}
//On change le char en position du joueur
if (p_new_array[pos]=='+'){
p_new_array[pos-width]='@';
p_new_array[pos]='.';
}
else if (p_new_array[pos-width]=='.'){
p_new_array[pos-width]='+';
p_new_array[pos]=' ';
}
else {
p_new_array[pos-width]='@';
p_new_array[pos]=' ';
}
break;
case 'S':
if (p_new_array[pos+width]=='#'){ // Si il y a un mur en dessous
return(map_loaded);
}
if ((p_new_array[pos+width]=='$' && p_new_array[pos+2*width]=='$')
||(p_new_array[pos+width]=='$' && p_new_array[pos+2*width]=='*')
||(p_new_array[pos+width]=='*' && p_new_array[pos+2*width]=='$')
||(p_new_array[pos+width]=='*' && p_new_array[pos+2*width]=='*')){//s'il y a 2 caisses au dessus
return(map_loaded);
}
if ((p_new_array[pos+width]=='$' && p_new_array[pos+2*width]=='#') //s'il y a une caisse au niveau +1 et un mur au niveau +2
||(p_new_array[pos+width]=='*' && p_new_array[pos+2*width]=='#')){
return(map_loaded);
}
// On vérifie d'abord s'il y a une caisse à déplacer
if (p_new_array[pos+width]=='$' || p_new_array[pos+width]=='*'){
if (p_new_array[pos+2*width]=='.'){
p_new_array[pos+2*width]='*';
}
else {
p_new_array[pos+2*width]='$';
}
if (p_new_array[pos+width]=='*'){
p_new_array[pos+width]='.';
}
else{
p_new_array[pos+width]=' ';
}
}
//On change le char en position du joueur
if (p_new_array[pos]=='+'){
p_new_array[pos+width]='@';
p_new_array[pos]='.';
}
else if (p_new_array[pos+width]=='.'){
p_new_array[pos+width]='+';
p_new_array[pos]=' ';
}
else {
p_new_array[pos+width]='@';
p_new_array[pos]=' ';
}
break;
case 'W':
if (p_new_array[pos-1]=='#'){ // Si il y a un mur en dessous
return(map_loaded);
}
if ((p_new_array[pos-1]=='$' && p_new_array[pos-2]=='$')
||(p_new_array[pos-1]=='$' && p_new_array[pos-2]=='*')
||(p_new_array[pos-1]=='*' && p_new_array[pos-2]=='$')
||(p_new_array[pos-1]=='*' && p_new_array[pos-2]=='*')){ //s'il y a 2 caisses à gauche
return(map_loaded);
}
if ((p_new_array[pos-1]=='$' && p_new_array[pos-2]=='#') //s'il y a une caisse au niveau -1 et un mur au niveau -2
||(p_new_array[pos-1]=='*' && p_new_array[pos-2]=='#')){
return(map_loaded);
}
// On vérifie d'abord s'il y a une caisse à déplacer
if (p_new_array[pos-1]=='$' || p_new_array[pos-1]=='*'){
if (p_new_array[pos-2]=='.'){
p_new_array[pos-2]='*';
}
else {
p_new_array[pos-2]='$';
}
if (p_new_array[pos-1]=='*'){
p_new_array[pos-1]='.';
}
else{
p_new_array[pos-1]=' ';
}
}
//On change le char en position du joueur
if (p_new_array[pos]=='+'){
p_new_array[pos-1]='@';
p_new_array[pos]='.';
}
else if (p_new_array[pos-1]=='.'){
p_new_array[pos-1]='+';
p_new_array[pos]=' ';
}
else {
p_new_array[pos-1]='@';
p_new_array[pos]=' ';
}
break;
case 'E':
if (p_new_array[pos+1]=='#') {// Si il y a un mur à droite
return(map_loaded);
}
if ((p_new_array[pos+1]=='$' && p_new_array[pos+2]=='$')
||(p_new_array[pos+1]=='$' && p_new_array[pos+2]=='*')
||(p_new_array[pos+1]=='*' && p_new_array[pos+2]=='$')
||(p_new_array[pos+1]=='*' && p_new_array[pos+2]=='*')){ //s'il y a 2 caisses à droite
return(map_loaded);
}
if ((p_new_array[pos+1]=='$' && p_new_array[pos+2]=='#') //s'il y a une caisse au niveau +1 et un mur au niveau +2
||(p_new_array[pos+1]=='*' && p_new_array[pos+2]=='#')){
return(map_loaded);
}
// On vérifie d'abord s'il y a une caisse à déplacer
if (p_new_array[pos+1]=='$' || p_new_array[pos+1]=='*'){
if (p_new_array[pos+2]=='.'){
p_new_array[pos+2]='*';
}
else {
p_new_array[pos+2]='$';
}
if (p_new_array[pos+1]=='*'){
p_new_array[pos+1]='.';
}
else{
p_new_array[pos+1]=' ';
}
}
//On change le char en position du joueur
if (p_new_array[pos]=='+'){
p_new_array[pos+1]='@';
p_new_array[pos]='.';
}
else if (p_new_array[pos+1]=='.'){
p_new_array[pos+1]='+';
p_new_array[pos]=' ';
}
else {
p_new_array[pos+1]='@';
p_new_array[pos]=' ';
}
break;
}
return(new_map);
}
And here is the load function ;这是加载函数;
char* Array_Creator(char* filename){
FILE* p_file = fopen(filename, "r");
char* p_array = NULL;
if (p_file == NULL) {
exit(EXIT_FAILURE);
}
else{
size_t size=1;
int c;
while (getc(p_file)!=EOF) {
size++;
}
fseek(p_file,0,SEEK_SET);
while ((c=getc(p_file))!='\n' && c!=EOF) { //on se débarasse de la première ligne
size--;
}
p_array=(char*)malloc(sizeof(char)*size);
if (p_array!=NULL) { //si jamais le malloc ne fonctionne pas
for(size_t i=0; i<size-1; i++) {
p_array[i]=(char)getc(p_file);
if (p_array[i] == '\n') { // si le caractère est une nouvelle ligne, on s'en sépare
i--; // on ajuste alors la taille et l'indice
size--;
}
}
p_array[size-1]='\0';
}
fclose(p_file);
}
return p_array;
}
//La fonction Dimensions permet de récupérer les dimensions d'une map
couple Dimensions(char *p_char){
FILE *p_file = NULL;
p_file=fopen(p_char, "r");
if (p_file == NULL) {
fprintf(stderr, "Cannot read file %s!\n", p_char);
exit(EXIT_FAILURE);
}
couple dim={0,0}; //la structure couple est déf dans le loader.h
int width = 0;
int height = 0;
int fscanf_result = 0;
fscanf_result = fscanf(p_file, "%d %d\n", &width, &height);
if (fscanf_result != 2) {
fprintf(stderr, "First line is not syntactically correct!\n");
exit(EXIT_FAILURE);
}
dim.x=width;
dim.y=height;
fclose(p_file);
return dim;
}
//La fonction Load est celle demandée et permettant de charger une carte ; elle utilise Dimensions et Array_Creator
map* load(char *filename){
map* map_loaded=malloc(sizeof(map)); //on alloue dynamiquement la map
//Dans un premier temps on récupère les dimensions
couple map_dim={0,0};
map_dim=Dimensions(filename);
map_loaded->width=map_dim.x;
map_loaded->height=map_dim.y;
//Dans un second temps on définit le tableau 1D contenant l'ensemble des éléments de la map
char* p_char=Array_Creator(filename);
map_loaded->p_char=p_char;
return map_loaded;
}
The command line is, for example : ./replay ./data/soko.in 4 7"NSWSESNWW"命令行是,例如: ./replay ./data/soko.in 4 7"NSWSESNWW"
In the move
function, should change all在move
功能中,应该改变所有
return(map_loaded); => return(new_map);
It will map_loaded to be freed twice when the move function returns map_loaded当移动函数返回 map_loaded 时,它会释放两次 map_loaded
Code analysis代码分析
map *replay(map *map_loaded, int length, char *plan)
{
map *new_map = move(map_loaded, plan[0]); // [2] set new_map to map_loaded
map *old_map = deep_copy(new_map);
for (int i = 1 ; i < length ; i++) {
free(new_map->p_char);
free(new_map); // [3] new_map be freed,
// equivalent map_loaded be freed
new_map = move(old_map, plan[i]);
...
}
...
return new_map;
}
int main(int argc, char *argv[])
...
map *map_loaded = load(argv[1]); // [1] map_loaded be malloc
...
map *new_map = replay(map_loaded, length, plan);
...
free(map_loaded->p_char);
free(map_loaded); // [4] map_loaded be freed
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.