C saper 0xC0000005

0

Witam, muszę napisać sapera (niestety w C) :/. Naskrobałem trochę kodu ale mam mały problem jak włączam program kończy się błędem. Co dziwniejsze jak włączę debugowanie to jest all ok, całość przechodzi bez problemów, tzn. jak wykonuje krok po kroku ma ktoś jakiś pomysł? Zamieszczam kod:

main.c:

 #include <gtk/gtk.h>
#include <stdio.h>
#include "list.h"
#include "sap_funct.h"
#include <time.h>

void gen_tab(int x, int y);

enum level_names selected_level;
struct level easy={.level_name=latwy, .high=9, .width=9, .num_of_bombs=10};
struct level mid={.level_name=sredniozaawansowany, .high=16, .width=16, .num_of_bombs=40};
struct level hard={.level_name=zaawansowany, .high=16, .width=30, .num_of_bombs=99};

int main( int   argc, char *argv[] )
{
    srand(time(NULL));
    read_score_info(&selected_level);
    gen_tab(4, 4);
    system("pause");
    return 0;
}

void gen_tab(int x, int y)
{
     switch(selected_level)
     {
            case 0:
                 generate_start_tab(easy, x, y, NULL);
                 break;
            case 1:
                 generate_start_tab(mid, x, y, NULL);
                 break;
            case 2:
                 generate_start_tab(hard, x, y, NULL);

     }
}

list.h:

#ifndef LIST_H
#define LIST_H

#include <stdio.h>

enum level_names{latwy, sredniozaawansowany, zaawansowany};

struct level
{
    enum level_names level_name;
    int high, width, num_of_bombs;       
};


struct level_info
{
    enum level_names level_name;
    int game_played;
    int game_won;
    double won_percent;
    int longest_won_count;
    int longest_lost_count;
    int current_pass;
};

struct list
{
    struct list *next;
    struct list *first;
    struct level_info info;
};

void list_add(struct level_info);
struct list* return_info(enum level_names);
void destroy_list();
char * lvl_nam_to_str(enum level_names name);
void save(struct level_info info);
void read_score_info(enum level_names *selected_level);

#endif 

list.c:

 #include "list.h"

struct list *curr = 0;

const char *file_patch="score_dat";

void list_add(struct level_info info)
{
     if(curr!=0)
            curr = curr -> first;
     struct list *tmp = (struct list *)malloc (sizeof(struct list));
     tmp -> info = info;
     tmp -> first = tmp;
     tmp -> next = curr;
     while(curr!=0)
     {
           curr -> first = tmp;
           curr = curr -> next;              
     } 
     curr = tmp;
}

struct list* return_info(enum level_names name)
{
     curr = curr -> first;
     while(curr!=0)
     {
            if(curr -> info.level_name==name)
                    return curr;
            else
                    curr = curr -> next;
     }
}

void destroy_list()
{
     curr = curr -> first;
     while(curr!=0)
     {
            struct list *tmp = (struct list *)malloc (sizeof(struct list));
            tmp = curr;
            curr = curr -> next;
            curr -> first = 0;
            free(tmp);            
     }     
}

char * lvl_nam_to_str(enum level_names name)
{
     char *lvl_name;
     switch(name)
     {
           case latwy:
                lvl_name="latwy";
                break;
           case sredniozaawansowany:
                lvl_name="sredniozaawansowany";
                break;
           case zaawansowany:
                lvl_name="zaawansowany";
     };
     return lvl_name;    
}

void save(struct level_info info)
{
     FILE *file;
     file = fopen(file_patch, "w");
     fprintf(file, "%s\n%d\n%d\n%f\n%d\n%d\n%d\n", lvl_nam_to_str(info.level_name), 
             info.game_played, info.game_won, info.won_percent, info.longest_won_count, 
             info.longest_lost_count, info.current_pass);
}

void read_score_info(enum level_names *selected_level)
{
     FILE *file;
     char name[50];
     file = fopen(file_patch, "r");
     int i;
     for(i=0; i<3; i++)
     {
         struct level_info info;
         fscanf(file, "%s", 
                      name,
                      &info.game_played,
                      &info.game_won,
                      &info.won_percent,
                      &info.longest_won_count,
                      &info.longest_lost_count,
                      &info.current_pass);
         if(name=="latwy")
               info.level_name=latwy;
         else if(name=="sredniozaawansowany")
              info.level_name=sredniozaawansowany;
         else
             info.level_name=zaawansowany;
         list_add(info);
     }
     int sel_lvl;
     fscanf(file, "%d", &sel_lvl); 
     selected_level = sel_lvl;
}

