Understanding Pointers

Similar documents
SYSC 2006 C Winter 2012

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

COSC Software Engineering. Lectures 14 and 15: The Heap and Dynamic Memory Allocation

Arrays and Memory Management

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

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

Dynamic memory allocation

Pointers and Terminal Control

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

Intermediate Programming, Spring 2017*

Dynamic Memory Allocation and Command-line Arguments

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Kurt Schmidt. October 30, 2018

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

Pointers, Dynamic Data, and Reference Types

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Dynamic Memory Allocation

Pointers, Arrays, Memory: AKA the cause of those Segfaults

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

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Memory (Stack and Heap)

CSC 1600 Memory Layout for Unix Processes"

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

Dynamic Allocation in C

CS 61C: Great Ideas in Computer Architecture Introduction to C, Part III

Processes. Johan Montelius KTH

Lab 3. Pointers Programming Lab (Using C) XU Silei

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Lecture 8 Dynamic Memory Allocation

Operating systems. Lecture 9

Arrays and Pointers (part 1)

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

A process. the stack

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Today s Learning Objectives

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

Lecture 04 Introduction to pointers

Arrays and Pointers. CSE 2031 Fall November 11, 2013

First of all, it is a variable, just like other variables you studied

Dynamic memory. EECS 211 Winter 2019

Arrays and Pointers in C. Alan L. Cox

Heap Arrays. Steven R. Bagley

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Arrays in C. Data and File Structures Laboratory. DFS Lab (ISI) Arrays in C 1 / 1

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Pointers and File Handling

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

Memory Management. CSC215 Lecture

Advanced Pointer Topics

Low-Level C Programming. Memory map Pointers Arrays Structures

Arrays, Pointers and Memory Management

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

Common Misunderstandings from Exam 1 Material

Fundamental of Programming (C)

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

CS 110 Computer Architecture. Lecture 4: Introduction to C, Part III. Instructor: Sören Schwertfeger.

[0569] p 0318 garbage

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

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

Systems Programming and Computer Architecture ( )

ch = argv[i][++j]; /* why does ++j but j++ does not? */

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Dynamic memory allocation (malloc)

Lecture 03 Bits, Bytes and Data Types

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

CS C Primer. Tyler Szepesi. January 16, 2013


Arrays and Pointers (part 1)

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Dynamic Allocation in C

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

POINTER AND ARRAY SUNU WIBIRAMA

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Dynamic Data Structures. CSCI 112: Programming in C

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

CS 261 Data Structures. Introduction to C Programming

CS 11 C track: lecture 5

EL2310 Scientific Programming

Memory Management I. two kinds of memory: stack and heap

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

211: Computer Architecture Summer 2016

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Memory Allocation in C

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

The C Programming Language

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

CS61C : Machine Structures

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Lecture 2: C Programm

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CSCI 171 Chapter Outlines

Transcription:

Division of Mathematics and Computer Science Maryville College

Pointers and Addresses Memory is organized into a big array. Every data item occupies one or more cells. A pointer stores an address. A pointer is itself a variable.

Pointer Syntax Declaration type *name int *ptr; Declares a pointer to a variable of given type. Address Of &variable &x Returns the address of the variable. Dereference *name *ptr Gets to the value at the address stored in the pointer. Size Information sizeof Gets the size, in bytes, of its argument. sizeof(type) sizeof expression

Pointers and Function Arguments Pointers are the only mechanism to share variables with a function. In C, all function calls are pass-by-value. The value of a pointer is a memory location, and so they are copied but provide a common reference.

scanf(fmt,...) char c; int x; double d; scanf("%c%d%x", &c, &x, &d); This is the formatted input method in stdio.h Uses the same formatters as printf You do not use normal strings, just simple format place holders. Parameters are passed in as pointers.

Pointers and Arrays Array Names Synonymous with the first address in the array. Is not a variable! Suppose we have an integer pointer pa and an int array ar[10] We can assign the pointer: pa = &ar[0] Alternatively: pa = ar The following are equivalent operations: ar[i] pa[i] *(ar + i) *(pa + i)

Character Pointers and Functions String literals have type const char* Strings are terminated by null characters. "This is a string" is really: "This is a string\0" When passing strings into functions, use type char* The null terminator is there so that functions can find the end of the string. Arrays of strings are arrays of pointers! char *[] or char**.

Address Arithmetic Every pointer has an associated type. Adding or subtracting from a pointer moves the address by the specified amount. int *pa; pa++; /* The next int */ pa ; /* The previous int */ pa+=2; /* Go forward 2 ints */ You can also add and subtract pointers: n = qa-pa; /* number of ints */

Alloc Example /* size of available space */ #define ALLOCSIZE 10000 /* storage for alloc */ static unsigned char allocbuf[allocsize]; /* next free position */ static void *allocp = allocbuf;

Alloc Example - alloc(int n) /* return pointer to n characters */ void *alloc(int n) { if((void*)allocbuf + ALLOCSIZE - allocp >= n) { /* it fits */ allocp += n; return allocp - n; /* old p */ } else { /* not enough room */ return 0; } }

Alloc Example - afree(void *p) /* free storage pointed to by p */ void afree(void *p) { if(p >= (void*)allocbuf && p < (void*)allocbuf + ALLOCSIZE) { allocp=p; } }

Command-Line Arguments C provides a mechanism to send parameters into main: int main(int argc, char **argv) argc is the number of arguments argv is an array of character pointers. The array itself is terminated by NULL. argv[0] is the name of the program as typed at the shell.

Pointers to Functions Functions are not variables, but they do reside in memory. A function pointer contains the address of the beginning of a function. Suppose we had a comparison function which takes two pointers: int compare(int *x, int *y); We could build a pointer to this function: int (*fp)(int*, int*); fp = compare; We can call the function from this pointer (*fp)(&x, &y); Could we use this function pointer to reference compare? Why or why not? int(*gp)(void*, void*);

malloc and free The standard library (in #include <stdlib.h>) has an allocation function. void *malloc(size_t n); Allocates n bytes and returns a pointer to the first byte. Example: Create an array of 10 integers. int *pa = malloc(10 * sizeof(int)); void free(void *p); Deallocates a pointer allocated by malloc. Example: Destroy our array. free(pa); Typically, malloc and free are implemented as a heap.

The Unix Memory Model A process begins with a read-only program text segment. Following the program text segment is the writable data segment. The program break is the address of the end of the data segment. The program break can be advanced upwards using the brk syscall. (sbrk is a more convenient wrapper.) malloc s heap lives in the data segment. The stack is at the highest address and grows downwards.