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.