Terminos españoles de ingeneria de computacion.

Slides:



Advertisements
Similar presentations
Kernighan/Ritchie: Kelley/Pohl:
Advertisements

1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Outline Midterm results Static variables Memory model
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
C++ Memory Overview 4 major memory segments Key differences from Java
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 Debugging (Part 2). “Programming in the Large” Steps Design & Implement Program & programming style (done) Common data structures and algorithms Modularity.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
1 C Basics Monday, August 30, 2010 CS 241. Announcements MP1, a short machine problem, will be released today. Due: Tuesday, Sept. 7 th at 11:59pm via.
CSE 333 – SECTION 1 C, Introduction to and Working with.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
CSE 333 – SECTION 2 Memory Management. Questions, Comments, Concerns Do you have any? Exercises going ok? Lectures make sense? Homework 1 – START EARLY!
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Lecture 3: Getting Started & Input / Output (I/O)
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CSE 220 – C Programming malloc, calloc, realloc.
Dynamic Allocation in C
Dynamic Storage Allocation
Memory allocation & parameter passing
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
CS1010 Programming Methodology
Stack and Heap Memory Stack resident variables include:
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Day 03 Introduction to C.
CSE 374 Programming Concepts & Tools
Recitation 6: C Review 30 Sept 2016.
Motivation and Overview
Valgrind Overview What is Valgrind?
2016.
CSE 303 Concepts and Tools for Software Development
Pointers and Memory Overview
Day 03 Introduction to C.
Checking Memory Management
SPAN 301 – SecCiÓn 1 Introducción
Arrays in C.
Dynamic Memory Allocation
Lecture 6 C++ Programming
Advanced Programming Behnam Hatami Fall 2017.
CS157: Dynamic Memory Dynamic Memory 11/9/201804/25/06.
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
CSE 333 – SECTION 2 Memory Management
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Memory Allocation CS 217.
Programming in C Pointer Basics.
EECE.2160 ECE Application Programming
CSCI 380: Operating Systems Lecture #11 Instructor: William Killian
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Programming in C Pointer Basics.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Pointers and Arrays Beyond Chapter 16
CSCI 380: Operating Systems Instructor: William Killian
C (and C++) Pointers April 4, 2019.
Manual Memory Management: Mastering malloc()
Homework Continue with K&R Chapter 5 Skipping sections for now
Valgrind Overview What is Valgrind?
Dynamic Memory – A Review
Caches and Blocking 26th February 2018.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Terminos españoles de ingeneria de computacion. SPAN 301 – SecCiÓn 2 Terminos españoles de ingeneria de computacion. Hello class welcome to your first quiz section for 333...

Traer galletas para los estudiantes Enseñar los estudiantes español Nuestros Objetivos Traer galletas para los estudiantes Enseñar los estudiantes español Confundir los estudiantes profundante y hacerles pensar que estamos en una clase de español Y realmente ninguno de estos objetivos se van a lograr  ADDITIONAL: Just a reminder there is an exercise due tomorrow and HW0 is due Tuesday. OVERVIEW: - C workflow review -

Pointers, Arrays, and Memory Management CSE 333 – SECTION 2 Pointers, Arrays, and Memory Management

Questions, Comments, Concerns Do you have any? Exercises going ok? Lectures make sense? Looked at the homework?

Pointers - Review A data type that stores an address Used to indirectly refer to values Can add/subtract to the address It’s just another number Value Address p a

Arrays and pointers a[0] <==> *a a[3] <==> *(a + 3) How about a, a+3, *a+3 or *a++?

Example [basic_pointer.c] #include <stdio.h> void f(int *j) { (*j)++; } int main() { int i = 20; int *p = &i; f(p); printf("i = %d\n", i); return 0;

Pointers to pointers char *c = “hello”; char **cp = &c; char ***cpp = &cp; Why could this be useful?

Function pointers We can have pointers to functions as well Syntax is a little awkward Example: int (*ptr_to_int_fn)(int, int) Makes sense if you think about it hard We will be using these in the homework assignments! Demo: [function_pointer.c]

Using the Heap Why is this necessary? Lifetime on the stack Lifetime on the heap

Memory Management C gives you the power to manage your own memory C does very little for you Benefits? Disadvantages? When would you want this vs. automatic memory management?

Memory Management Done Right Need to let the system know when we are done with a chunk of memory In general, every malloc() must (eventually) be matched by a free() Example: [arraycopy.c]

Memory Management Details When are we done with a piece of data? Depends on where we got it from, how we are using it, etc. Some functions expect allocated space, others allocate for you sprintf() vs asprintf() Some APIs expect you to free structures, others free for you Compare / contrast?

Memory Management Gone Horribly Wrong Many (many!) ways to mess up Dangling pointers Double frees Incorrect frees Never frees That’s just a few Small example: [badlylinkedlist.c]

Valgrind Is Your Friend Use of uninitialized memory Use of memory you shouldn’t be using Memory leaks Definitely Lost Indirectly Lost Possibly Lost Still Reachable* Simply run: valgrind <program> Small example: [imsobuggy.c] *This is generally ok

Exercise 1 Assume that a character takes 1 byte. Output of following program? #include <stdio.h> int main() { char str1[20] = "GeeksQuiz"; char *str2 = "GeeksQuiz"; char str3[] = "GeeksQuiz"; char str4[] = {'G', 'e', 'e', 'k', 's', 'Q', 'u', 'i', 'z'}; printf("%zu\n", sizeof(str1)); printf("%zu\n", sizeof(str2)); printf("%zu\n", sizeof(str3)); printf("%zu\n", sizeof(str4)); return 0; } Answer: 20 4 or 8 depending on whether you’re on a 32 or 64 bit machine 10 9

Exercise 2 – What happens here? #include <stdio.h> #include <stdlib.h> int *a; void add1(int * arr); // Adds 1 to each element of array a int main() { int *a = malloc(4 * sizeof(int)); for (int i = 0; i < 4; i++) { a[i] = i; printf("a[%d] = %d\n", i, i); } add1(a); free(a); return 0; void add1(int *arr) { a[i] = a[i + 1]; free(arr); This code WILL compile. However it causes a segmentation fault and valgrind produces some helpful errors. Have the class help you get rid of those valgrind errors, then run it again (Hopefully it’ll seg fault again) and rerun valgrind and have the class help you fix the messages it complains about again. Point is, this is mostly a debugging exercise using valgrind although many of the kids will be able to notice errors within a minute if you give them time.

Homework Tips Start Them Early Seg Faults? You have friends to help Spend 1 week implementing, the following debugging Seg Faults? You have friends to help Valgrind, GDB, Discussion Board, TA Office Hours What are we looking for? Correct, well documented code that passes our unit tests (hint: test_suite), has no memory leaks (hint: valgrind), and is stylistically pleasing (hint: clint) Watch out for Segmentation Faults, Valgrind Errors/Leaks, Compiler Errors/Warnings Dangling pointers, double frees, null pointers Violation of the DRY principle (Don’t repeat yourself, refactor!)