0

In Pset3 I am using this line for duration

return 8/atoi(&fraction[2]);

When I use gdb, and go into atoi, atoi calls a function in the file strtol_l. At line 298 I get a seg fault saying "error: Cannot access memory at address 0x2". Does anyone know what caused this? Is it my code?

0
1

Maybe you are accidentally processing a rest line, because your is_rest function does not work correctly.

Reason: &fraction[2] in C is the same as fraction + 2. The 2 is multiplied by the size of whatever fraction is pointing to, here char, so the factor is 1. To have a pointer of 0x2 as result, fraction must be NULL.

Personal opinion on the code you did show: I wouldn't use atoi to convert a single digit, but just subtract character '0' from the digit. Works for all the single digit integers, like the octave or both numbers in the duration.

11
  • I am SO dumb. I forgot to input a fraction to synthisize. Mar 6 '18 at 15:06
  • I fixed that Seg fault. Now the song_close function seg faults at wav.c:194. Mar 6 '18 at 15:42
  • So you're calling the functions manually? The whole file handling is nothing one is supposed to touch, so a segfault there is a bit surprising. Is there any chance you are messing up the output file (or the corresponding FILE* variable) in some way? Or have out-of-bounds accesses which accidentally overwrite the FILE structure on the heap?
    – Blauelf
    Mar 6 '18 at 16:18
  • I didn't mess with the file handling. I don't really understand the FILE variable, but I don't think I broke it. Mar 6 '18 at 16:24
  • I looked up line 194, and it's not related to FILE at all. I still assume you are accessing some memory that does not belong to the variable you meant, e.g. an out-of-bounds access (using an invalid index on an array or string). Problem and where it manifests might be pretty far away from each other.
    – Blauelf
    Mar 6 '18 at 16:48

You must log in to answer this question.

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