Memory Layout, File I/O
description
Transcript of Memory Layout, File I/O
![Page 1: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/1.jpg)
Memory Layout, File I/O
Bryce Boe2013/06/27
CS24, Summer 2013 C
![Page 2: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/2.jpg)
Outline
• Review HW1 (+command line arguments)• Memory Layout• File I/O
![Page 3: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/3.jpg)
HW1 REVIEW
![Page 4: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/4.jpg)
HW1 Common Problems
• Taking input from stdin (via scanf)• Performing failure testing too late• Not handling the 0 case• Whitespace issues• Others?
![Page 5: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/5.jpg)
HW1 Solution
• <In-class review of hw1 solution source code>
![Page 6: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/6.jpg)
MEMORY LAYOUT
![Page 7: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/7.jpg)
View from three Levels
• The address space of a process• Function activation records on the stack• Data within an activation record
![Page 8: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/8.jpg)
Simplified process’s address space
STACK
HEAP
CODE+
0xFFFFFFFF
0x00000000
![Page 9: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/9.jpg)
Data Segments
• Code(+)– Program code– Initialization data, global and static variables
• Heap– Memory chunks allocated via malloc
• Stack– Function activation records
![Page 10: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/10.jpg)
Creating and destroying activation records
• Program starts with main• main calls strsafeconcat• strsafeconcat calls strlength• strlength returns• strsafeconcat calls strlength• strlength returns• strsafeconcat returns
main
strsafeconcat
strlength
top of stackHigh Memory
![Page 11: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/11.jpg)
Simplified function activation records
• Stores values passed into the function as parameters
• Stores the function’s local variables
![Page 12: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/12.jpg)
How many bytes is the simplified activation record?
int main(int argc, char *argv[]) {char buf[8];for (int i = 0; i < argc; ++i)
buf[i] = argv[i][0];}
![Page 13: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/13.jpg)
main’s simplified activation record
argc (int)4 bytes
argv (pointer)4 bytes
buf (char array)8 bytesi (int)
4 bytes
Total: 20 bytes
data stored
![Page 14: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/14.jpg)
Think about it
int main() {char msg[] = “hello”;char buf[] = “1234567”;char msg2[] = “world”;
}• What value does buf[8] hold?• What about msg[7]?
![Page 15: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/15.jpg)
Similar but different
int main() {int x = 0xDEADBEEF;char buf[] = “1234567”;
}
• What value does buf[8] hold?
![Page 16: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/16.jpg)
Inspecting addresses in a program
• <In class review of variables_in_memory.c>
![Page 17: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/17.jpg)
FILE I/O
![Page 18: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/18.jpg)
File I/O
• I/O stands for input/output• Provided by the stdio library (stdio.h)• Allows reading and writing to/from streams
(type: FILE *)– stdin (read only)– stdout / stderr (write only)– named files
![Page 19: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/19.jpg)
stdio.h
• Explore via opengroup: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/stdio.h.html
• Explore via `man stdio.h` (same info as opengroup, but not easily searchable or linkable)
![Page 20: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/20.jpg)
Opening named files and closing streams
• FILE* fopen(char *filename, char *mode)– open a file specifying whether to open for
reading, writing, appending, and create or truncate if desired
• int fclose(FILE *stream) – close an open FILE*– Flush and close the stream– Return 0 on success
![Page 21: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/21.jpg)
Reading from streams
• int fgetc(FILE *stream)– Returns the next character in the stream or EOF
(this is why it returns an int)• char *fgets(char *buf, int n, FILE *stream)– Read at most n-1 bytes from stream into buf– Also stops when ‘\n’ or EOF is reached– Terminates buf with the null character ‘\0’
![Page 22: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/22.jpg)
Other read functions
• fread – very useful (especially when input is unbounded), we won’t use in this class
• fscanf – useful, but tricky, don’t use in this class
• Functions with similar names less the ‘f’– Uses the stdin stream thus doesn’t require the
stream argument
![Page 23: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/23.jpg)
Writing to streams
• int fputc(int c, FILE *stream)– Write a single character c to stream
• int fputs(char *buf, FILE *stream)– Write the null-terminated string in buf to stream
• int fprintf(FILE *stream, char *format, …)– Write formatted string to stream making the
specified replacements
![Page 24: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/24.jpg)
SECURITY WARNING
• NEVER do the following:fprintf(stdout, buf); // buf is some c-string• Could allow an attacker to inspect and change
your program (format string exploit)• Use either fputs or fprintf(stdout, “%s”, buf)
![Page 25: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/25.jpg)
Other write functions
• fwrite – generally very useful, we won’t use in this class
• Functions with similar names less the ‘f’– Uses the stdout stream thus doesn’t require the
stream argument
![Page 26: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/26.jpg)
Other useful stream functions
• int feof(FILE *stream)– Return non-zero if the stream has reached the end
of the file• int fflush(FILE *stream)– Force writing any buffered data to the stream– Flushing typically occurs when a newline is
encountered, thus fflush is often needed when newlines aren’t used
![Page 27: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/27.jpg)
I/O Questions
• Why does fgetc/fputc return/get an integer?• If a file with only a single newline at the end is
32 bytes long, how many bytes does the buffer for fgets require to read the entire file?
![Page 28: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/28.jpg)
More I/O Questions
• When using fgets, how can you determine if the string is longer than the value of ‘n’ (the number of bytes to read)
• What will happen if the ‘n’ parameter to fgets is larger than the buffer?
![Page 29: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/29.jpg)
Real-time cat program writing
• <In class creation of simple_copy.c>
![Page 30: Memory Layout, File I/O](https://reader031.fdocuments.net/reader031/viewer/2022013105/56813dfd550346895da7d54b/html5/thumbnails/30.jpg)
For Tuesday
• Finish reading chapter 1 in the text book