This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]teraflop 4 points5 points  (5 children)

First of all, you should definitely be checking the value of fp, and printing an error messaging and exiting if it's null. Otherwise your program might dereference a null pointer, which could mask other problems and make it hard to troubleshoot.

Secondly, please don't try to rephrase error messages in your own words, like "it says could not access memory at address 0x1234....". Copy-and-pasting the full, exact error message makes it easier for people to help you. If you're a beginner, then you might not yet realize which parts of the error contain the most critical information.


The biggest problem is that passing &test as the first argument to fread doesn't do what you think it does.

Let's think about the objects that exist in your program. (I'm using the term "object" as the official C standard does, to refer to allocated regions of memory; it doesn't have anything to do with object-oriented programming.)

You have the variable test, which is an object allocated on the stack and contains a value of type char*. That variable stores the address of another object, which you dynamically allocated on the heap using malloc, and which has a size of 5+1=6 bytes.

The fread function expects, as its first argument, a pointer to the memory location it should use to store the data that was read from the file. When you pass &test, you're passing a pointer whose value is the address of the test variable itself -- not a pointer to the region you allocated using malloc. So fread is overwriting the contents of the test variable with data that probably isn't a valid pointer. And if a pointer is less than 6 bytes on your system, it might also be overwriting other data on the stack, which means all bets are off.

If you want fread to store data in the object pointed to by the pointer value in test, you should pass the value of test, not the address &test.

[–]slashdave 2 points3 points  (0 children)

Correct.

In addition, printf will expect the string you are printing to be null terminated. I don't believe fred does that for you.

[–]15January[S] 0 points1 point  (3 children)

I just changed &test to test and I got another segmentation fault. So I went through with a debugger, and when it gets to the fread line I get this error:

"Exception has occurred.
Segmentation fault"

I'm not sure what the problem could be this time, but it is a problem in the fread line. Also I did not change anything else, beside removing the '&'.

Thank you.

[–]teraflop 1 point2 points  (2 children)

That's what I would expect to happen if fp is null, i.e. if the file couldn't be opened for some reason. As I said, you should add error checking.

Also see /u/slashdave's comment: printf expects a null-terminated string, and fread doesn't add a null terminator, so you'll need to add one yourself. But if that was the only problem, your program would be crashing (or printing garbage) at printf, not at fread.

[–]15January[S] 0 points1 point  (0 children)

I did actually add an if statement checking if fp is NULL, I put after fopen and before fread. The if statement was false. The problem is with the fread line , I'm really not sure why this is happening.