sap_funct.h:

#ifndef SAP_FUNC_H
#define SAP_FUNC_H
#include "list.h"

void generate_start_tab(struct level, int first_x, int first_y, struct level *old_level);
char count_bombs(int x, int y, int high, int width);
char **sap_tab;



#endif 

sap_funct.c:

 #include "sap_funct.h"
#include <time.h>

void generate_start_tab(struct level cur_level, int first_x, int first_y, struct level *old_level)
{
     int j, i;
     if(old_level!=0)
     {
         for(i=0; i<old_level->high; i++)
                free(sap_tab[i]);
         free(sap_tab);
     }
     int high = cur_level.high, width = cur_level.width;
     sap_tab = (char **)malloc(sizeof(char)*high);
     for(i=0; i<high; i++)
         sap_tab[i] = (char *)malloc(sizeof(char)*width);

    for(i=0; i<high; i++)
            for(j=0; j<width; j++)
                sap_tab[i][j]=' ';

     int x, y, bomb_num=cur_level.num_of_bombs;
     i=0;
     while(i<bomb_num)
     {
           x=rand()%high;
           y=rand()%width;
           if((x<first_x-1 || x>first_x+1) && (y<first_y-1 || y>first_y+1) && sap_tab[x][y]!='b')
           {
                     sap_tab[x][y]='b';
                     i++;
           }
     }
     for(i=0; i<high; i++)
              for(j=0; j<width; j++)
                       if(sap_tab[i][j]!='b')
                                sap_tab[i][j]=count_bombs(i, j, high, width);
     for(i=0; i<high; i++)
     {
            for(j=0; j<width; j++)
                printf("%c", sap_tab[i][j]);
            printf("\n");
     }
     if(old_level!=0)
         free(old_level);
}

char count_bombs(int x, int y, int high, int width)
{
         int i, j, count=0;
         x--;
         y--;
         for(i=0; i<3; i++)
             for(j=0; j<3; j++)
                   if((x+i>=0 && x+i<high) && (y+j>=0 && y+j<width))
                        if(sap_tab[x+i][y+j]=='b')
                              count++;
         char sign = count+48;
         return sign;
}
0

w samym tylko list.c:

list.c:11:40: warning: implicitly declaring C library function 'malloc' with type 'void *(unsigned int)'
     struct list *tmp = (struct list *)malloc (sizeof(struct list));
                                       ^
list.c:11:40: note: please include the header <stdlib.h> or explicitly provide a declaration for 'malloc'
list.c:33:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
list.c:44:13: warning: implicit declaration of function 'free' is invalid in C99 [-Wimplicit-function-declaration]
            free(tmp);
            ^
list.c:91:17: warning: result of comparison against a string literal is unspecified (use strncmp instead)
      [-Wstring-compare]
         if(name=="latwy")
                ^ ~~~~~~~
list.c:93:22: warning: result of comparison against a string literal is unspecified (use strncmp instead)
      [-Wstring-compare]
         else if(name=="sredniozaawansowany")
                     ^ ~~~~~~~~~~~~~~~~~~~~~
list.c:101:21: warning: incompatible integer to pointer conversion assigning to 'enum level_names *' from 'int';
     selected_level = sel_lvl;
                    ^ ~~~~~~~
6 warnings generated.
0

Tak wiem ze są warningi ale one nie powinny powodować wywalania się programu bo akurat ta część działa.
Zajmę się nimi ale chciałbym najpierw żeby mi program działał :P

0

Akurat naprawienie warningów rozwiąże też twój problem

0

Perełka.

Ostrzeżenia oznaczają, że kod jest formalnie poprawny, ale prawdopodobnie nie jest to to, co programista miał na myśli, albo zachowanie programu jest niezdefiniowane.

Czyli: albo usuwasz warningi, albo wiesz co oznaczają i jesteś pewien, że tak ma być.
Nie można ich tak zostawiać nie wiedząc nawet co oznaczają.

1 użytkowników online, w tym zalogowanych: 0, gości: 1