Memory Arrangement - PowerPoint PPT Presentation

About This Presentation
Title:

Memory Arrangement

Description:

Memory Arrangement. Memory is arrange in a sequence of addressable units (usually bytes) ... sizeof( Type ) return the number of units it takes to store a ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 49
Provided by: ronygol
Category:

less

Transcript and Presenter's Notes

Title: Memory Arrangement


1
Memory Arrangement
  • Memory is arrange in a sequence of addressable
    units (usually bytes)
  • sizeof( ltTypegt ) return the number of units it
    takes to store a type.
  • sizeof(char) 1
  • sizeof(int) 4 (on most of our machines)

2
Memory
  • int main()
  • char c
  • int i,j
  • double x

c
i
j
x
3
Arrays
  • Defines a block of consecutive cells
  • int main()
  • int i
  • int a4

i
a0
a1
a2
a3
4
Arrays
  • C does not provide any run time checks
  • int a4
  • a-1 0
  • a4 0
  • This will compile and run (no errors)
  • but can lead to unpredictable results.
  • It is the programmers responsibility to check
    whether the index is out of bounds

5
Arrays
  • C does not provide array operations
  • int a4
  • int b4
  • a b // illegal
  • if( a b ) // illegal

6
Array Initialization
  • int arr3 3, 4, 5 // Good
  • int arr 3, 4, 5 // Good - The same
  • int arr4 3, 4, 5 // Good - The last is 0
  • int arr2 3, 4, 5 // Bad
  • int arr23 2,5,7,4,6,7 // Good
  • int arr23 2,5,7,4,6,7 // Good - The
    same
  • int arr32 2,5,7,4,6,7 // Bad
  • int arr3
  • arr 2,5,7 // Bad - array assignment only
    in initialization

7
Pointers
  • int main()
  • int i,j
  • int x // x points to an integer
  • i 1
  • x i
  • j x
  • x j
  • (x) 3

i
j
x
1
8
Pointers
  • int main()
  • int i,j
  • int x // x points to an integer
  • i 1
  • x i
  • j x
  • x j
  • (x) 3

i
j
x
1
0x0100
0x0100
9
Pointers
  • int main()
  • int i,j
  • int x // x points to an integer
  • i 1
  • x i
  • j x
  • x j
  • (x) 3

i
j
x
1
0x0100
1
0x0100
10
Pointers
  • int main()
  • int i,j
  • int x // x points to an integer
  • i 1
  • x i
  • j x
  • x j
  • (x) 3

i
j
x
1
0x0104
1
0x0100
11
Pointers
  • int main()
  • int i,j
  • int x // x points to an integer
  • i 1
  • x i
  • j x
  • x j
  • (x) 3

i
j
x
1
0x0104
3
0x0100
12
Pointers
  • Declaration
  • lttypegt p
  • p points to objects of type lttypegt
  • Reference
  • x - the pointer to x
  • DeReference
  • p x
  • y p
  • p refers to the object p points to

13
Example the swap function
Does nothing Works
void swap(int a, int b) int temp a a
b b temp . int main() int x, y x
3 y 7 swap(x, y) // now x3, y7 .
void swap(int pa, int pb) int temp pa
pa pb pb temp . int main()
int x, y x 3 y 7 swap(x, y) // x
7, y 3
14
Pointers Arrays
  • int p
  • int a4
  • p a0
  • (p1) 1 // assignment to a1!

p
a1
a2
a3
a0
15
Pointers arrays
  • Arrays are essentially constant pointers
  • int p
  • int a4
  • p a // same as p a0
  • p1 102 // same as (p1)102
  • (a1) 102 // same
  • p // p a1 a1
  • a p // illegal
  • a // illegal

16
Pointers Arrays
  • int foo( int p )
  • and
  • int foo( int a )
  • Are declaring the same interface
  • In both cases, a pointer to int is being passed
    to the function foo

17
Pointer Arithmetic
  • int a4
  • int p a
  • char q (char )a // Explicit cast
  • // p and q point to the same location
  • p // increment p by 1 int (4 bytes)
  • q // increment q by 1 char (1 byte)

a1
a2
a3
a0
q
p
18
Pointer arithmetic
  • int FindFirstNonZero( int a, int n )
  • int p
  • for( p a (p lt an) ((p) 0) p )
  • return p-a
  • Same as
  • int FindFirstNonZero( int a, int n )
  • int i
  • for( i 0 (i lt n) (ai 0) i )
  • return i

19
void
  • void p defines a pointer to undetermined type
  • int j
  • int p j
  • void q p // no cast needed
  • p (int)q // cast is needed

20
NULL pointer
  • Special value uninitialized pointer
  • int p NULL
  • if( p ! NULL )

21
  • Strings in C

