Skip to content
Snippets Groups Projects
Commit f08d0564 authored by Dominik Zobel's avatar Dominik Zobel
Browse files

Unify compiler flags example with locating right error message

parent 3bc416f0
No related branches found
No related tags found
1 merge request!56Debugging Strategies lecture notes
......@@ -320,11 +320,11 @@ Also gcc compile time checks and options like
## Look out for compiler warnings
## Look out for compiler warnings {.leftalign auto-animate=true}
::::::::{.columns}
:::{.column width=50%}
:::{.column width=48%}
```c
#include <stdlib.h>
......@@ -336,33 +336,84 @@ int main() {
free(ptr);
// ERROR: Trying to free
// memory from stack
// memory from stack
return 0;
}
```
Compiling with
`gcc inv_ptr.c -o inv_ptr`
:::
:::{.column width=50% .fragment}
:::{.column width=52% .fragment}
Compiler output
- Compilation only throws a warning
```
$ gcc inv_pointer.c -o inv_pointer
inv_pointer.c: In function ‘main’:
inv_pointer.c:8:4: warning: ‘free’ called on unallocated object ‘number’ [-Wfree-nonheap-object]
inv_ptr.c: In function ‘main’:
inv_ptr.c:8:4: warning: ‘free’ called on unallocated object ‘number’ [-Wfree-nonheap-object]
8 | free(ptr);
| ^~~~~~~~~
inv_pointer.c:4:8: note: declared here
inv_ptr.c:4:8: note: declared here
4 | int number = 1;
| ^~~~~~
```
- But running fails
Run output e.g.
```
Segmentation fault
```
:::
::::::::
## Look out for compiler warnings {.leftalign auto-animate=true}
::::::::{.columns}
:::{.column width=48%}
```c
#include <stdlib.h>
int main() {
int number = 1;
int* ptr;
ptr = &number;
free(ptr);
// ERROR: Trying to free
// memory from stack
return 0;
}
```
Compiling with
`gcc inv_ptr.c -Werror -o inv_ptr`
:::
:::{.column width=52%}
Compiler output
```
munmap_chunk(): invalid pointer
Aborted
inv_ptr.c: In function ‘main’:
inv_ptr.c:8:4: error: ‘free’ called on unallocated object ‘number’ [-Werror=free-nonheap-object]
8 | free(ptr);
| ^~~~~~~~~
inv_ptr.c:4:8: note: declared here
4 | int number = 1;
| ^~~~~~
cc1: all warnings being treated as errors
```
:::
......@@ -370,6 +421,7 @@ Aborted
::::::::
## Locating the right error message {.leftalign}
::::::::{.columns .smaller}
......@@ -460,106 +512,6 @@ Try with more than just one compiler
$\Rightarrow$ HPC systems usally provide native compiler plus gcc/gfortan. Try both variants.
## Compiler flags example {.leftalign auto-animate=true}
::::::::{.columns}
:::{.column width=45%}
```c
#include <stdlib.h>
int main() {
int number = 1;
int* ptr;
ptr = &number;
free(ptr);
// ERROR: Trying to free
// memory from stack
return 0;
}
```
Compiling with
`gcc inv_ptr.c -o inv_ptr`
:::
:::{.column width=55% .smaller}
Compiler output
```
inv_ptr.c: In function ‘main’:
inv_ptr.c:8:4: warning: ‘free’ called on unallocated object ‘number’ [-Wfree-nonheap-object]
8 | free(ptr);
| ^~~~~~~~~
inv_ptr.c:4:8: note: declared here
4 | int number = 1;
| ^~~~~~
```
Run output
```
Segmentation fault
```
:::
::::::::
## Compiler flags example {.leftalign auto-animate=true}
::::::::{.columns}
:::{.column width=45%}
```c
#include <stdlib.h>
int main() {
int number = 1;
int* ptr;
ptr = &number;
free(ptr);
// ERROR: Trying to free
// memory from stack
return 0;
}
```
Compiling with
`gcc inv_ptr.c -Werror -o inv_ptr`
:::
:::{.column width=55% .smaller}
Compiler output
```
inv_ptr.c: In function ‘main’:
inv_ptr.c:8:4: error: ‘free’ called on unallocated object ‘number’ [-Werror=free-nonheap-object]
8 | free(ptr);
| ^~~~~~~~~
inv_ptr.c:4:8: note: declared here
4 | int number = 1;
| ^~~~~~
cc1: all warnings being treated as errors
```
:::
::::::::
## Fixing run-time errors
- Produce sensible debug messages to determine code area with the issue
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment