Scientific Programming in C IV. Pointers

Size: px
Start display at page:

Download "Scientific Programming in C IV. Pointers"

Transcription

1 Scientific Programming in C IV. Pointers Susi Lehtola 1 November 2012

2 Pointers The feature at the heart of C are pointers, which are simply pointers to memory addresses. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 2/45

3 Memory Programs are run in the computer memory. The logical memory seen by the program is composed of physical memory (RAM), and virtual memory (disk swap). Logical memory Physical memory Virtual memory The kernel of the operating system performs the memory management, in which it tries to keep only the active stuff in the (fast) physical memory, and to cache the unneeded stuff onto the (slow) disk swap space. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 3/45

4 Memory management When a variable is declared, the operating system reserves some memory for its storage. free int k float x double y free In C you have direct access to the memory. You can get the memory address of a variable with the & operator, and deference a memory addess get its contents with the * operator. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 4/45

5 Example Pointers are declared as double x =1.0; double y=&x ; Now y is a pointer to a double, and it is initialized to the address of x. double x=1.0; double y=&x; other stuff double x 1.0 in memory y Scientific Programming in C, fall 2012 Susi Lehtola Pointers 5/45

6 Example, cont d #i n c l u de <s t d i o. h> i n t main ( void ) { double x =1.0; / y p o i n t s to the memory a d d r e s s o f x / double y=&x ; / P r i n t i n i t i a l v a l u e o f x / p r i n t f ( x = %f \n, x ) ; } / Set the v a l u e o f the memory s l o t to 2. 0 / y =2.0; p r i n t f ( x = %f \n, x ) ; return 0 ; Scientific Programming in C, fall 2012 Susi Lehtola Pointers 6/45

7 Example, cont d $. / a. out x = x = Scientific Programming in C, fall 2012 Susi Lehtola Pointers 7/45

8 Function arguments Function arguments are passed by value in C, in contrast to, e.g., Fortran, where they are passed by reference. #i n c l u de <s t d i o. h> void c h a n g e v a l u e ( i n t n ) { n=3; } i n t main ( void ) { i n t n=1; p r i n t f ( n=%i \n, n ) ; c h a n g e v a l u e ( n ) ; p r i n t f ( n=%i \n, n ) ; } Scientific Programming in C, fall 2012 Susi Lehtola Pointers 8/45

9 Function arguments, cont d The result is $. / a. out n=1 n=1 since the n modified in the change value function is just a local copy. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 9/45

10 Function arguments, cont d If you want the value to be changed, you need to supply a pointer: #i n c l u de <s t d i o. h> void c h a n g e v a l u e ( i n t n ) { n=3; } i n t main ( void ) { i n t n=1; p r i n t f ( n=%i \n, n ) ; c h a n g e v a l u e (&n ) ; p r i n t f ( n=%i \n, n ) ; } Scientific Programming in C, fall 2012 Susi Lehtola Pointers 10/45

11 Function arguments, cont d Now the result is the wanted one $. / a. out n=1 n=3 Scientific Programming in C, fall 2012 Susi Lehtola Pointers 11/45

12 sizeof You can get the size of a datatype with the sizeof function. #i n c l u de <s t d i o. h> i n t main ( void ) { p r i n t f ( s i z e o f ( f l o a t )=%u\n, s i z e o f ( f l o a t ) ) ; p r i n t f ( s i z e o f ( double)=%u\n, s i z e o f ( double ) ) ; p r i n t f ( s i z e o f ( i n t)=%u\n, s i z e o f ( i n t ) ) ; p r i n t f ( s i z e o f ( l o n g i n t)=%u\n, s i z e o f ( long i n t ) ) ; } p r i n t f ( s i z e o f ( f l o a t )=%u\n, s i z e o f ( f l o a t ) ) ; p r i n t f ( s i z e o f ( double )=%u\n, s i z e o f ( double ) ) ; p r i n t f ( s i z e o f ( i n t )=%u\n, s i z e o f ( i n t ) ) ; p r i n t f ( s i z e o f ( l o n g i n t )=%u\n,\ s i z e o f ( long i n t ) ) ; return 0 ; Scientific Programming in C, fall 2012 Susi Lehtola Pointers 12/45

13 sizeof, cont d On x86 s i z e o f ( f l o a t )=4 s i z e o f ( double )=8 s i z e o f ( i n t )=4 s i z e o f ( l o n g i n t )=4 s i z e o f ( f l o a t )=4 s i z e o f ( double )=4 s i z e o f ( i n t )=4 s i z e o f ( l o n g i n t )=4 The pointers are 4 bytes long, since the memory space is 32 bits 4 bytes * 8 bits/byte = 32 bits. The maximum memory size accessible to a program is thus 2 32 = bits = 4 GiB. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 13/45

14 sizeof, cont d However, on x86 64 the program results in s i z e o f ( f l o a t )=4 s i z e o f ( double )=8 s i z e o f ( i n t )=4 s i z e o f ( l o n g i n t )=8 s i z e o f ( f l o a t )=8 s i z e o f ( double )=8 s i z e o f ( i n t )=8 s i z e o f ( l o n g i n t )=8 Note that long int is 8 bytes, compared to 4 bytes on x86. The type is more accurate on x86 64 types in C are not portable. While int and float take only 4 bytes of memory, pointers to float take twice the amount of memory. The maximum amount of memory accessible on 64-bit architectures is 2 64 bits GiB. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 14/45

15 Arrays Arrays in C are pointers to the start of the array. 7 * sizeof(double) double p[7]; p[0] p[1] p[2] p[3] p[4] p[5] p[6] p The following is completely valid: double p [ 7 ] ; double q=p ; Here q is now explicitely defined as a pointer. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 15/45

16 Multidimensional arrays Multidimensional arrays are arrays, the elements of which are arrays. double t[2][3]; t t[0][0] t[0][1] t[0][2] t[1][0] t[1][1] t[1][2] t[0] t[1] C stores multidimensional arrays by rows, i.e. the last index runs the fastest (opposite to Fortran). Scientific Programming in C, fall 2012 Susi Lehtola Pointers 16/45

17 Static vs dynamic allocation When an array has a fixed size, it is allocated statically during compilation. However, you may run into problems with the stack size if you have large static arrays (especially common in Fortran 77 programs for which dynamic allocation is not possible). If you get segfaults with large static arrays, check the stack size limit. In the BASH shell, set ulimit -s unlimited and try again. In Windows, the stack size is set during the linker phase, and you need to increase it with, e.g. -Wl, stack, Scientific Programming in C, fall 2012 Susi Lehtola Pointers 17/45

18 Multidimensional arrays, cont d There are three possibilities in C to handle multidimensional arrays. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 18/45

19 Multidimensional arrays, cont d There are three possibilities in C to handle multidimensional arrays. 1. Use static allocation, defining the size of the array at compile time. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 19/45

20 Multidimensional arrays, cont d There are three possibilities in C to handle multidimensional arrays. 1. Use static allocation, defining the size of the array at compile time. 2. Use dynamic allocation to create a multidimensional array. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 20/45

21 Multidimensional arrays, cont d There are three possibilities in C to handle multidimensional arrays. 1. Use static allocation, defining the size of the array at compile time. 2. Use dynamic allocation to create a multidimensional array. 3. Use dynamic allocation to create a one-dimensional array, and handle the indexing yourself, e.g., with a macro. Which one is the fastest? Scientific Programming in C, fall 2012 Susi Lehtola Pointers 21/45

22 Multidimensional arrays, cont d There are three possibilities in C to handle multidimensional arrays. 1. Use static allocation, defining the size of the array at compile time. 2. Use dynamic allocation to create a multidimensional array. 3. Use dynamic allocation to create a one-dimensional array, and handle the indexing yourself, e.g., with a macro. Which one is the fastest? Macros are defined as, e.g., #define sq ( x ) ( ( x ) ( x ) ) Scientific Programming in C, fall 2012 Susi Lehtola Pointers 22/45

23 Multidimensional arrays, cont d There are three possibilities in C to handle multidimensional arrays. 1. Use static allocation, defining the size of the array at compile time. 2. Use dynamic allocation to create a multidimensional array. 3. Use dynamic allocation to create a one-dimensional array, and handle the indexing yourself, e.g., with a macro. Which one is the fastest? Macros are defined as, e.g., #define sq ( x ) ( ( x ) ( x ) ) Why the need for extra parentheses? Scientific Programming in C, fall 2012 Susi Lehtola Pointers 23/45

24 Multidimensional arrays, cont d There are three possibilities in C to handle multidimensional arrays. 1. Use static allocation, defining the size of the array at compile time. 2. Use dynamic allocation to create a multidimensional array. 3. Use dynamic allocation to create a one-dimensional array, and handle the indexing yourself, e.g., with a macro. Which one is the fastest? Macros are defined as, e.g., #define sq ( x ) ( ( x ) ( x ) ) Why the need for extra parentheses? Since the preprocessor just does string replacement, without the parentheses calling, e.g. sq(x+1) would result in x + 1 * x + 1, i.e., 2*x+1. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 24/45

25 Dynamic memory allocation Dynamic memory allocation in C is performed using the void m a l l o c ( s i z e t s i z e ) ; void c a l l o c ( s i z e t nmemb, s i z e t s i z e ) ; functions, defined in stdlib.h. void is a dummy datatype used to make the functions totally general. The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 25/45

26 Dynamic memory allocation, cont d The memory that has been dynamically allocated must be freed when it is no longer used. This is done with the void f r e e ( void p t r ) ; function, also defined in stdlib.h. The free() function frees the memory space pointed to by ptr, which must have been reserved by malloc() or calloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 26/45

27 Dynamic memory allocation, cont d If you need to grow or reduce the amount of allocated memory, you can use the void r e a l l o c ( void ptr, s i z e t s i z e ) ; function, still defined in stdlib.h. The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old size, the added memory will not be initialized. If ptr is NULL, then the call is equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 27/45

28 Dynamic memory allocation, example #i n c l u de < s t d l i b. h> i n t main ( void ) { / Amount o f e l e m e n t s i n the a r r a y / s i z e t N=1000; / P o i n t e r to the a r r a y, i n i t i a l i z e to n u l l / double p=null ; / A l l o c a t e memory f o r the a r r a y / p=m a l l o c (N s i z e o f ( double ) ) ; / Double the amount o f a l l o c a t e d memory / p=r e a l l o c ( p, 2 N s i z e o f ( double ) ) ; / Free the memory / f r e e ( p ) ; return 0 ; } Scientific Programming in C, fall 2012 Susi Lehtola Pointers 28/45

29 Dynamic memory allocation, cont d Whenever you use dynamic allocation, be sure to check that the allocation was succesful, i.e. that the returned pointer is not NULL. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 29/45

30 Dynamic memory allocation, cont d In order for a function to return an array, you must use dynamic memory allocation. double a r r ( ) { double a [ 1 0 ] ; return a ; } This function returns a pointer to the start of a. However, a is a local variable, and thus when the value is returned, the array doesn t exist anymore. double a r r ( ) { double a=m a l l o c (10 s i z e o f ( double ) ) ; return a ; } Scientific Programming in C, fall 2012 Susi Lehtola Pointers 30/45

