Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
lecture materials
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
generic software skills
lecture materials
Commits
f08d0564
Commit
f08d0564
authored
11 months ago
by
Dominik Zobel
Browse files
Options
Downloads
Patches
Plain Diff
Unify compiler flags example with locating right error message
parent
3bc416f0
No related branches found
No related tags found
1 merge request
!56
Debugging Strategies lecture notes
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lectures/debugging-strategies/slides.qmd
+65
-113
65 additions, 113 deletions
lectures/debugging-strategies/slides.qmd
with
65 additions
and
113 deletions
lectures/debugging-strategies/slides.qmd
+
65
−
113
View file @
f08d0564
...
...
@@ -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_p
ointe
r.c:4:8: note: declared here
inv_p
t
r.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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment