You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
_Notice that this uses `g++` instead of `gcc`. This because the `gtest`
109
+
is technically a C++ library, but it also works for "plain" C code, which
110
+
is all we need it for here. The `-g` flag isn't strictly necessary; it
111
+
causes a variety of useful debugging information to be included in
112
+
the executable, however, which can be *extremely* helpful when using
113
+
tools like `valgrind` or the `gdb` debugger. If you don't include it,
114
+
for example, then those tools won't be able to report accurate or useful
115
+
line numbers or function names. The `-lgtest` tells the compiler to include
116
+
the `gtest` library (that's the `-l` part) when generating the executable.
118
117
119
118
---
120
119
121
120
# The problems
122
121
123
122
:bangbang: Remember: For each problem you should at a minimum
123
+
124
124
* Pass our tests, and
125
125
* Have _no_ memory leaks, as confirmed by `valgrind`.
126
-
* Remove any print statements that you used to debug your code before you turn it in.
126
+
* Remove any print statements or other code that you used to debug your code before you turn it in.
127
127
128
128
Also, please don't lose your brains and forget good programming practices just because you're working in a new language. C can be quite difficult to read under the best of circumstances, and using miserable names like `res`, `res2`, and `res3` doesn't help. *Use functions* to break up complicated bits of logic; it's really not fun when a group turns in a solution that is one huge function, especially when there are several instances of repeated logic.
129
129
130
130
Some things to watch our for:
131
+
131
132
* In the past there has been strong inverse correlation between length
132
133
and correctness on these problem. If you find yourself wandering off into 2
133
134
or (especially!) 3 pages of code for any of these, you've likely lost the plot
134
135
and should probably ask for some help.
135
136
* Make sure you initialize all variables (including variables used to index arrays in loops). C won't give you an error if you fail to initialize something, and sometimes you can get lucky and your tests will accidentally pass because, at least that one time, you happened to get the "right" initial value. That doesn't mean your code is correct, though.
136
137
* Make sure you allocate space for the null terminator `\0` when allocating space for strings.
137
138
138
-
[There are more comprehensive tips and suggestions here.](https://github.umn.edu/UMM-CSci-Systems/C-Lab-Starter/blob/master/Tips_and_suggestions.md)
139
-
140
-
## Fixing palindromes
141
-
142
-
Before you start writing your own C code, we'll start by using valgrind
143
-
to identify memory leaks in an existing program. In the
144
-
`palindrome` directory there is a program that
145
-
determines (in sort of a dumb way) if a string is a palindrome. The file
146
-
`palindrome.c` has the code that checks for palindromes and (instead of
147
-
doing the more obvious thing of returning a boolean) returns the string
148
-
"Yes" or "No". The file `palindrome_test.c` uses the CMockery library
149
-
mentioned above to test that the `palindrome` function works. You should
150
-
go into that `palindrome` directory in your project and compile the
Copy file name to clipboardExpand all lines: Tips_and_suggestions.md
+3-11Lines changed: 3 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,9 +46,9 @@ Several groups had off-by-one errors where they weren't allocating
46
46
where the null terminator would need to be, but which we never
47
47
allocated space for.
48
48
49
-
A number of groups had a subtle mistake where they
49
+
A number of groups had a subtle mistake where they went through something like the following sequence of steps:
50
50
51
-
1. Dynamically allocated an array that was potentially empty: `int *a = calloc(n, sizeof(int));`.
51
+
1. Dynamically allocated an array that was potentially empty (because _n=0_): `int *a = calloc(n, sizeof(int));`.
52
52
1. Copied some data into that array using a loop that (correctly) did nothing if *n=0*.
53
53
1. Accessed the first item in the array, which might not actually be there, e.g., `int i = a[0];`.
54
54
1. Protected the remainder of the code (through an `if` or loop with appropriate bounds) so that the value taken from the array was never *used* if n=0.
@@ -85,11 +85,6 @@ Use functions to break things up! Many of the submissions have
85
85
three loops in their `array_merge` function, with several people
86
86
getting as high as 6. I'd have a look at that if I were you.
87
87
88
-
On a related note, *many* people have their vowel check in
89
-
`disemvowel` as a huge long chain of `||` statements. Pull that
90
-
stuff out into a named function, *especially* if you end up
91
-
repeating it!
92
-
93
88
# Style and clarity: Odds and ends
94
89
95
90
You should remove printing code from "production" code (i.e.,
@@ -120,7 +115,4 @@ Several people did a variant of linear search to see if an item was
120
115
duplicated. Use the fact that your array is sorted (or go ahead and
121
116
sort it if it isn't) to simplify this (and speed it up, but the
122
117
simplicity is arguably the bigger issue).
123
-
124
-
---
125
-
126
-
These notes were started by Vincent Borchardt, 16 Aug 2012
0 commit comments