31 Multidimensional arrays, dynamical allocation #i n c l u de < s t d l i b. h> i n t main ( void ) { / S i z e o f the a r r a y / s i z e t M=20, N=10; s i z e t i ; / P o i n t e r to the a r r a y o f p o i n t e r s / double p=null ; / A l l o c a t e memory f o r the p o i n t e r a r r a y / p=m a l l o c (M s i z e o f ( double ) ) ; / I n i t i a l i z e the p o i n t e r a r r a y / f o r ( i =0; i <M; i ++) p [ i ]= m a l l o c (N s i z e o f ( double ) ) ; / Now the e l e m e n t s can be a c c e s s e d as p [ i ] [ j ]. Free the memory / f o r ( i =0; i <M; i ++) f r e e ( p [ i ] ) ; f r e e ( p ) ; return 0 ; } Scientific Programming in C, fall 2012 Susi Lehtola Pointers 31/45

32 Multidimensional arrays, dynamical allocation What does this look like in memory? int i; double **p=malloc(3*sizeof(double *)); for(i=0;i<3;i++) p[i]=malloc(4*sizeof(double)); double * double * double * p p[0] p[1] p[2] double double double double double double double double double double double double Scientific Programming in C, fall 2012 Susi Lehtola Pointers 32/45

33 Multidimensional arrays, dynamical allocation Allocation of the pointer arrays is done in small blocks, so the memory might be non-continguously allocated. If you do a lot of malloc()s and free()s of different sizes, you will quickly end up trashing your memory. Even though there is a substantial amount of free memory still left, it s in bits and pieces all over, and no large blocks are free. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 33/45

34 Multidimensional arrays, motivation for manual indexing Quite often one can run into situations, where manual indexing comes in handy. For example, the integrals that occur in quantum chemistry have an awful lot of indices. When a Gaussian basis set is used, integrals are calculated over functions χ GTO klm (r) = χgto k (x) χ GTO l (y) χ GTO m (z), k (x) = ( ) 2α 1/4 (4α) k π (2k 1) x k exp ( αx 2). χ GTO As you can see, every function has 3 indices (and a specific value of α). Functions are added in shells with k + l + m = constant, sharing a constant value of α. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 34/45

35 Multidimensional arrays, motivation for manual indexing The simplest integral is the overlap integral S ij = χ i (r)χ j (r)d 3 r Here i and j are compound indices, that include both the cartesian indices k, l, m and the exponent α. The integrals factorize in x, y and z, and since, e.g. x χ GTO k (x) = aχ GTO k 1 (x) + bχgto k+1 (x) recursion relations can be come up with. With the recursion relations it s possible to quickly calculate all of the wanted integrals. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 35/45

36 Multidimensional arrays, motivation for manual indexing Even the overlap integral has now 2 3 = 6 indices. Even harder are the electron repulsion integrals χ i (r)χ j (ij kl) = (r)χ k(r )χ l (r ) r r d 3 rd 3 r where the recursion relations now have 4 3 = 12 indices. For the overlap we just want a matrix, so we have to come up with a scheme for compounding the three cartesian indices into one. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 36/45

37 Multidimensional arrays, manual indexing Use of manual indexing instead of pointer arrays guarantees that the memory is continguous. If you want, you can also switch the storage order to match with that of Fortran. #i n c l u de < s t d l i b. h> #define i d x ( a r r, i, j ) ( ( j ) a r r.m+( i ) ) #define e l ( a r r, i, j ) ( a r r. p [ i d x ( a r r, i, j ) ] ) i n t main ( void ) { s t r u c t a r r a y { s i z e t M; s i z e t N; double p ; } ; s t r u c t a r r a y a ; a.m=10; a.n=20; a. p=m a l l o c ( i d x ( a, a.m, a.n) s i z e o f ( double ) ) ; f r e e ( a. p ) ; return 0 ; } Scientific Programming in C, fall 2012 Susi Lehtola Pointers 37/45

38 Memory leaks When you use dynamic memory allocation in C, you have to make sure that you free the memory you have allocated otherwise you will be leaking memory. For example #i n c l u de < s t d l i b. h> i n t main ( void ) { s i z e t N=1000; double p=m a l l o c (N s i z e o f ( double ) ) ; / Memory l e a k h e r e / p=m a l l o c (2 N s i z e o f ( double ) ) ; f r e e ( p ) ; return 0 ; } Scientific Programming in C, fall 2012 Susi Lehtola Pointers 38/45

39 Memory leaks You can find memory leaks with, e.g., Valgrind, which is available for Linux and MacOS X. $ valgrind./ memleak.x ==27142== Memcheck, a memory error detector ==27142== Copyright (C) , and GNU GPL d, by Julian Seward et al. ==27142== Using Valgrind and LibVEX ; rerun with -h for copyright info ==27142== Command :./ memleak.x ==27142== ==27142== ==27142== HEAP SUMMARY : ==27142== in use at exit : 8,000 bytes in 1 blocks ==27142== total heap usage : 2 allocs, 1 frees, 24,000 bytes allocated ==27142== ==27142== LEAK SUMMARY : ==27142== definitely lost : 8,000 bytes in 1 blocks ==27142== indirectly lost : 0 bytes in 0 blocks ==27142== possibly lost : 0 bytes in 0 blocks ==27142== still reachable : 0 bytes in 0 blocks ==27142== suppressed : 0 bytes in 0 blocks ==27142== Rerun with --leak - check=full to see details of leaked memory ==27142== ==27142== For counts of detected and suppressed errors, rerun with : -v ==27142== ERROR SUMMARY : 0 errors from 0 contexts ( suppressed : 2 from 2) Scientific Programming in C, fall 2012 Susi Lehtola Pointers 39/45

