Thread: Exc_bad_access

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    21

    Exc_bad_access

    I've looked this up over and over and every post I come up with has something to do with Objective-C. Nobody has been able to tell me what it means. I am not writing Objective-C code, but I am using the Xcode compiler, which is designed specifically for Objective-C.
    When I compile, there is no error, instead it hangs up during runtime when it hits certain lines.
    I was hoping somebody could compile this on a windows machine and tell me what happens (as I don't own one).

    If more information about the exact error is needed I can provide it, but it is a lot of writing so I won't clutter the initial post with it. (Xcode shows where in the assembly code the program goes bad, so if someone knows assembly I will provide that)

    here is main.h:
    Code:
    #include <string.h>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #define HEADINGS "Employee Name             Rate     Hours      Gross     Tax     Net\n"
    //#define HEADINGS " Rate     Hours      Gross     Tax     Net\n"
    #define FORMAT         "%-24s %4.2f %9.2f %10.2f %7.2f %7.2f\n"
    //#define FORMAT         "%4.2f %9.2f %10.2f %7.2f %7.2f\n"
    #define TAXRATE .15
    
    int main(void); //3.0
    void openFile(void); //3.1
    void mainLoop(void); //3.2
    char* getName(char* firstName, char* lastName); //3.2.1
    char* getFirstName(void); //3.2.1.1
    char* getLastName(void); //3.2.1.2
    float getHours(void); //3.2.2
    float getPayrate(void); //3.2.3
    void printReport(void); //3.2.4
    float calcGross(void); //3.2.4.1
    float calcTax(void); //3.2.4.2
    float calcNet(void); //3.2.4.3
    void askToContinue(void); //3.2.5
    void closeFile(void); //3.3
    
    FILE* report;
    char* fullName;
    char* firstName;
    char* lastName;
    float hours;
    float payrate;
    float gross;
    float tax;
    float net;
    char* answer;
    int continueBool;
    and here is main.c:
    Code:
    #include "main.h"
    
    //3.0
    int main(void) {
        openFile();
        mainLoop();
        closeFile();
        return 0;
    }
    //3.1
    void openFile(void) {
        report = fopen("/Users/mike/Desktop/report.txt", "w");
        fprintf(report, HEADINGS);
        printf(HEADINGS);
    }
    //3.2
    void mainLoop(void) {
        continueBool = 1;
        while (continueBool == 1) {
            getName(firstName, lastName);
            getPayrate();
            getHours();
            calcGross();
            calcTax();
            calcNet();
            printReport();
            askToContinue();
        }
    }
    //3.2.1
    char* getName(char* firstName, char* lastName) {
        sprintf(fullName, "%s, %s", lastName, firstName);
        return fullName;
    }
    //3.2.1.1
    char* getFirstName(void) {
        printf("Enter first name: ");
        scanf("%s", &firstName);
        return firstName;
    }
    //3.2.1.2
    char* getLastName(void) {
        printf("Enter last name: ");
        scanf("%s", &lastName);
        return lastName;
    }
    //3.2.2
    float getHours(void) {
        printf("Enter hours worked: ");
        scanf("%f", &hours);
        return hours;
    }
    //3.2.3
    float getPayrate(void) {
        printf("Enter hourly pay rate: $");
        scanf("%f", &payrate);
        return payrate;
    }
    //3.2.4
    void printReport(void) {
        printf(FORMAT, fullName, payrate, hours, gross, tax, net);
        //printf(FORMAT, payrate, hours, gross, tax, net);
        fprintf(report, FORMAT, fullName, payrate, hours, gross, tax, net);
        //fprintf(report, FORMAT, payrate, hours, gross, tax, net);
    }
    //3.2.4.1
    float calcGross(void) {
        if (hours <= 40) {
            gross = hours * payrate;
            return gross;
        } else {
            gross = (payrate * 40) + ((hours - 40) * (payrate * 1.5));
            return gross;
        }
    }
    //3.2.4.2
    float calcTax(void) {
        tax = gross * TAXRATE;
        return tax;
    }
    //3.2.4.3
    float calcNet(void) {
        net = gross - tax;
        return tax;
    }
    //3.2.5
    void askToContinue(void) {
        printf("Would you like to make another entry? [Y/N]");
        scanf("%s", &answer);
        if ((strcmp(answer, "Y") && strcmp(answer, "y")) != 0) {
            continueBool = 0;
        }
    }
    //3.3
    void closeFile(void) {
        fclose(report);
    }
    Thanks for your time.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well,

    1. You're using global variables.
    2. The global variables which are pointers will be initialised to point to NULL.
    3. The only function being passed parameters, gets passed global variables.

    > sprintf(fullName, "%s, %s", lastName, firstName);
    So this basically expands to
    sprintf(NULL, "%s, %s", NULL, NULL); // boom today!


    At a bare minimum, your char *foo variables should be char foo[1000]; say.

    > scanf("%s", &lastName);
    This is very wrong - lastName IS a pointer.
    So all you're doing here is trashing your actual pointer, not writing a string to where it points.
    You might just get away with it for very short strings, but be sure that it is totally wrong.

    > #include <iostream>
    > #include <fstream>
    > #include <iomanip>
    If you're REALLY programming in C, then don't include C++ header files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    Okay, good to know. I fixed what I could see to fix, but there are still problems.
    sorry about the c++ headers, I compile this as cpp but I don't really use anything that's c++ specific besides those headers so I figured I'd post it here.
    main.h:
    Code:
    #include <string.h>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #define HEADINGS "Employee Name             Rate     Hours      Gross     Tax     Net\n"
    //#define HEADINGS " Rate     Hours      Gross     Tax     Net\n"
    #define FORMAT         "%-24s %4.2f %9.2f %10.2f %7.2f %7.2f\n"
    //#define FORMAT         "%4.2f %9.2f %10.2f %7.2f %7.2f\n"
    #define TAXRATE .15
    
    int main(void); //3.0
    void openFile(void); //3.1
    void mainLoop(void); //3.2
    char* getName(); //3.2.1
    char* getFirstName(void); //3.2.1.1
    char* getLastName(void); //3.2.1.2
    float getHours(void); //3.2.2
    float getPayrate(void); //3.2.3
    void printReport(void); //3.2.4
    float calcGross(void); //3.2.4.1
    float calcTax(void); //3.2.4.2
    float calcNet(void); //3.2.4.3
    void askToContinue(void); //3.2.5
    void closeFile(void); //3.3
    
    FILE* report;
    char fullNameBuff[23];
    char* fullName;
    char firstNameBuff[12];
    char* firstName;
    char lastNameBuff[12];
    char* lastName;
    float hours;
    float payrate;
    float gross;
    float tax;
    float net;
    char* answer;
    int continueBool;
    main.cpp
    Code:
    #include "main.h"
    
    //3.0
    int main(void) {
        openFile();
        mainLoop();
        closeFile();
        return 0;
    }
    //3.1
    void openFile(void) {
        report = fopen("/Users/mike/Desktop/report.txt", "w");
        fprintf(report, HEADINGS);
        printf(HEADINGS);
    }
    //3.2
    void mainLoop(void) {
        continueBool = 1;
        while (continueBool == 1) {
            getName();
            getPayrate();
            getHours();
            calcGross();
            calcTax();
            calcNet();
            printReport();
            askToContinue();
        }
    }
    //3.2.1
    char* getName() {
        firstName = firstNameBuff;
        lastName = lastNameBuff;
        getFirstName();
        getLastName();
        sprintf(fullName, "%s, %s", lastName, firstName); Thread 1: Program received signal: "EXC_BAD_ACCESS"
        return fullName;
    }
    //3.2.1.1
    char* getFirstName(void) {
        printf("Enter first name: ");
        scanf("%s", firstName);
        return firstName;
    }
    //3.2.1.2
    char* getLastName(void) {
        printf("Enter last name: ");
        scanf("%s", lastName);
        return lastName;
    }
    //3.2.2
    float getHours(void) {
        printf("Enter hours worked: ");
        scanf("%f", &hours);
        return hours;
    }
    //3.2.3
    float getPayrate(void) {
        printf("Enter hourly pay rate: $");
        scanf("%f", &payrate);
        return payrate;
    }
    //3.2.4
    void printReport(void) {
        printf(FORMAT, fullName, payrate, hours, gross, tax, net);
        //printf(FORMAT, payrate, hours, gross, tax, net);
        fprintf(report, FORMAT, fullName, payrate, hours, gross, tax, net);
        //fprintf(report, FORMAT, payrate, hours, gross, tax, net);
    }
    //3.2.4.1
    float calcGross(void) {
        if (hours <= 40) {
            gross = hours * payrate;
            return gross;
        } else {
            gross = (payrate * 40) + ((hours - 40) * (payrate * 1.5));
            return gross;
        }
    }
    //3.2.4.2
    float calcTax(void) {
        tax = gross * TAXRATE;
        return tax;
    }
    //3.2.4.3
    float calcNet(void) {
        net = gross - tax;
        return tax;
    }
    //3.2.5
    void askToContinue(void) {
        printf("Would you like to make another entry? [Y/N]");
        scanf("%s", answer);
        if ((strcmp(answer, "Y") && strcmp(answer, "y")) != 0) {
            continueBool = 0;
        }
    }
    //3.3
    void closeFile(void) {
        fclose(report);
    }

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Why don't you go ahead and read through Salem's post again. Where do you actually allocate any space for your char pointers?

    Quote Originally Posted by Salem View Post
    Well,

    1. You're using global variables.
    2. The global variables which are pointers will be initialised to point to NULL.
    3. The only function being passed parameters, gets passed global variables.

    > sprintf(fullName, "%s, %s", lastName, firstName);
    So this basically expands to
    sprintf(NULL, "%s, %s", NULL, NULL); // boom today!


    At a bare minimum, your char *foo variables should be char foo[1000]; say.

    > scanf("%s", &lastName);
    This is very wrong - lastName IS a pointer.
    So all you're doing here is trashing your actual pointer, not writing a string to where it points.
    You might just get away with it for very short strings, but be sure that it is totally wrong.

    > #include <iostream>
    > #include <fstream>
    > #include <iomanip>
    If you're REALLY programming in C, then don't include C++ header files.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    oh sorry looks like I missed some.
    main.h
    Code:
    #include <string.h>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #define HEADINGS "Employee Name             Rate     Hours      Gross     Tax     Net\n"
    //#define HEADINGS " Rate     Hours      Gross     Tax     Net\n"
    #define FORMAT         "%-24s %4.2f %9.2f %10.2f %7.2f %7.2f\n"
    //#define FORMAT         "%4.2f %9.2f %10.2f %7.2f %7.2f\n"
    #define TAXRATE .15
    
    int main(void); //3.0
    void openFile(void); //3.1
    void mainLoop(void); //3.2
    char* getName(); //3.2.1
    char* getFirstName(void); //3.2.1.1
    char* getLastName(void); //3.2.1.2
    float getHours(void); //3.2.2
    float getPayrate(void); //3.2.3
    void printReport(void); //3.2.4
    float calcGross(void); //3.2.4.1
    float calcTax(void); //3.2.4.2
    float calcNet(void); //3.2.4.3
    void askToContinue(void); //3.2.5
    void closeFile(void); //3.3
    
    FILE* report;
    char fullNameBuff[23];
    char* fullName;
    char firstNameBuff[12];
    char* firstName;
    char lastNameBuff[12];
    char* lastName;
    float hours;
    float payrate;
    float gross;
    float tax;
    float net;
    char answerBuff[2];
    char* answer;
    int continueBool;
    main.cpp
    Code:
    #include "main.h"
    
    //3.0
    int main(void) {
        openFile();
        mainLoop();
        closeFile();
        return 0;
    }
    //3.1
    void openFile(void) {
        report = fopen("/Users/mike/Desktop/report.txt", "w");
        fprintf(report, HEADINGS);
        printf(HEADINGS);
    }
    //3.2
    void mainLoop(void) {
        continueBool = 1;
        while (continueBool == 1) {
            getName();
            getPayrate();
            getHours();
            calcGross();
            calcTax();
            calcNet();
            printReport();
            askToContinue();
        }
    }
    //3.2.1
    char* getName() {
        fullName = fullNameBuff;
        firstName = firstNameBuff;
        lastName = lastNameBuff;
        getFirstName();
        getLastName();
        sprintf(fullName, "%s, %s", lastName, firstName);
        return fullName;
    }
    //3.2.1.1
    char* getFirstName(void) {
        printf("Enter first name: ");
        scanf("%s", firstName);
        return firstName;
    }
    //3.2.1.2
    char* getLastName(void) {
        printf("Enter last name: ");
        scanf("%s", lastName);
        return lastName;
    }
    //3.2.2
    float getHours(void) {
        printf("Enter hours worked: ");
        scanf("%f", &hours);
        return hours;
    }
    //3.2.3
    float getPayrate(void) {
        printf("Enter hourly pay rate: $");
        scanf("%f", &payrate);
        return payrate;
    }
    //3.2.4
    void printReport(void) {
        printf(FORMAT, fullName, payrate, hours, gross, tax, net);
        //printf(FORMAT, payrate, hours, gross, tax, net);
        fprintf(report, FORMAT, fullName, payrate, hours, gross, tax, net);
        //fprintf(report, FORMAT, payrate, hours, gross, tax, net);
    }
    //3.2.4.1
    float calcGross(void) {
        if (hours <= 40) {
            gross = hours * payrate;
            return gross;
        } else {
            gross = (payrate * 40) + ((hours - 40) * (payrate * 1.5));
            return gross;
        }
    }
    //3.2.4.2
    float calcTax(void) {
        tax = gross * TAXRATE;
        return tax;
    }
    //3.2.4.3
    float calcNet(void) {
        net = gross - tax;
        return tax;
    }
    //3.2.5
    void askToContinue(void) {
        printf("Would you like to make another entry? [Y/N]");
        scanf("%s", answer); Thread 1: Program received signal: "EXC_BAD_ACCESS"
        if ((strcmp(answer, "Y") && strcmp(answer, "y")) != 0) {
            continueBool = 0;
        }
    }
    //3.3
    void closeFile(void) {
        fclose(report);
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > sorry about the c++ headers, I compile this as cpp but I don't really use anything that's c++ specific besides those headers
    There is NOTHING I could see in your code which needs any of those headers at all.
    Use
    #include <stdio.h>
    for all your printf/scanf prototypes.

    C is one side of the road.
    C++ is the other side of the road.
    C/C++ (what you're doing) is a drunk staggering down the median line; the only end result of this is going to be roadkill.

    Decide which language you're actually trying to learn, and set about learning it.
    Throwing any old rubbish at the compiler ("Oh well, it compiles") is not getting you anywhere at the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    okay I did it AGAIN. sorry. I missed the
    answer = answerBuff; line.
    It all works now. thanks guys. you're both great.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    I really enjoyed that analogy salem. so you're saying that I don't need fstream or iomanip to deal with files? I was told I needed those. I don't know why i had string.h in there to tell you the truth.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ObjectWithBrain
    so you're saying that I don't need fstream or iomanip to deal with files?
    Not in C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by ObjectWithBrain View Post
    I really enjoyed that analogy salem. so you're saying that I don't need fstream or iomanip to deal with files? I was told I needed those. I don't know why i had string.h in there to tell you the truth.
    I am not sure what you are "learning" from, but I would suggest you look at C Made Easy Tutorials for a good starting point. Additionally, I would recommend you find a decent book. We have a link to suggestions at the top of this forum.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exc_bad_access error when compiling c code in xcode
    By new_cuser in forum C Programming
    Replies: 6
    Last Post: 07-27-2011, 03:43 PM
  2. Exc_bad_access
    By xsmet in forum C Programming
    Replies: 5
    Last Post: 05-24-2011, 03:16 PM
  3. string compare “EXC_BAD_ACCESS”.
    By dunsta in forum C Programming
    Replies: 3
    Last Post: 05-03-2010, 10:30 PM

Tags for this Thread