Programming Tips GS540 January 10, 2011.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Advanced Programming 15 Feb The “OI” Programming Process Reading the problem statement Thinking Coding + Compiling Testing + Debugging Finalizing.
Programming and Data Structure
ITEC 320 Lecture 11 Pointers(1). Pointers Review Packages –Generic –Child Homework 3 is posted.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Bitwise Operators Pointers Functions Structs Interrupts (in C)
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
C Programming for Embedded Systems. fig_06_00 Computer Layers Low-level hardware to high-level software (4GL: “domain-specific”, report-driven, e.g.)
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Discussion Section: HW1 and Programming Tips GS540.
General Issues in Using Variables
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
Scope.
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Stack and Heap Memory Stack resident variables include:
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Dynamic memory allocation and Pointers Lecture 4.
Introduction to Programming
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CS 261 – Data Structures Introduction to C Programming.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Carnegie Mellon 1 Malloc Recitation Ben Spinelli Recitation 11: November 9, 2015.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Lecture 3 Translation.
Object Lifetime and Pointers
Dynamic Storage Allocation
EGR 2261 Unit 11 Pointers and Dynamic Variables
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Programming Tips GS540 January 10, 2011.
Checking Memory Management
Programmazione I a.a. 2017/2018.
Pointers and References
Introduction to Linked Lists
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Pointers and References
First discussion section agenda
Closure Representations in Higher-Order Programming Languages
Variables Title slide variables.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Homework Any Questions?.
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Malloc Lab CSCI 380: Operating Systems
COP 3330 Object-oriented Programming in C++
Data Structures and Algorithms Introduction to Pointers
Outline Announcements Differences between FORTRAN and C
Chapter 9: Pointers and String
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Pointers and References
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
Testing & Security Dr. X.
CSE 206 Course Review.
Procedure Linkages Standard procedure linkage Procedure has
SPL – PS2 C++ Memory Handling.
Introduction to C CS 3410.
Presentation transcript:

Programming Tips GS540 January 10, 2011

Outline General tips / Coding style advice Performance Debugging advice Numerical issues C Programming Pointers Sorting Homework 1

General tips Validate using toy cases with a small amount of data Figure out minimal cases by hand to verify program Print intermediate output Test important functions

Coding style advice Your audience is people as well as the computer Break large functions into small, simple functions Break large files into smaller files containing groups of related functions Use descriptive names for function arguments and variables with larger scopes (e.g. out_file, exons) Use short names for iterators, vars of limited scope, and vars that are used many times (e.g. i, j)

Performance Avoid unnecessary optimization! Better to write simple code first and improve speed if necessary Big performance gains often result from changing a few small sections of code (e.g. within loops) Move unnecessary code out of loops Avoid frequent memory allocation Allocate memory in large blocks (e.g. array of structs, not one at a time) Re-use the same piece of memory Avoid slow comparison routines when sorting Use a profiler for tough cases (gprof for C; dprofpp for perl)

Debugging advice Use assertions E.g. check probabilities: should always be >= 0 and <= 1 often should sum to 1.0 Write slow but sure code to check optimized code In difficult cases use a debugger, but avoid overuse valgrind can help find segfaults, memleaks (compile with -g first)

Numerical issues Consider using log space to avoid overflow and underflow Don’t compare floats with equals (use >= or <=, NOT ==) Beware subtracting large, close numbers Beware integer casts 1/2 is 0, but 1.0/2 is 0.5 To generate random numbers, use random() rather than rand()

Pointers in C Pointers are memory addresses (they point to other variables) The address-of operator (&) obtains the memory address of a variable The dereference operator (*) accesses the value stored at the pointed-to mem location

Pointers in C (cont’d) Arrays are pointers to blocks of memory Array indices are just pointer arithmetic and dereferencing combined: a[12] is the same as *(a+12) &a[3] is the same as a+3 From The C Programming Language by B. Kernighan & D. Ritchie

Pointers in C (cont’d) Attributes of pointed-to structures can be derefenced with “arrow notation”: a->elem is equivalent to (*a).elem

Sorting (in C)

Homework 1 Declare large arrays on heap not stack Outside main() or as static Output XML markup directly Avoid copy-pasting results into an XML template

Words of wisdom "Everything should be made as simple as possible, but no simpler." -- Albert Einstein KISS principle: “Keep It Simple, Stupid” From The Zen of Python by Tim Peters: Beautiful is better than ugly Explicit is better than implicit Simple is better than complex Complex is better than complicated Flat is better than nested Sparse is better than dense Readability counts