40 Dynamically allocated memory In contrast to Fortran arrays, in C there s no way to find out how much memory has been allocated for an array you have to keep track of this yourself. Statically allocated arrays form an exception: #i n c l u de <s t d i o. h> #i n c l u de < s t d l i b. h> i n t main ( void ) { double p [ 1 0 ] ; double q=p ; double r=m a l l o c (10 s i z e o f ( double ) ) ; p r i n t f ( s i z e o f ( p)=%u\n, s i z e o f ( p ) ) ; p r i n t f ( s i z e o f ( q)=%u\n, s i z e o f ( q ) ) ; p r i n t f ( s i z e o f ( r)=%u\n, s i z e o f ( r ) ) ; f r e e ( r ) ; return 0 ; } Scientific Programming in C, fall 2012 Susi Lehtola Pointers 40/45

41 Dynamically allocated memory The output on x86 64 is $. / a. out s i z e o f ( p)=80 s i z e o f ( q)=8 s i z e o f ( r )=8 Even though q points to the static array, its size is still that of a pointer to double. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 41/45

42 Tables and pointers The [] operator is in principle not necessary for array access - pointer arithmetic can also be used for the same thing. #i n c l u de <s t d i o. h> #i n c l u de < s t d l i b. h> i n t main ( void ) { i n t i ; const i n t N=10; double p [N ] ; double q=p ; / H e l p e r / } f o r ( i =0; i <N; i ++) / D e r e f e r e n c e q, s e t the v a l u e to i and then i n c r e a s e q by one / ( q++)=i ; f o r ( i =0; i <N; i ++) p r i n t f ( %i %e \n, i, p [ i ] ) ; return 0 ; Scientific Programming in C, fall 2012 Susi Lehtola Pointers 42/45

43 Pointer arithmetic When a pointer is incremented (or decremented), it is moved forward (backward) by the size of its datatype. #i n c l u de <s t d i o. h> #i n c l u de < s t d l i b. h> i n t main ( void ) { / Get a random p o i n t e r / void p=m a l l o c ( s i z e o f ( double ) ) ; / Test p o i n t e r s / f l o a t f p=p ; double dp=p ; } f p++; dp++; p r i n t f ( p = %p\n, p ) ; p r i n t f ( f p = %p\n, f p ) ; p r i n t f ( dp = %p\n, dp ) ; f r e e ( p ) ; return 0 ; Scientific Programming in C, fall 2012 Susi Lehtola Pointers 43/45

44 Pointer arithmetic, cont d The result is, e.g. $. / a. out p = 0 x21d4010 f p = 0 x21d4014 dp = 0 x21d4018 Even though the pointers started from the same place, incrementation had different effects on them, because float is 4 bytes, whereas double is 8 bytes. Scientific Programming in C, fall 2012 Susi Lehtola Pointers 44/45

45 Pointers to structures When you have pointers to structures, you can access elements with the -> operator. #i n c l u d e <s t d i o. h> i n t main ( v o i d ) { s t r u c t c o o r d s { double x ; double y ; double z ; } ; s t r u c t c o o r d s r ; s t r u c t c o o r d s rp=&r ; } rp >x =1.0; rp >y =2.0; rp >z =3.0; p r i n t f ( r=(%f,% f,% f )\ n, r. x, r. y, r. z ) ; r e t u r n 0 ; The other option would be to write (*rp).x etc. The output is $. / a. out r =( , , ) Scientific Programming in C, fall 2012 Susi Lehtola Pointers 45/45

Scientific Programming in C IX. Debugging

Scientific Programming in C IX. Debugging Scientific Programming in C IX. Debugging Susi Lehtola 13 November 2012 Debugging Quite often you spend an hour to write a code, and then two hours debugging why it doesn t work properly. Scientific Programming

More information

Scientific Programming in C VI. Common errors

Scientific Programming in C VI. Common errors Scientific Programming in C VI. Common errors Susi Lehtola 6 November 2012 Beginner errors If you re a beginning C programmer, you might often make off-by one errors when you use arrays: #i n c l u de

More information

Key C Topics: Tutorial Pointers, Dynamic Memory allocation, Valgrind and Makefile CS370

Key C Topics: Tutorial Pointers, Dynamic Memory allocation, Valgrind and Makefile CS370 Key C Topics: Tutorial Pointers, Dynamic Memory allocation, Valgrind and Makefile CS370 Outline Pointers in C & and * operators Pointers with Arrays and Strings Dynamic memory allocation malloc() and free()

More information

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science Memory Allocation in C C Programming and Software Tools N.C. State Department of Computer Science The Easy Way Java (JVM) automatically allocates and reclaims memory for you, e.g... Removed object is implicitly

More information

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory Announcements CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory There will be no lecture on Tuesday, Feb. 16. Prof. Thompson s office hours are canceled for Monday, Feb. 15. Prof.

More information

Debugging (Part 2) 1

Debugging (Part 2) 1 Debugging (Part 2) 1 Programming in the Large Steps Design & Implement Program & programming style (done) Common data structures and algorithms Modularity Building techniques & tools (done) Test Testing

More information

Scientific Programming in C X. More features & Fortran interface

Scientific Programming in C X. More features & Fortran interface Scientific Programming in C X. More features & Fortran interface Susi Lehtola 20 November 2012 typedef typedefs are a way to make shorthand for data types, and possibly also make the code more general

More information

Princeton University Computer Science 217: Introduction to Programming Systems. Data Structures

Princeton University Computer Science 217: Introduction to Programming Systems. Data Structures Princeton University Computer Science 217: Introduction to Programming Systems Data Structures 1 Goals of this Lecture Help you learn (or refresh your memory) about: Common data structures: linked lists

