Search code examples
cstringdata-structures

what is the error cygwin_exception::open_stackdumpfile: Dumping stack trace?


Im doing a project where i need print a searched structure, but when i print, it does the first print and then it exits the run and gives me this error: error i searched the error and it is normaly issues with dynamic memory, but it shouldn't be my case because im only printing wht is there, im not adding anything, to my knowledge.

print function:

void printCompany(Companies companies, int i) {
    printf("\nNif: %d", companies.companies[i].Nif);
    printf("\nName: %s", companies.companies[i].name);
    printf("\nCategory: %s", companies.companies[i].categories);
}

structure:

#define COMPANY_INITIAL_SIZE 20
#define SEARCHES_INITIAL_SIZE 20
#define ACTIVITIES_INITIAL_SIZE 20

#define TEXT_MAX_SIZE 200
#define NAME_MAX_SIZE 50
#define EMAIL_MAX_SIZE 50
#define TITLE_MAX_SIZE 50
#define STREET_MAX_SIZE 70
#define LOCATION_MAX_SIZE 70

#define NIF_MIN_SIZE 100000000
#define NIF_MAX_SIZE 999999999

    enum category {
        microCompany = 1, pmeCompany, macroCompany
    };

    enum state {
        inactive, active
    };

    enum searchCamp {
        nif, businessArea, nameLocation
    };

    typedef struct {
        char title[TITLE_MAX_SIZE];
        char text[TEXT_MAX_SIZE];
        char userName[NAME_MAX_SIZE];
        char userEmail[EMAIL_MAX_SIZE];
    } Coment;

    typedef struct {
        int nota;
        char userName[NAME_MAX_SIZE];
        char userEmail[EMAIL_MAX_SIZE];
    } Rating;

    typedef struct {
        int Nif;
        char name[NAME_MAX_SIZE];
        enum category categories;
        int businessAreaCode;
        enum state states;
        char street[STREET_MAX_SIZE];
        char location[LOCATION_MAX_SIZE];
        int postalCode;
        int nComents;
        Coment coments;
        int nRating;
        Rating ratings;
    } Company;

    typedef struct {
        int counter, size;
        Company *companies;
    } Companies;

    void freeCompanies(Companies *);
    void loadCompanyFile(Companies *, char*);
    void saveCompanyFile(Companies *, char *);
    void insertCompanies(Companies*);
    void expandCompanySize(Companies *);
    void searchNifCompanies(Companies);
    //void printCompany(Company);
    void printCompany(Companies,int);

function that calls the print function:

/**
 * @brief This function compares the element 
 * @param companies The struct Companies.
 * @param nif The unique value of a company
 * @return if sucesseful returns the index of the company found, if  unsucesseful returns -1.
 */
int searchNifCompany(Companies companies, int nif) {
    int i;
    for (i = 0; i < companies.counter; i++) {
        if (companies.companies[i].Nif == nif) {

            return i;
        }
    }

    return -1;
}

/**
 * @brief This function 
 * @param *companies The pointer of the struct Companies.
 * @return 
 */
void searchNifCompanies(Companies companies) {
    int company, nif;

    nif = getInt(NIF_MIN_SIZE, NIF_MAX_SIZE, "\nNIF[9 digits]: ");
    company = searchNifCompany(companies, nif);

    if (company != -1) {
        //printCompany(companies.companies[company]);
        printCompany(companies, company);
    } else {

        puts("The company doesn't exist.");
    }
}

it gives me the same result even i manualy tell to print the structure:

Companies companies;

int main() {
    
    loadCompanyFile(&companies, COMPANIES_FILE_NAME);

    mainMenu();
    
    printf("\nNif: %d", companies.companies[1].Nif);
    printf("\nName: %s", companies.companies[1].name);
    printf("\nCategory: %s", companies.companies[1].categories);
    
    saveCompanyFile(&companies, COMPANIES_FILE_NAME);
    freeCompanies(&companies);
    return (EXIT_SUCCESS);
}

Solution

  • You should get some compiler warnings about wrong parameter types for printf:

    printf("\nCategory: %s", companies.companies[i].categories);
    

    You are passing an integer value instead of the address of the first character in a string.

    That is cheating on your printf function. While you promise to provide the address of a string (by using %s format specifier), the value you pass is no valid address. And its size is also likely to be smaller than an address value (probably 32 bit vs 64 bit).

    printf relies on you keeping your promise and take that value as you told so..

    That will cause an illegal memory access triggering your stack dump.

    Always listen to warnings from your compiler. If you did not get a message, increase warning level.

    For GCC or Clang use -Wall -Wextra -pedantic

    BTW: Did you expect to print text like microCompany that you used for naming the enum values? That is not how enums work. They are only named numbers. The identifiers used for the values are not existing as strings in the program.