Dynamic memory allocation - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Dynamic memory allocation

Description:

Always use Wall option and try to make your program compile ... brk(0) = 0x8b5a000. access('/etc/ld.so.preload', R_OK) = -1 ENOENT (No such file or directory) ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 44
Provided by: stormCis
Category:

less

Transcript and Presenter's Notes

Title: Dynamic memory allocation


1
Dynamic memory allocation system calls
  • CSRU3130
  • Ellen Zhang

2
Last class development tools
  • gcc
  • Always use Wall option and try to make your
    program compile cleanly
  • Make
  • Dependence rule, pattern rule
  • gdb
  • The program need to be compiled with g options
  • Debug a program as its running, after it crashes
    (core dump file), or by running it from gdb

3
Todays Topics
  • Memory related topics
  • C storage class
  • Dynamic memory allocation
  • GNU C Library malloc, free, etc.
  • Library calls for accessing directory
  • System calls
  • Low-level File access

4
A piece of C code
  • char Func(void)
  • main()
  • char Text1
  • Text1 Func()
  • printf (s\n, Text1)
  • char Func(void)
  • char Text210"martin"
  • return(Text2)

5
C Storage Class
  • Each variable has a scope
  • Visibility of the variable
  • local variable (block scope) or global variable
    (program scope)
  • Each variable has a storage classes
  • life time (duration) of variables and/or
    functions
  • auto , register, static, extern
  • Good resource Sams Teach Yourself C in 24 Hours

6
C Storage Class auto
  • auto - storage class means that the memory
    location of a variable is temporary
  • the default storage class for local variables.
  • Only local variables can be defined as auto
  • void funca( )
  • int Count
  • auto int Month

7
C Storage Class register
  • local variables that should be stored in a
    register for quick access instead of RAM.
  • the variable has a maximum size equal to register
    size (usually one word), cannot have '' operator
    applied to it (as it has no memory location).
  • register int Miles
  • It means that it MIGHT be stored in a register -
    depending on hardware and implimentation
    restrictions.

8
C Storage Class static
  • applied to variables with either block scope or
    program scope
  • default storage class for global variables
  • 'static' can also be used for local variables
  • the variable is initalised at compilation time
    (the value must be a constant) and retains its
    value between calls.
  • i.e., static local variable has a permanent
    duration

9
A piece of C code
  • char Func(void)
  • main()
  • char Text1
  • Text1 Func()
  • printf (s\n, Text1)
  • char Func(void)
  • char Text210"martin"
  • return(Text2)

9
CSRU 3130 Unix System Programming
10
Storage class extern
  • extern defines a global variable that is visable
    to ALL object modules.
  • When you use 'extern' the variable cannot be
    initalized as all it does is point the variable
    name at a storage location that has been
    previously defined.
  • Source 1 Source 2
  • -------------------------------------------
    ---------------------------
  • extern int count int count5
  • write() main()
  • printf("count is d\n", count) write()

11
Dynamic memory
  • Why need dynamic memory ?
  • Solutions
  • Dynamically allocate memory, use malloc (new in
    C)
  • Deallocate memory using free (delete in C)
  • Note
  • In java, garbage collection free programmers from
    the burden of deallocatin memory

12
Allocating memory in C
  • include ltstdio.hgt
  • void malloc(size_t size)
  • size_t a data type defined to represent the
    amount of memory a data object requires,
    expressed in units of char'. (unsigned int, or
    unsigned long, depending on implementation)
  • Note the allocated memory is not initialized
    (cleared)

13
sizeof operator
  • Some of you find out in hw5 that
  • A variable of type char takes up one byte
  • A variable of type int takes up 4 byte
  • You could have found out this more easily
  • Use sizeof operator followed by type name or
    variable name
  • int p,q
  • printf ("size of int is u\n", sizeof(int ))
  • printf ("size of a pointer is d\n",sizeof(p))
  • printf ("size of integer is u u\n",
    sizeof(int), sizeof(q))

14
Example of malloc
  • int iptr
  • iptr (int )malloc(sizeof(int))
  • if (iptr NULL)
  • .. ERROR ROUTINE GOES HERE ..
  • double d
  • d (double )malloc (sizeof(char))
  • if (dNULL)
  • printf ("Failed to malloc\n")
  • exit(1)
  • d10000.21324
  • printf ("d is point to lf\n",d)

15
Array Pointers
  • int array23, 12, 34
  • When you use the name of an array in your code,
    you actually use a pointer to its first element
    (in C terms, array0).
  • This is called decaying the array decays to
    a pointer.

16
  • main()
  • int num_array101,2,3
  • int p
  • int q
  • printf ("num_array is u, size is
    d\n",num_array,
  • sizeof num_array)
  • printf ("num_array is u or p, the
    content is d\n",
  • num_array, num_array,
    (num_array))
  • printf ("num_array1 is u or p, the
    content is d\n",
  • num_array1, num_array1,
    (num_array1))
  • printf ("num_array2 is u or p, the
    content is d",
  • num_array2, num_array2,
    (num_array2))