More information

Dynamic Memory Allocation

Dynamic Memory Allocation Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility, there are four library routines known as memory management

More information

Use Dynamic Analysis Tools on Linux

Use Dynamic Analysis Tools on Linux Use Dynamic Analysis Tools on Linux FTF-SDS-F0407 Gene Fortanely Freescale Software Engineer Catalin Udma A P R. 2 0 1 4 Software Engineer, Digital Networking TM External Use Session Introduction This

More information

Lecture 8 Dynamic Memory Allocation

Lecture 8 Dynamic Memory Allocation Lecture 8 Dynamic Memory Allocation CS240 1 Memory Computer programs manipulate an abstraction of the computer s memory subsystem Memory: on the hardware side 3 @ http://computer.howstuffworks.com/computer-memory.htm/printable

More information

LOÏC CAPPANERA. 1. Memory management The variables involved in a C program can be stored either statically or dynamically.

LOÏC CAPPANERA. 1. Memory management The variables involved in a C program can be stored either statically or dynamically. C PROGRAMMING LANGUAGE. MEMORY MANAGEMENT. APPLICATION TO ARRAYS. CAAM 519, CHAPTER 7 This chapter aims to describe how a programmer manages the allocation of memory associated to the various variables

More information

CSE 333 Midterm Exam Sample Solution 7/28/14

CSE 333 Midterm Exam Sample Solution 7/28/14 Question 1. (20 points) C programming. For this question implement a C function contains that returns 1 (true) if a given C string appears as a substring of another C string starting at a given position.

More information

Memory (Stack and Heap)

Memory (Stack and Heap) Memory (Stack and Heap) Praktikum C-Programmierung Nathanael Hübbe, Eugen Betke, Michael Kuhn, Jakob Lüttgau, Jannek Squar Wissenschaftliches Rechnen Fachbereich Informatik Universität Hamburg 2018-12-03

More information

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Programming. Pointers, Multi-dimensional Arrays and Memory Management Programming Pointers, Multi-dimensional Arrays and Memory Management Summary } Computer Memory } Pointers } Declaration, assignment, arithmetic and operators } Casting and printing pointers } Relationship

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

The assignment requires solving a matrix access problem using only pointers to access the array elements, and introduces the use of struct data types.

The assignment requires solving a matrix access problem using only pointers to access the array elements, and introduces the use of struct data types. C Programming Simple Array Processing The assignment requires solving a matrix access problem using only pointers to access the array elements, and introduces the use of struct data types. Both parts center

More information

Both parts center on the concept of a "mesa", and make use of the following data type:

Both parts center on the concept of a mesa, and make use of the following data type: C Programming Simple Array Processing This assignment consists of two parts. The first part focuses on array read accesses and computational logic. The second part requires solving the same problem using

More information

Pointers and Memory Management

Pointers and Memory Management Pointers and Memory Management Timo Karvi 2013 Timo Karvi () Pointers and Memory Management 2013 1 / 58 Memory and C I In most modern computers, main memory is divided into bytes, with each byte capable

More information

CSE 374 Final Exam 3/15/17. Name UW ID#

CSE 374 Final Exam 3/15/17. Name UW ID# Name UW ID# There are 10 questions worth a total of 110 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,

More information

TI2725-C, C programming lab, course

TI2725-C, C programming lab, course Valgrind tutorial Valgrind is a tool which can find memory leaks in your programs, such as buffer overflows and bad memory management. This document will show per example how Valgrind responds to buggy

More information

Dynamic memory allocation (malloc)

Dynamic memory allocation (malloc) 1 Plan for today Quick review of previous lecture Array of pointers Command line arguments Dynamic memory allocation (malloc) Structures (Ch 6) Input and Output (Ch 7) 1 Pointers K&R Ch 5 Basics: Declaration

More information

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS Syllabus: Pointers and Preprocessors: Pointers and address, pointers and functions (call by reference) arguments, pointers and arrays, address arithmetic, character pointer and functions, pointers to pointer,initialization

More information

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017 CS 137 Part 5 Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors October 25th, 2017 Exam Wrapper Silently answer the following questions on paper (for yourself) Do you think that the problems on

More information

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21 Department of Computer Science College of Engineering Boise State University September 11, 2017 1/21 Pointers A pointer is a variable that stores the address of another variable. Pointers are similar to

More information

Praktische Aspekte der Informatik

Praktische Aspekte der Informatik Praktische Aspekte der Informatik Moritz Mühlhausen Prof. Marcus Magnor Optimization valgrind, gprof, and callgrind Further Reading Warning! The following slides are meant to give you a very superficial

More information

o Code, executable, and process o Main memory vs. virtual memory

o Code, executable, and process o Main memory vs. virtual memory Goals for Today s Lecture Memory Allocation Prof. David August COS 217 Behind the scenes of running a program o Code, executable, and process o Main memory vs. virtual memory Memory layout for UNIX processes,

More information

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Dynamic Memory Allocation (and Multi-Dimensional Arrays) Dynamic Memory Allocation (and Multi-Dimensional Arrays) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan

More information

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. CSE 2031 Fall November 11, 2013 Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example

More information

CS 11 C track: lecture 5

CS 11 C track: lecture 5 CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap Pointers (from last week) Address: location where data stored

More information

In Java we have the keyword null, which is the value of an uninitialized reference type

In Java we have the keyword null, which is the value of an uninitialized reference type + More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are

More information

The Valgrind Memory Checker. (i.e., Your best friend.)

