Skip to content

Commit

Permalink
more explanations
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-taylor committed Jul 25, 2019
1 parent 0c3e4c6 commit cbff2a5
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 42 deletions.
17 changes: 8 additions & 9 deletions compiler_explanations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import copy, re, sys
import colors

DEFAULT_EXPLANATION_URL = "https://comp1511unsw.github.io/dcc/"
from drive_gdb import explanation_url

def get_explanation(message, colorize_output):
for e in explanations:
Expand Down Expand Up @@ -55,7 +54,7 @@ def __init__(self, label=None, precondition=None, regex=None, explanation=None,
def get(self, message, colorize_output):
explanation = self.get_short_explanation(message, colorize_output)
if explanation and (self.long_explanation or self.long_explanation_url):
url = self.long_explanation_url or DEFAULT_EXPLANATION_URL + self.label + '.html'
url = self.long_explanation_url or explanation_url(self.label)
explanation += "\n See more information here: " + url
return explanation

Expand Down Expand Up @@ -479,8 +478,8 @@ def get_short_explanation(self, message, colorize_output):
if (argc == 1)
return 42;
else if (argc == 1)
return 43;
else
return 43;
else
return 44;
}
""",
Expand All @@ -502,7 +501,7 @@ def get_short_explanation(self, message, colorize_output):
if (argc == 1)
return 42;
else
return 42;
return 42;
}
""",
),
Expand All @@ -522,7 +521,7 @@ def get_short_explanation(self, message, colorize_output):
if (argc > 1 || argc < 3)
return 42;
else
return 43;
return 43;
}
""",
),
Expand All @@ -542,7 +541,7 @@ def get_short_explanation(self, message, colorize_output):
if (argc > 1 && argc < 1)
return 42;
else
return 43;
return 43;
}
""",
),
Expand All @@ -562,7 +561,7 @@ def get_short_explanation(self, message, colorize_output):
if (argc > 1 ||argc > 1)
return 42;
else
return 43;
return 43;
}
""",
),
Expand Down
13 changes: 13 additions & 0 deletions docs/assign_function_to_int.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
You will get this error if for example, you write code like this::

```c
int a;
a = square;
```

when you wanted to do this:

```c
int a;
a = square(5);
```
27 changes: 27 additions & 0 deletions docs/assign_to_array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
You are not permitted to assign arrays in C.

You can NOT do this:

```c
#define ARRAY_SIZE 10

int array1[ARRAY_SIZE] = {0,1,2,3,4,5,6,7,8,9};
int array2[ARRAY_SIZE];

array2 = array1;
```
You can instead use a loop to copy each array element individually.
```c
#define ARRAY_SIZE 10
int array1[ARRAY_SIZE] = {0,1,2,3,4,5,6,7,8,9};
int array2[ARRAY_SIZE];
for (int i = 0; i < 10; i++) {
array2[ARRAY_SIZE] = array1[ARRAY_SIZE];
}
```
60 changes: 60 additions & 0 deletions docs/eof_byte.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Accidentally printing the special **EOF** value returned by the functions _getchar_, _getc_ and _fgetc_.

For example, this program prints the **EOF** value before the loop exits:

```c
#include <stdio.h>

int main(void) {
int c = 0;
while (c != EOF) {
int c = getchar();
putchar(c);
}
return 0;
}
```
The special **EOF** value typically is defined to be `-1` (in `<stdio.h>`)
and when printed is invisible. So the program appears to work.
```console
$ dcc cat.c
$ echo cat | ./a.out
cat
$
```

But the program will fail automated testing because it is printing an extra byte.

This is a program that output the same bytes as the above example.

```c
#include <stdio.h>

int main(void) {

putchar('c');
putchar('a');
putchar('t');
putchar('\n');
putchar(EOF);

return 0;
}
```
This is a program which doesn't print the EOF value:
```c
#include <stdio.h>
int main(void) {
int c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
return 0;
}
```
33 changes: 33 additions & 0 deletions docs/function-definition-not-allowed-here.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

If you forget a curly bracket, e.g.
```c
int sum(int x, int y) {
if (x > y) {
return x + y;
} else {
return x - y;
// <-- missing closing curly bracket
}

int f(int x) {
return sum(x, x);
}
```
The compiler will give an error when the next function definition starts.
You can fix by adding the missing curly bracket (brace):
```c
int sum(int x, int y) {
if (x > y) {
return x + y;
} else {
return x - y;
}
}
int f(int x) {
return sum(x, x);
}
```
32 changes: 32 additions & 0 deletions docs/function-variable-clash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
It is legal to have local variable with the same name as a function, but you can't then call the function
because the local variable declaration hides the function.

For example:

```c
int sum(int x, int y) {
return x + y;
}

int f(int sum) {
int square = sum * sum;

// error sum because sum is a variable
return sum(square, square);
}
```
You can fix the name clash by changing the name of the variable:
```c
int sum(int x, int y) {
return x + y;
}
int f(int a) {
int square = a * a;
// error sum because sum is a variable
return sum(square, square);
}
```
39 changes: 39 additions & 0 deletions docs/missing_newline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Forgetting to print a newline is a common mistake.

For example, this program doesn't print a newline.

```c
int main(void) {
int answer = 7 * 6;
printf("%d", answer);
}

```
So when its compiled and run you'll see something like this:
```console
$ dcc answer.c
$ ./a.out
42$
```

If you add a **\n** to the _printf_ like this:

```c
int main(void) {
int answer = 7 * 6;
printf("%d\n", answer);
}
```
It will fix the problem:
```console
$ dcc answer.c
$ ./a.out
42
$
```

43 changes: 43 additions & 0 deletions docs/stack_use_after_return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
A C function can not return a pointer to a local variable.

A local variable variable does not exist after the function returns.

For example, you can NOT do this:

```c
struct node {
struct node *next;
int data;
};

struct node *create_node(int i) {
struct node n;

n.data = i;
n.next = NULL;

return &n;
}
```
A function can return a pointer provided by malloc:
For example, you can do this:
```c
struct node {
struct node *next;
int data;
};
struct node *create_node(int i) {
struct node *p;
p = malloc(sizeof (struct node));
p->data = i;
p->next = NULL;
return p;
}
```
Loading

0 comments on commit cbff2a5

Please sign in to comment.