num_array is 3215120768, size is 40 num_array is
3215120768 or 0xbfa2d980, the content is
1 num_array1 is 3215120772 or 0xbfa2d984, the
content is 2 num_array2 is 3215120776 or
0xbfa2d988, the content is 3
17
Array Pointers
  • int array 45, 67, 89
  • int array_ptr array1
  • printf("i\n", array_ptr1)

18
Dynamic allocated array
  • int iptr
  • iptr (int )malloc(10 sizeof(int))
  • if (iptr NULL)
  • .. ERROR ROUTINE GOES HERE ..
  • Used same way as statically allocated array
  • int k
  • for (k 0 k lt 10 k)
  • iptrk 2
  • Enlarge/shrink the array using realloc()

19
calloc and realloc
  • include ltstdlib.hgt
  • void calloc(size_t nmemb, size_t size)
  • allocate unused space for an array of nmemb
    elements each of whose size in bytes is size. The
    space is initialized to 0.
  • void realloc(void ptr, size_t size)
  • changes size of memory object pointed to by ptr
    to size specified by size. Contents of object
    will remain unchanged up to the lesser of the new
    and old sizes.
  • If new size is larger, contents of newly
    allocated portion are unspecified.

20
Deallocate memory
  • Memory allocated using malloc etc. need to be
    freed explicitly, otherwise, program gets larger
    and larger (memory leak) and eventually runs out
    of memory
  • void free(void ptr)

21
Valgrind memcheck tool
  • Valgrind tool suite provides a number of
    debugging and profiling tools.
  • The most popular is Memcheck, a memory checking
    tool which can detect many common memory errors
    such as
  • Memory leaks
  • Touching memory you shouldn't (eg. overrunning
    heap block boundaries, or reading/writing freed
    memory).
  • Using values before they have been initialized.
  • Incorrect freeing of memory, such as
    double-freeing heap blocks.

22
Todays Topics
  • Memory related topics
  • C storage class
  • Dynamic memory allocation
  • GNU C Library malloc, free, etc.
  • Library calls for accessing directory
  • System calls
  • Low-level File access

23
File System directories
  • How would you implement file expansion function
    of bash ?
  • Expand by all files/directories under current
    directory
  • Directory, a special file, contains a list of
    file names and an indication of where they are
    located (inode)
  • Standard library calls opendir, readdir,
    writedir (man 3 opendir)

24
A directory entry
  • include ltsys/types.hgt
  • include ltdirent.hgt
  • struct dirent
  • ino_t d_ino /
    inode number /
  • off_t d_off /
    offset to the next dirent /
  • unsigned short d_reclen /
    length of this record /
  • unsigned char d_type / type
    of file /
  • char d_name256 /
    filename /