The Valgrind Memory Checker. (i.e., Your best friend.) The Valgrind Memory Checker. (i.e., Your best friend.) Dept of Computing Science University of Alberta Small modifications by Stef Nychka, Mar. 2006 5th March 2006 Attribution. Introduction Some of the

More information

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS Pages 792 to 800 Anna Rakitianskaia, University of Pretoria INITIALISING POINTER VARIABLES Pointer variables are declared by putting

More information

Dynamic Allocation in C

Dynamic Allocation in C Dynamic Allocation in C C Pointers and Arrays 1 The previous examples involved only targets that were declared as local variables. For serious development, we must also be able to create variables dynamically,

More information

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014. Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example

More information

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size APS105 Malloc and 2D Arrays Textbook Chapters 6.4, 10.2 Datatype Size Datatypes have varying size: char: 1B int: 4B double: 8B int sizeof(): a builtin function that returns size of a type int x =

More information

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS C Programming C SYLLABUS COVERAGE Introduction to Programming Fundamentals in C Operators and Expressions Data types Input-Output Library Functions Control statements Function Storage class Pointer Pointer

More information

Dynamic Memory. R. Inkulu (Dynamic Memory) 1 / 19

Dynamic Memory. R. Inkulu  (Dynamic Memory) 1 / 19 Dynamic Memory R. Inkulu http://www.iitg.ac.in/rinkulu/ (Dynamic Memory) 1 / 19 Types of memory allocations auto local * allocated on stack and uninitialized by default * accessible in the function that

More information

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW IMPORTANT QUESTIONS IN C FOR THE INTERVIEW 1. What is a header file? Header file is a simple text file which contains prototypes of all in-built functions, predefined variables and symbolic constants.

More information

C Structures & Dynamic Memory Management

C Structures & Dynamic Memory Management C Structures & Dynamic Memory Management Goals of this Lecture Help you learn about: Structures and unions Dynamic memory management Note: Will be covered in precepts as well We look at them in more detail

More information

CMSC 341 Lecture 2 Dynamic Memory and Pointers

CMSC 341 Lecture 2 Dynamic Memory and Pointers CMSC 341 Lecture 2 Dynamic Memory and Pointers Park Sects. 01 & 02 Based on earlier course slides at UMBC Today s Topics Stack vs Heap Allocating and freeing memory new and delete Memory Leaks Valgrind

More information

Memory Management. CS449 Fall 2017

Memory Management. CS449 Fall 2017 Memory Management CS449 Fall 2017 Life9mes Life9me: 9me from which a par9cular memory loca9on is allocated un9l it is deallocated Three types of life9mes Automa9c (within a scope) Sta9c (dura9on of program)

More information

Dynamic Allocation in C

Dynamic Allocation in C Dynamic Allocation in C 1 The previous examples involved only targets that were declared as local variables. For serious development, we must also be able to create variables dynamically, as the program

More information

Class Information ANNOUCEMENTS

Class Information ANNOUCEMENTS Class Information ANNOUCEMENTS Third homework due TODAY at 11:59pm. Extension? First project has been posted, due Monday October 23, 11:59pm. Midterm exam: Friday, October 27, in class. Don t forget to