22
C String
  • String
  • char
  • usually 1 byte.
  • An Integer (ASCII code).
  • String
  • An array of characters.
  • char txt1 text
  • char txt2 text
  • char txt3 t,e,x,t,\0

23
C Strings
  • Strings are always terminated by a null
    character, (a character with integer value 0
    equals to the special character \0).
  • There is no way to enforce it when you do your
    own strings ?
  • Remember its there
  • Allocate memory for it

24
C String Example
  • char text string
  • // means text5 g and text6 \0
  • // 7 chars are allocated!

25
C Strings More Examples
  • Recall arrays are essentially constant pointers.
  • char txt1 textchar txt2
    textint i strlen(txt1) // i 4, same for
    strlen(txt2)txt10 n //nexttxt1
    txt2 // illegal !txt2 txt1
  • //legal. now txt2 points to the same string.

26
C Strings Manipulation
  • To manipulate a single character use the
    functions defined in ctype.h
  • include ltctype.hgt
  • Manipulation of Strings is done by including the
    string.h header file
  • include ltstring.hgt
  • Read manual pages string and isalpha

27
Test yourself
  • What does this do? What are the assumptions ?
  • int f(const char p,const char q)
  • for(p p q p,q)
  • return p-q

28
Memory Organization
  • During run time, variables can be stored in one
    of three pools
  • Stack
  • Static heap
  • Dynamic heap

29
Stack
  • Maintains memory during function calls
  • Argument of the function
  • Local variables
  • Call Frame
  • Variables on the stack have limited life time

30
Stack - Example
ltcallgt
a
  • int foo( int a, double f )
  • int b

f
b
31
Stack - Example
ltcallgt
a
  • int foo( int a, double f )
  • int b
  • int c

f
b
32
Stack - Example
ltcallgt
a
  • int foo( int a, double f )
  • int b
  • int c

f
b
c
33
Stack - Example
ltcallgt
a
  • int foo( int a, double f )
  • int b
  • int c

f
b
c
34
Stack - Example
ltcallgt
a
  • int foo( int a, double f )
  • int b
  • int c

f
b
c
35
Stack recursive example
  • void foo( int depth )
  • int a
  • if( depth gt 1 )
  • foo( depth-1 )
  • int main()
  • foo(3)

36
Stack errors?
  • void foo( int depth )
  • int a
  • if( depth gt 1 )
  • foo( depth )
  • Will result in run time error
  • out of stack space

37
Static heap
  • Memory for global variables
  • include ltstdio.hgt
  • const int ListOfNumbersSize 1000
  • int ListOfNumbersListOfNumbersSize
  • int main()

38
Static heap
  • Variables on the static heap are defined
    throughout the execution of the program
  • Memory on the static heap must be defined at
    compile time

39
Static heap reverse example
  • Example program to reverse the order of lines of
    a file
  • To this task, we need to
  • read the lines into memory
  • Print lines in reverse
  • How do we store the lines in memory?

40
Static heap reverse example
  • const int LineLength 100
  • const int NumberOfLines 10000
  • char LinesNumberOfLinesLineLength
  • int main()
  • int n ReadLines()
  • for( n-- n gt 0 n-- )
  • printf(s\n, Linesn)

41
Static heap reverse example
  • This solution is problematic
  • The program cannot handle files larger than these
    specified by the compile time choices
  • If we set NumberOfLines to be very large, then
    the program requires this amount of memory even
    if we are reversing a short file
  • ?Want to use memory on as needed basis

42
Dynamic Heap
  • Memory that can be allocated and freed by the
    program during run time
  • The program controls how much is allocated and
    when
  • Limitations based on run-time situation
  • Available memory on the computer

43
Allocating Memory from Heap
  • void malloc( size_t Size )
  • Returns a pointer to a new memory block of size
    Size
  • Returns NULL if it cannot allocate memory of this
    size

44
Example strdup
  • Function to duplicate a string
  • char
  • strdup( char const p )
  • int n strlen(p)
  • char q (char)malloc(sizeof(char)(n1))
  • if( q ! NULL )
  • strcpy( q, p )
  • return q
  • This function is part of the standard library

45
Memory Management
  • void
  • foo( char const p )
  • char q strdup( p )
  • // do something with q
  • The allocated memory remains in use
  • cannot be reused later on


Heap
46
De-allocating memory
  • void free( void p )
  • Returns the memory block pointed by p to the pool
    of unused memory
  • No error checking!
  • If p was not allocated by malloc, undefined
    behavior

47
Example of free
  • void
  • foo( char const p )
  • char q strdup( p )
  • // do something with q
  • free(q)
  • This version frees the allocated memory

48
Further Knowledge
  • Read manual page of
  • malloc
  • calloc
  • realloc
  • free
Write a Comment
User Comments (0)
About PowerShow.com