25
Code Example
  • DIR dip struct dirent dit
  • if ((dip opendir(argv1)) NULL)
  • perror("opendir")
  • return 0
  • printf("Directory stream is now open\n")
  • while ((dit readdir(dip)) ! NULL)
  • i
  • printf("\ns", dit-gtd_name)
  • printf("\n\nreaddir() found a total of i
    files\n", i)
  • if (closedir(dip) -1)
  • perror("closedir")
  • return 0

26
Todays Topics
  • Memory related topics
  • C storage class
  • Dynamic memory allocation
  • GNU C Library malloc, free, etc.
  • Library calls for accessing directory
  • System calls
  • overview
  • Low-level File access
  • Strace tool

27
What is Operating System?
  • From app. programmers point of view
  • O.S. manages hardware resources
  • O.S. provides user programs with a simpler
    interface, i.e. system calls
  • cntread(fd, buffer,nbytes)

User level applications
System calls
Operating System
Physical machine interface
Hardware one or more processors, main memory,
disks, printers, keyboard, display, network
interface, etc.
28
Layers in UNIX/Linux System
Users
library interface
Standard utility programs (shell, editors,
compilers)
POSIX 1003.2
System call interface
Standard library (open, close, read, write, fork,
etc)
POSIX
Unix/Linux kernel
(process management,
memory management, file system, I/O, etc)
  • Hardware

    (CPU, memory, disks, terminals, etc)

29
System calls overview
  • We will learn the following system calls
  • Low level file I/O foundations beneath library
    routines such as fopen and putc.
  • Processes how to run programs from within a
    program.
  • Pipe use pipe for inter-process communications

30
Lowest-level I/O
  • All input and output is done by reading/writing
    files
  • All peripheral devices, e.g. terminals, are files
    in the file system
  • A single interface handles all communication
    between program and I/O device
  • Standard library I/O is built based on low-level
    I/O system call

31
Remember standard file I/O ?
  • FILE fopen(const char path, const char mode)
  • FILE a data structure containing info. needed
    to perform input or output operations on it,
    including
  • a file descriptor (will study in low-level file
    access)
  • current stream position
  • an end-of-file indicator , an error indicator
  • a pointer to the stream's buffer, if applicable
  • fprintf, fscanf, fclose, fread, fwrite
  • all requires FILE as parameter
  • All have a name starting with f

32
low-level I/O open a file
  • include ltsys/types.hgt
  • include ltsys/stat.hgt
  • include ltfcntl.hgt
  • int open(const char pathname, int flags)
  • int open(const char pathname, int flags,
    mode_t mode)
  • Before reading/writing a file, open the file
  • Check existence, file permission
  • Return a non-negative file descriptor
  • A program has three open files
  • Standard input, file descriptor is 0
  • Standard output, 1
  • Standard error, 2

33
low-level I/O read
  • include ltunistd.hgt
  • ssize_t read(int fd, void buf, size_t count)
  • attempts to read up to count bytes from file
    descriptor fd into buf.
  • On success, return number of bytes read (can be
    smaller than count, zero indicates end of file),
    and file position is advanced by this number.
  • On error, -1 is returned, and errno is set
    appropriately.

34
Sample low-level I/O write
  • include ltunistd.hgt
  • ssize_t write(int fd, const void buf, size_t
    count)
  • writes up to count bytes from the buffer pointed
    buf to the file fd.
  • Return number of bytes written on success (zero
    indicates nothing was written). Return -1 on
    error, and errno is set appropriately.

35
Example of read/write
  • define SIZE 512
  • main()
  • char bufSIZE
  • int n
  • while ( (nread(0,buf,sizeof buf))gt0 )
  • write(1,buf,n)
  • exit(0)

36
Code Example any problem ?
  • include ltunistd.hgt
  • include ltfcntl.hgt
  • define MAX_BUF 128
  • int main()
  • int fd
  • char bufMAX_BUF1
  • fdopen(myfile.txt,O_RDONLY)
  • read(fd,buf,MAX_BUF)
  • printf(read s\n,buf)
  • close(fd)

37
Strace system call tracer
  • diagnostic, instructional, and debugging tool.
  • Programmers will find that since system calls and
    signals are events that happen at the user/kernel
    interface, a close examination of this boundary
    is very useful for bug isolation, sanity checking
    and attempting to capture race conditions.

38
Basic Usage
  • strace command arg ...
  • runs specified command until it exits.
  • intercepts and records system calls which are
    called by a process and signals which are
    received by a process.
  • The name of each system call, its arguments and
    its return value are printed on standard error or
    to the file specified with the -o option.

39
Example of output
  • Each line in the trace contains system call name,
    followed by its arguments in parentheses and its
    return value.
  • An example from stracing the command ''cat
    /dev/null'' is
  • open("/dev/null", O_RDONLY) 3
  • Errors (typically a return value of -1) have the
    errno symbol and error string appended.
  • open("/foo/bar", O_RDONLY) -1 ENOENT (No such
    file or directory)

40
Example of output signals
  • Signals are printed as a signal symbol and a
    signal string.
  • An excerpt from stracing command sleep 1000 and
    killing the process
  • nanosleep(1000, 0, ltunfinished ...gt
  • killed by SIGKILL
  • More on signals next class

41
Example of using strace
  • include ltunistd.hgt
  • include ltfcntl.hgt
  • define MAX_BUF 128
  • int main()
  • int fd
  • char bufMAX_BUF1
  • fdopen(myfile.txt,O_RDONLY)
  • read(fd,buf,MAX_BUF)
  • printf(read s\n,buf)
  • close(fd)

42
Strace ./bad result
  • execve("./bad", "./bad", / 29 vars /) 0
  • brk(0)
    0x8b5a000
  • access("/etc/ld.so.preload", R_OK) -1
    ENOENT (No such file or directory)
  • open("/etc/ld.so.cache", O_RDONLY) 3
  • open("myfile.txt", O_RDONLY) -1
    ENOENT (No such file or directory)
  • read(-1, 0xbfee25df, 128) -1
    EBADF (Bad file descriptor)
  • fstat64(1, st_modeS_IFCHR0620,
    st_rdevmakedev(136, 1), ...) 0
  • mmap2(NULL, 4096, PROT_READPROT_WRITE,
    MAP_PRIVATEMAP_ANONYMOUS, -1, 0) 0xb7ff0000
  • write(1, "read \n", 6read
  • ) 6
  • close(-1) -1
    EBADF (Bad file descriptor)
  • exit_group(-1) ?

43
Final project
  • Timeline
  • What will you accomplish in each week ?
  • How will you divide the work among participants ?
  • What are the deliverable of each week ?
  • Final presentation on May 6th
Write a Comment
User Comments (0)
About PowerShow.com