More information

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT C Review MaxMSP Developers Workshop Summer 2009 CNMAT C Syntax Program control (loops, branches): Function calls Math: +, -, *, /, ++, -- Variables, types, structures, assignment Pointers and memory (***

More information

Important Questions for Viva CPU

Important Questions for Viva CPU Important Questions for Viva CPU 1. List various components of a computer system. i. Input Unit ii. Output Unit iii. Central processing unit (Control Unit + Arithmetic and Logical Unit) iv. Storage Unit

More information

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018 CS 31: Intro to Systems Pointers and Memory Kevin Webb Swarthmore College October 2, 2018 Overview How to reference the location of a variable in memory Where variables are placed in memory How to make

More information

Kurt Schmidt. October 30, 2018

Kurt Schmidt. October 30, 2018 to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed

More information

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef today advanced data types (1) typedef. mon 23 sep 2002 homework #1 due today homework #2 out today quiz #1 next class 30-45 minutes long one page of notes topics: C advanced data types dynamic memory allocation

More information

CSCI-1200 Data Structures Fall 2015 Lecture 6 Pointers & Dynamic Memory

CSCI-1200 Data Structures Fall 2015 Lecture 6 Pointers & Dynamic Memory CSCI-1200 Data Structures Fall 2015 Lecture 6 Pointers & Dynamic Memory Announcements: Test 1 Information Test 1 will be held Monday, September 21st, 2015 from 6-:50pm. Students have been randomly assigned

More information

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor CS 261 Fall 2017 Mike Lam, Professor C Introduction Variables, Memory Model, Pointers, and Debugging The C Language Systems language originally developed for Unix Imperative, compiled language with static

More information

Heap Arrays and Linked Lists. Steven R. Bagley

Heap Arrays and Linked Lists. Steven R. Bagley Heap Arrays and Linked Lists Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index Variables and arrays have a type Create our

More information

CSE 374 Final Exam 3/15/17 Sample Solution. Question 1. (8 points) Suppose we have the following two statements in a C program:

CSE 374 Final Exam 3/15/17 Sample Solution. Question 1. (8 points) Suppose we have the following two statements in a C program: Question 1. (8 points) Suppose we have the following two statements in a C program: int *x = malloc(sizeof(int)); int *y = malloc(sizeof(int)); For each of the following expressions, write true if the

More information

Projet 0 - Correc,on. Structures de données et algorithmes

Projet 0 - Correc,on. Structures de données et algorithmes Projet 0 - Correc,on Structures de données et algorithmes Code de base #include #define uint unsigned int #define N 101 int f(int m, int M, uint* s) *s ^= (uint)(*s

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36 Department of Computer Science College of Engineering Boise State University August 25, 2017 1/36 Pointers and Arrays A pointer is a variable that stores the address of another variable. Pointers are similar

More information

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space Dynamic Memory Allocation All variables, arrays, structures and unions that we worked with so far are statically allocated,

More information

Review of the C Programming Language for Principles of Operating Systems

Review of the C Programming Language for Principles of Operating Systems Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights

More information

Computer Programming Unit 3

Computer Programming Unit 3 POINTERS INTRODUCTION Pointers are important in c-language. Some tasks are performed more easily with pointers such as dynamic memory allocation, cannot be performed without using pointers. So it s very

More information

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites: C Programming Code: MBD101 Duration: 10 Hours Prerequisites: You are a computer science Professional/ graduate student You can execute Linux/UNIX commands You know how to use a text-editing tool You should

More information

Homework #3 CS2255 Fall 2012

Homework #3 CS2255 Fall 2012 Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation

More information

DEBUGGING: DYNAMIC PROGRAM ANALYSIS

DEBUGGING: DYNAMIC PROGRAM ANALYSIS DEBUGGING: DYNAMIC PROGRAM ANALYSIS WS 2017/2018 Martina Seidl Institute for Formal Models and Verification System Invariants properties of a program must hold over the entire run: integrity of data no

More information

ESC101N: Fundamentals of Computing End-sem st semester

ESC101N: Fundamentals of Computing End-sem st semester ESC101N: Fundamentals of Computing End-sem 2010-11 1st semester Instructor: Arnab Bhattacharya 8:00-11:00am, 15th November, 2010 Instructions 1. Please write your name, roll number and section below. 2.

More information

CSE 160 Discussion Section. Winter 2017 Week 3

CSE 160 Discussion Section. Winter 2017 Week 3 CSE 160 Discussion Section Winter 2017 Week 3 Homework 1 - Recap & a few points ComputeMandelbrotPoint func() in smdb.cpp does the job serially. You will have to do the same task in parallel manner in

More information

Heap Arrays. Steven R. Bagley

Heap Arrays. Steven R. Bagley Heap Arrays Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index a[42] = 35; Variables and arrays have a type int, char, double,

More information

Lecture 14 Notes. Brent Edmunds

Lecture 14 Notes. Brent Edmunds Lecture 14 Notes Brent Edmunds October 5, 2012 Table of Contents 1 Sins of Coding 3 1.1 Accessing Undeclared Variables and Pointers...................... 3 1.2 Playing With What Isn t Yours..............................

More information

A Capacity: 10 Usage: 4 Data:

A Capacity: 10 Usage: 4 Data: Creating a Data Type in C Integer Set For this assignment, you will use the struct mechanism in C to implement a data type that represents sets of integers. A set can be modeled using the C struct: struct

More information

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016 CS 31: Intro to Systems Pointers and Memory Martin Gagne Swarthmore College February 16, 2016 So we declared a pointer How do we make it point to something? 1. Assign it the address of an existing variable

More information

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009 Lecture 5: Multidimensional Arrays CS209 : Algorithms and Scientific Computing Wednesday, 11 February 2009 CS209 Lecture 5: Multidimensional Arrays 1/20 In today lecture... 1 Let s recall... 2 Multidimensional

More information

Hacking in C. Memory layout. Radboud University, Nijmegen, The Netherlands. Spring 2018

Hacking in C. Memory layout. Radboud University, Nijmegen, The Netherlands. Spring 2018 Hacking in C Memory layout Radboud University, Nijmegen, The Netherlands Spring 2018 A short recap The & operator gives us the address of data Inverse of & is the * operator (dereferencing) 2 A short recap

More information

My malloc: mylloc and mhysa. Johan Montelius HT2016

My malloc: mylloc and mhysa. Johan Montelius HT2016 1 Introduction My malloc: mylloc and mhysa Johan Montelius HT2016 So this is an experiment where we will implement our own malloc. We will not implement the world s fastest allocator, but it will work

More information

ECE 551D Spring 2018 Midterm Exam

ECE 551D Spring 2018 Midterm Exam Name: ECE 551D Spring 2018 Midterm Exam NetID: There are 6 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual

More information

valgrind overview: runtime memory checker and a bit more What can we do with it?

valgrind overview: runtime memory checker and a bit more What can we do with it? Valgrind overview: Runtime memory checker and a bit more... What can we do with it? MLUG Mar 30, 2013 The problem When do we start thinking of weird bug in a program? The problem When do we start thinking

More information

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface Secure Coding CYSE 411/AIT681 Secure Software Engineering Topic #9. Secure Coding: Dynamic Memory Instructor: Dr. Kun Sun String management Pointer Subterfuge Dynamic memory management Integer security

More information

PDS Class Test 2. Room Sections No of students

PDS Class Test 2. Room Sections No of students PDS Class Test 2 Date: October 27, 2016 Time: 7pm to 8pm Marks: 20 (Weightage 50%) Room Sections No of students V1 Section 8 (All) Section 9 (AE,AG,BT,CE, CH,CS,CY,EC,EE,EX) V2 Section 9 (Rest, if not

More information

Arrays, Pointers and Memory Management

Arrays, Pointers and Memory Management Arrays, Pointers and Memory Management EECS 2031 Summer 2014 Przemyslaw Pawluk May 20, 2014 Answer to the question from last week strct->field Returns the value of field in the structure pointed to by

More information

C Introduction. Comparison w/ Java, Memory Model, and Pointers

C Introduction. Comparison w/ Java, Memory Model, and Pointers CS 261 Fall 2018 Mike Lam, Professor C Introduction Comparison w/ Java, Memory Model, and Pointers Please go to socrative.com on your phone or laptop, choose student login and join room LAMJMU The C Language

More information

DAY 3. CS3600, Northeastern University. Alan Mislove

DAY 3. CS3600, Northeastern University. Alan Mislove C BOOTCAMP DAY 3 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh and Pascal Meunier s course at Purdue Memory management 2 Memory management Two

More information

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-43 Exam Review February, 01 Presented by the RIT Computer Science Community http://csc.cs.rit.edu C Preprocessor 1. Consider the following program: 1 # include 3 # ifdef WINDOWS 4 # include

More information

CS201 Latest Solved MCQs

CS201 Latest Solved MCQs Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th Introduction to Computer Systems 15 213/18 243, fall 2009 16 th Lecture, Oct. 22 th Instructors: Gregory Kesden and Markus Püschel Today Dynamic memory allocation Process Memory Image %esp kernel virtual

More information

MODULE 5: Pointers, Preprocessor Directives and Data Structures

MODULE 5: Pointers, Preprocessor Directives and Data Structures MODULE 5: Pointers, Preprocessor Directives and Data Structures 1. What is pointer? Explain with an example program. Solution: Pointer is a variable which contains the address of another variable. Two

More information

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming Jagannath Institute of Management Sciences Lajpat Nagar BCA II Sem C Programming UNIT I Pointers: Introduction to Pointers, Pointer Notation,Decalaration and Initialization, Accessing variable through

More information

ME964 High Performance Computing for Engineering Applications

ME964 High Performance Computing for Engineering Applications ME964 High Performance Computing for Engineering Applications Quick Overview of C Programming January 26, 2012 Dan Negrut, 2012 ME964 UW-Madison There is no reason for any individual to have a computer

More information

Computer Systems and Networks

Computer Systems and Networks LECTURE 7: PERFORMANCE MEASUREMENT Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu) University of the Pacific Lab Schedule Today Lab 5 Performance Measurement is open Work

More information

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5 C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5 1. Pointers As Kernighan and Ritchie state, a pointer is a variable that contains the address of a variable. They have been

More information

Pointers. Héctor Menéndez 1. November 28, AIDA Research Group Computer Science Department Universidad Autónoma de Madrid.

Pointers. Héctor Menéndez 1. November 28, AIDA Research Group Computer Science Department Universidad Autónoma de Madrid. Pointers Héctor Menéndez 1 AIDA Research Group Computer Science Department Universidad Autónoma de Madrid November 28, 2013 1 based on the original slides of the subject Index 1 Dynamic Memory 2 Arrays

More information

C Tutorial. Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha

C Tutorial. Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha C Tutorial Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha CS 370 - Operating Systems - Spring 2019 1 Outline What is a pointer? & and * operators

More information

Dynamic Data Structures. CSCI 112: Programming in C

Dynamic Data Structures. CSCI 112: Programming in C Dynamic Data Structures CSCI 112: Programming in C 1 It s all about flexibility In the programs we ve made so far, the compiler knows at compile time exactly how much memory to allocate for each variable

More information

Review of the C Programming Language

Review of the C Programming Language Review of the C Programming Language Prof. James L. Frankel Harvard University Version of 11:55 AM 22-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reference Manual for the

More information

Pointers. Reference operator (&) ted = &andy;

Pointers. Reference operator (&)  ted = &andy; Pointers We have already seen how variables are seen as memory cells that can be accessed using their identifiers. This way we did not have to care about the physical location of our data within memory,

More information

Valgrind. Philip Blakely. Laboratory for Scientific Computing, University of Cambridge. Philip Blakely (LSC) Valgrind 1 / 21

Valgrind. Philip Blakely. Laboratory for Scientific Computing, University of Cambridge. Philip Blakely (LSC) Valgrind 1 / 21 Valgrind Philip Blakely Laboratory for Scientific Computing, University of Cambridge Philip Blakely (LSC) Valgrind 1 / 21 Part I Valgrind Philip Blakely (LSC) Valgrind 2 / 21 Valgrind http://valgrind.org/

More information

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

CMPS 105 Systems Programming. Prof. Darrell Long E2.371 + CMPS 105 Systems Programming Prof. Darrell Long E2.371 darrell@ucsc.edu + Chapter 7: The Environment of a UNIX process + Introduction + The main() fuction n int main(int argc, char* argv[]); n argc =

More information

Part V. Memory and pointers. Philip Blakely (LSC) C++ Introduction 145 / 370

Part V. Memory and pointers. Philip Blakely (LSC) C++ Introduction 145 / 370 Part V and pointers Philip Blakely (LSC) C++ Introduction 145 / 370 Outline 19 20 Function pointers 21 Global variables Philip Blakely (LSC) C++ Introduction 146 / 370 Heap and Stack The stack is a Last-In-First-Out

More information

The Valgrind Memory Checker. (i.e., Your best friend.)

The Valgrind Memory Checker. (i.e., Your best friend.) The Valgrind Memory Checker. (i.e., Your best friend.) Dept of Computing Science University of Alberta modifications by Stef Nychka October 24, 2007 Attribution. Introduction Some of the material in this

More information

Dynamic memory allocation

Dynamic memory allocation Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes

More information

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) !

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) ! Memory Questions? CSCI [4 6]730 Operating Systems Main Memory! What is main memory?! How does multiple processes share memory space?» Key is how do they refer to memory addresses?! What is static and dynamic

More information

M1-R4: Programing and Problem Solving using C (JULY 2018)

M1-R4: Programing and Problem Solving using C (JULY 2018) M1-R4: Programing and Problem Solving using C (JULY 2018) Max Marks: 100 M1-R4-07-18 DURATION: 03 Hrs 1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter

More information