0

Check50 Server2 failed multiple tests, and someone pointed out that these issues can stem from load(). I've looked over my load code and I've looked at other questions on SE but I can't find anything that worked. Check50 fails all tests except for these:

:) server.c exists
:) server.c compiles
:) Requesting /test redirects to /test/`

Here is my load code:

//code edited out per academic honesty

Thanks in advance.

1

The program reads one byte at a time and essentially appends it to buffer. So the number of bytes that buffer needs to hold increases each read. This buffer = realloc(buffer, sizeof(char)); allocates buffer for 1 byte every time. It needs to be at least as big as the number of bytes read. You're already keeping track of that so it should be an easy correction.

It is a problem to treat the bytes read as char data. Valid char values are 0 - 127 (corresponding to the ASCII character set). Think about a white pixel from pset 4. That would be 3 bytes of 255,255,255. Image data does not "translate" to ascii. Therefore you should not "translate" the bytes read to char. Use int here for (char c = fgetc(file); c != EOF; c = fgetc(file)). And do not cast c here buffer[size] = (char) c;. Something like:

for (int c = fgetc(file); c != EOF; c = fgetc(file))
    {
        buffer[size] =  c; 

Another problem: free(buffer); The memory for buffer has been allocated and will be "sent" back to main in *content. This free will "destroy" that memory when it returns to main, so content will contain "nothing". content will be free'd in main around line 1234. Do not free(buffer);.

  • Thank you so much! – SuperNovaCoder Jan 21 '17 at 1:52
  • I apologize for responding so late, but I have just tried to implement load() the way you suggested and I am still having problems. You say not to cast c here buffer[size] = (char) c; but I am having trouble figuring out what to cast instead and what the variable c could be used for. Any pointers or tips would be much appreciated. Thank you. – SuperNovaCoder Jan 25 '17 at 14:10
  • added example to the answer – DinoCoderSaurus Jan 25 '17 at 14:50
  • One final question - is this calculation for realloc correct? buffer = realloc(buffer, size + sizeof(char)); I just wanted to know because I have implemented everything else and load() is still not working correctly. – SuperNovaCoder Jan 25 '17 at 18:42
  • Sounds correct. does "load is still not working correctly" mean you are getting the same check50 result? – DinoCoderSaurus Jan 25 '17 at 19:42

You must log in to answer this question.

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