Title: Plab Tirgul 4 structs
1Plab Tirgul 4structs arrays, file
I/O,debugging memory errors
2Assignment struct vs. pointer to struct
struct Complex double x,y
Complex cp1, cp2cp1 (Complex)malloc
...cp2 (Complex)malloc ...cp1-gtx7
cp1-gty3 cp2 cp1
Complex c1, c2c1.x7 c1.y3 c2 c1
x7
garbage
x7
x7
y3
garbage
y3
y3
c1
c2
cp1
cp2
3Arrays structs as arguments
- When an array is passed as an argument to a
function, the address of the 1st element is
passed. - Structs are passed by value, exactly as the basic
types.
4Arrays structs as arguments
struct MyStr int a10 void f(int a)
a7 89 void g(MyStr s)
s.a7 84
main() MyStr x x.a7 0 f(x.a)
printf("d\n", x.a7) g(x)
printf("d\n", x.a7)
Output 89 89
5argv argc
- To pass command line arguments to our program we
should use the following main declaration main(i
nt argc, char argv) ... - Compare to main(String args) in java.
- Unlike java the first argument is the name of
the program itself.
6argv argc example
- prog1 u danny p 1234argc 5 argv0
prog1 argv1 -u ... argv4
1234Always argvargc 0
7Default function arguments
- We can specify default value for trailing
arguments of a function. - An alternative to overloading.
- Exampleprint(int value, int base10) . .
.print(31, 16)print(31)// equivalent to
print(31,10) h(int x0, int y) // error!
Only in C
Only in C
8File I/O
- File I/O is mostly similar to stdin stdout I/O.
- Most I/O functions we encountered have a file
counterpart which receives a FILE pointer
(handle). - Examplesgetchar(void)
fgetc(FILE) scanf(const char ,...)
fscanf(FILE, const char,...)printf(const char
,...) fprintf(FILE, const char,...) - The standard streams (stdin, stdout, stderr) are
also of FILE type.
See related man pages fprintf, fscanf, etc.
9File I/O example mywc
- include ltstdio.hgtinclude lterrno.hgtinclude
ltctype.hgtmain(int argc, char argv)
FILE fp int wc 0, ch if (argc !
2) printf("Usage mywc
ltfilenamegt\n") exit(1)
errno 0 fp fopen(argv1, "r") - if (fp NULL) perror()
exit(1) -
10File I/O example
-
- while (1) while ((ch fgetc(fp)) ! EOF
isspace(ch)) if (ch
EOF) break wc
while ((ch fgetc(fp)) ! EOF !isspace(ch))
if (ch EOF)
break fclose(fp)
printf("There are d words in s\n", wc,
argv1) return 0 -
Related man pages fopen, fclose
11Memory related bugs
- Memory leaks.
- Accessing random/freed memory addresses (e.g.
off-by-one errors).
12malloc_stats()
- By including malloc.h you can use the
malloc_stats() function which prints to the
stderr information about the amount of used
memory. - Example
. . . malloc_stats() destroyDictionary(dict) mal
loc_stats() . . .
13malloc_stats() cntd.
With memory leak Arena 0 system bytes 8140
in use bytes 6860 . . . Arena 0 system
bytes 8140 in use bytes 4084 . . .
Without memory leak Arena 0system bytes
8124 in use bytes 6860. . . Arena 0system
bytes 8124 in use bytes 4 . . .
14mtrace
- Log all memory allocations to a file.
- The file name is contained in the MALLOC_TRACE
environment variable. - For example setenv MALLOC_TRACE
/plab/ex1/trace - Analyze the file to find memory leaks using the
mtrace utility. - The program must
- be compiled with g flag
- include ltmcheck.hgt
15mtrace example
The program include ltmcheck.hgt int main()
mtrace() // later we can call muntrace() . . .
The trace file looks like this Start _at_
0x80486fd 0x804a0e0 0x8 _at_ 0x804887d
0x804a0f0 0x8 _at_ 0x8048c7d 0x804a100 0x14 _at_
/lib/libc.so.6(__strdup0x29)0x400d7a29
0x804a118 0x5 _at_ 0x8048c7d 0x804a128 0x14 _at_
/lib/libc.so.6(__strdup0x29)0x400d7a29
0x804a140 0x3 _at_ 0x8048c7d 0x804a150 0x14. .
.
16mtrace example cntd.
The result of analysis ( mtrace ex1
trace) Memory not freed -----------------
Address Size Caller 0x0804a100 0x14 at
/home/mush/plab/ex1/strBinTree.c65 0x0804a128
0x14 at /home/mush/plab/ex1/strBinTree.c65
0x0804a150 0x14 at /home/mush/plab/ex1/strBinTree.
c65
Another example
Memory not freed ----------------- Address
Size Caller 0x0804a118 0x5 at /lib/libc.so.6(__s
trdup0x29)0x400d7a29 0x0804a140 0x3 at
/lib/libc.so.6(__strdup0x29)0x400d7a29
17MALLOC_CHECK_
- By setting this environment variable to 0, 1, 2
we can handle some bugs, most notably freeing
twice the same memory. - Usually double free causes segmentation fault.
- When MALLOC_CHECK_ is 0 freeing twice works.
- When MALLOC_CHECK_ is 1 an error message is
printed. - Example free() invalid pointer 0x80497b8!
- When MALLOC_CHECK_ is 2 the program (gracefully)
aborts.
18ElectricFence
- ElectricFence is a library which allows to catch
accesses to memory that was already freed, as
well as off-by-one errors. - It will cause the program to segfault in the
above cases, which is usually better than
continue running and have unpredictable errors
later.
Example char a (char)malloc(100sizeof(char))
. . . a100 'c' // ElectricFence will
cause segfault
19ElectricFence cntd.
Example Node n1 (Node)malloc(sizeof(Node)).
. . free(n1) . . . n1-gtx 7 //
ElectricFence will cause segfault
- To use ElectricFence you should link your program
with the efence library. - For exampleg prog1.o list.o read.o -lefence
20Commercial products
- Purify
- BoundsChecker
- MSs VisualStudio