0

So I just finished up pset3 and when I run checks for correctness and style everything comes up good. The only issue I seem to be having is if I go to manually test with ./synthesize test.wav then give it a note, it comes back with "helpers.c:13:14: runtime error: load of null pointer of type 'char' Segmentation fault". I've tried looking into it and switching things around but can't seem to figure out what this actually means. Any help here? Here's the code on the specified lines in the error:

    char n = fraction[0];
int numerator = n - '0';
char d = fraction[2];
int denominator = d - '0';

return ((8 / denominator) * numerator);

And the rest of the code, if needed:

int frequency(string note)
{

int octave = note[strlen(note) - 1];
octave -= 48;
float freq = 0.0;

// Checks which note letter it is and adjusts the frequency using A4, or 440hz, as a base
switch (note[0])
{
    case 'C':
        freq = 440.0 / pow(2.0, (9.0 / 12.0));
        break;

    case 'D':
        freq = 440.0 / pow(2.0, (7.0 / 12.0));
        break;

    case 'E':
        freq = 440.0 / pow(2, (5.0 / 12.0));
        break;

    case 'F':
        freq = 440.0 / pow(2.0, (4.0 / 12.0));
        break;

    case 'G':
        freq = 440.0 / pow(2.0, (2.0 / 12.0));
        break;

    case 'A':
        freq = 440.0;
        break;

    case 'B':
        freq = 440.0 * pow(2.0, (2.0 / 12.0));
        break;
}

// checks if the octave is lower or higher than 4, for base A4, and adjusts the frequency
if (octave > 4)
{
    int n = octave - 4;
    freq *= pow(2.0, n);
}

else if (octave < 4)
{
    int n = 4 - octave;
    freq /= pow(2.0, n);
}

// Adjusts frequency if the note is sharp or flat
if (note[1] == '#')
{
    freq *= pow(2.0, (1.0 / 12.0));
}

else if (note[1] == 'b')
{
    freq /= pow(2.0, (1.0 / 12.0));
}

// Rounds the frequency number to turn it from a float to an int
int result = round(freq);
return result;

}

My is rest line:

bool is_rest(string s)
{
    if (s[0] == '\0')
    {
        return true;
    }

    else
    {
        return false;
    }

}

2
  • What's at line 13 of helpers.c?
    – Blauelf
    Mar 8 '18 at 14:57
  • Sorry, guess I should have specified. Line 13 is: char n = fraction[0];
    – DylieWylie
    Mar 8 '18 at 14:59
1

One thing to check is your is_rest. If you mess up there, you might pass garbage (here: a NULL pointer) to the other functions when there's an actual rest line.

A rest line is specified by an empty line, meaning the string has a length of zero, equivalent to the first character being the null terminator.

EDIT: An alternative is that your input is bad. Notes have to consist of a tone and a duration, separated by a @. Any line without a note should not have any characters.

4
  • I think my is_rest is okay as far as I can tell? I edited the code I currently have for that into the question.
    – DylieWylie
    Mar 8 '18 at 15:24
  • That's really weird. The error message clearly states that fraction is NULL, which happens if you enter some irregular line without a duration part. Are you sure that's the code you compiled and ran and that led to this error message?
    – Blauelf
    Mar 8 '18 at 16:27
  • Yeah I copied the code directly and pasted it here. I don't understand it either. Like I said, when I run the test for correctness everything comes back green and all the outputs are correct. Only when I use the ./synthesize test.wav command does it give me an error.
    – DylieWylie
    Mar 8 '18 at 18:11
  • Oh wow that was it lol. I was forgetting to put the @duration and was just putting the note itself. I wasn't even thinking about that.
    – DylieWylie
    Mar 8 '18 at 18:49
0

Check out the bday.txt file for the format.

0
0

The problem with the returning NULL char is in the is_rest() function. Try changing your condition line to

    if (strlen(s) == 0) return true;
    else return false;

of course, remember to include the library.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .