0

I have a little problem. I can get get this to pass all the checks, but only if I remove the free() statement on L71

Link to code removed by andy5995

If I leave the free() statement in, this is the output of check50:

$ check50 2015.fall.pset2.caesar caesar.c
:) caesar.c exists
:) caesar.c compiles
:( encrypts "a" as "b" using 1 as key
   \ expected output, not standard error of "======= Backtrace: =========\n======= M..."
:( encrypts "barfoo" as "yxocll" using 23 as key
   \ expected output, not standard error of "======= Backtrace: =========\n"
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
   \ expected output, not standard error of "======= Backtrace: =========\n======= M..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
   \ expected output, not standard error of "======= Backtrace: =========\n======= M..."
:( encrypts "barfoo" as "onesbb" using 65 as key
   \ expected output, not standard error of "======= Backtrace: =========\n======= M..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
   \ expected output, not standard error of "======= Backtrace: =========\n======= M..."
:) handles lack of argv[1]

The free() isn't entirely necessary in this case, as the malloc(ed) memory should be freed when the program exits, but when I read this post on StackOverflow, I get the impression it's better to explicitly free() memory, as opposed to only relying on the OS: When you exit a C application, is the malloc-ed memory automatically freed?

My question is, does anyone know why this fails? For now, I've removed the free() statement and plan to submit the program without it.

EDIT: This the link I referenced for how I used malloc() in this example: Lesson 6: Pointers in C

Upon further research, I think it would be better to use something like char *text = (char *)malloc (sizeof (char) * 256); See Sizeof and storage allocation

I tried that code, but in either case, the result of check50 is the same. The strange thing is, the errors don't happen when I run the program with the test situations that check50 does. There's no segfault, and when I run it locally, there's no indication in my system logs of any segfaults or unusual behavior.

I don't want to confuse anyone. I don't have to use malloc() or sizeof at all; I am using it for practice, and apparently it's working because I've learned a bit more.

1 Answer 1

1

It appears that check50 is using a slightly older version of the CS50 Library than the CS50 IDE is currently using. In that version, all strings created using GetString are freed for you before the program finishes. So when your program hits the free(text), the program segfaults with a 'double free' error.

You don't see this in the IDE because the CS50 Library used there doesn't free those strings for you.

It is good practice to free any strings that have been malloced, but in this case, for the sake of check50 at this point, you don't have to do that.

1
  • FYI. check50 has been updated with the latest library, so feel free to free when using GetString.
    – curiouskiwi
    Oct 4, 2016 at 23:35

You must log in to answer this question.

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