A Capacity: 10 Usage: 4 Data:

Size: px
Start display at page:

Download "A Capacity: 10 Usage: 4 Data:"

Transcription

1 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 _CSet { uint32_t Capacity; // dimension of the set's array uint32_t Usage; // number of elements in the set int32_t Data; // pointer to the set's array }; typedef struct _CSet CSet; So, a CSet object A that represents the set 17, 9, 23, 44 could be initialized like this: A Capacity: We will say that a CSet object A is proper if and only if it satisfies each of the following conditions: 1. If A.Capacity == 0 then A.Usage == 0 and A.Data == NULL. 2. If A.Capacity > 0 then A.Data points to an array of dimension A.Capacity. 3. A.Data[0 : A.Usage-1] are the values stored in the set (in unspecified order). 4. A.Data[A.Usage : A.Capacity-1] equal INT32_MIN. A data type consists of a collection of values and a set of operations that may be performed on those values. For a set type, it would make sense to provide the common set operations; for example: / Sets punion to be the union of the sets pa and pb. Pre: punion, pa and pb are proper Post: pa and pb are unchanged For every integer x, x is contained in punion iff x is contained in pa or in pb (or both). punion->capacity == pa->capacity + pb->capacity punion->usage == pa->usage + pb->usage - number of elements that occur in both pa and pb punion is proper Returns: true if the union is successfully created; false otherwise / bool CSet_Union(CSet const punion, const CSet const pa, const CSet const pb); Version 2.00 This is a purely individual assignment! 1

2 Some Notes on Function Interface Design In this case, the use of a bool return type allows the function to indicate whether the operation was completed successfully. The functions we will implement are designed to pass pointers to CSet objects, rather than to pass CSet objects by copy. While that would not duplicate the array (see the deep copy discussion later in this specification), passing a CSet object would still entail copying the actual members of the object, and that's wasteful of both time and space. The interface allows the union to be a different CSet object than either of the CSet objects being combined. That has semantic integrity, since in mathematics writing A B does not imply that either of the two sets will be changed. That is, is the name of a different set than A or B (although the union could equal one of them). A B On the other hand, the preconditions do not require that punion point to a different CSet object than pa or pb (in fact, all three could point to the same object). That allows the user the flexibility to use the function to combine computing the union with overwriting one of the source sets with the union, analogous to writing "let " in mathematics. It should be noted that this flexibility also creates some potential problems in implementing the computation of the union. A A B The interface also illustrates the complexities of using the const qualifier in C. The specification of the parameter const CSet const pa implies that the value of the parameter pa cannot be modified within the function, nor can the value of the pointer's target be changed via a use of that pointer. Any violations of those restrictions are to be flagged as errors at compile time. On the other hand, if the target of the pointer is modified via some other means, the compiler will not be expected to detect that possibility, much less to flag it as an error, or even with a warning. So, if the user calls CSet_Union() and the first and second parameters are aliases (that is, pointers to the same CSet object), there is no violation of the const specifier. This doesn't indicate there's something "wrong" with the way const works in C, just that there are subtleties which need to be understood. Note also that it's possible to avoid the issue by changing the preconditions (and enforcing them). If the preconditions for CSet_Union() specified that punion could not alias either pa or pb, then the implementation could simply check those conditions and return false if either were violated. The function does provide an excuse to make good use of the const qualifier, applied to the pointer and/or its target, as appropriate. In the case of the function above, there is no logical reason the function should modify either of the CSet objects whose union is being found, unless the caller deliberately creates an alias, so the pointers to those are both are qualified as const CSet const. But the function does need to modify the CSet object that will hold the union, so the pointer to that object is qualified as CSet const. Note that all three pointers are const -qualified so that the function cannot make any of them point to a different location in memory. Obviously there's no reason for the function to give any of those pointers a new target; qualifying them as const pointers ensures that we won't make any mistakes of that sort within the definition of the function, unless we were to do something disreputable... The first post-condition might seem to be superfluous; since the use of const would seem to prevent any modifications to pa or pb. However, C allows many typecasts that are potentially unwise, including casting away const-ness. The stated post-condition assures the caller that the implementation of the function does not violate the intent indicated by the way const was applied to the parameters. Version 2.00 This is a purely individual assignment! 2

3 Other Operations The most basic operation for a complex data type is the initialization of a new object of that type. Even though C does not provide for automatic initialization via a "constructor", it is still useful to supply a function that will allow the client to control the initialization of new objects explicitly. For the CSet type we have the following function, which allows the user to specify the initial capacity of the set, and provides a guaranteed filler value in each position in the set's array: / Initializes a raw pset object, with capacity Sz. Pre: pset points to a CSet object, but pset may be proper or raw Sz has been initialized Post: If successful: pset->capacity == Sz pset->usage == 0 pset->data points to an array of dimension Sz, or is NULL if Sz == 0 if Sz!= 0, pset->data[0 : Sz-1] == INT32_MIN else: pset->capacity == 0 pset->usage == 0 pset->data == NULL Returns: true if successful, false otherwise / bool CSet_Init(CSet const pset, uint32_t Sz); The CSet type raises a deep-copy issue, since the data array is allocated dynamically. Since a user might very well need to make a copy of a CSet object, we will provide a deep-copy function to support that. The cost of making a deep copy of a CSet object may also be significant, due to the potential size of that array, so we should take care to optimize the implementation: / Makes a deep copy of a CSet object. Pre: ptarget and psource are proper Post: psource is unchanged If successful: ptarget->capacity == psource->capacity ptarget->usage == psource->usage ptarget[0:ptarget->capacity-1] == psource[0:psource->capacity-1] ptarget->data!= psource->data, unless ptarget == psource ptarget is proper. else: ptarget is unchanged Returns: true if successful, false otherwise / bool CSet_Copy(CSet const ptarget, const CSet const psource); In this case, there is no requirement that ptarget and psource are different. The rationale is that it's easy to handle that case (do nothing), and while it's strange to specify copying an object to itself, a user may choose to do so, or even do so accidentally. See the Appendix: Deep Copy for a full discussion of the issues this function must solve. Version 2.00 This is a purely individual assignment! 3

4 Here's another support function we'll include: / Adds Value to a pset object. Pre: pset is proper Value has been initialized Post: If successful: Value is a member of pset pset->usage has been incremented pset->capacity has been increased, if necessary pset is proper else: pset is unchanged Returns: true if successful, false otherwise / bool CSet_Insert(CSet const pset, int32_t Value); One of the post-conditions deserves some comments. If the CSet object pset is full, its Capacity is supposed to be increased. That requires allocating a new array dynamically, which could fail; that's why the return type is bool. Once the new array is created, the values must be copied into it from the old array, the new element must be added, and the old array must be deallocated in order to avoid a memory leak. This may seem to be a good place to consider using the C Standard Library function realloc(). However, you must be careful to ensure that the resulting CSet object is proper. How should you determine the size of the new array? On the one hand, it is good to avoid wasting memory. On the other hand, the resizing operation described above is fairly expensive, and it is desirable to avoid doing it often. If we double the size of the array, we can show that the average cost of an insertion is O(1). In essence, if we currently have a full array of size N, we get one very expensive insertion as we create a new array of size 2N, and then we get up to N more insertions that are very cheap. So, the average cost is quite good. Here are the (mostly mathematical) set operations we will support: inserting a new value into a set removing a value from a set determining if a value is a member of a set forming the union of two sets forming the (nonsymmetric) difference of two sets determining if two sets are equal determining if one set is a subset of another set determining if a set is empty making a copy of a set There are many other operations that would be included in a production version of a set type (intersection, cardinality, isproper-subset-of, etc.). Pay attention to the comments in the header file. All the stated pre- and post-conditions are part of the assignment. Pay particular attention to avoiding memory leaks. Version 2.00 This is a purely individual assignment! 4

5 Testing/Grading Harness Download the posted tar file, C01.tar from the course website and unpack it on a CentOS 7 system. You should receive the following files: driver.c CSet.h CSet.c gradecset.h gradecset.o driver for running all the tests and scoring; see embedded comments! declaration of CSet type shell for implementation of CSet type interface for testing/grading code 64-bit CentOS 7 binary for testing/grading code Unpack the posted tar file, and complete the implementation file (CSet.c). You can then compile with the following commands: gcc o driver std=c99 Wall ggdb3.c.o This will probably not work on CentOS 6, or other Linux distros, due to the presence of incompatible libraries, but it does work on CentOS 7 and on rlogin. You should also note that the posted code will, indeed, compile. And, if you execute it as is, it will not perform correctly, because the given implementations in CSet.c are almost entirely incomplete. You will find it valuable to implement some static ("private") helper functions. Those must be placed in your.c file. Your solution will be compiled with a test driver, using the supplied header file, so if you do not conform to the specified interfaces there will very likely be compilation and/or linking errors. Your solution is expected to achieve a clean run on Valgrind (see the Appendix: Valgrind). Failure to do so will result in a penalty of up to 10%. The requirements related to memory management and computational complexity, will be checked manually by a TA after the assignment is closed. Any shortcomings will be assessed a penalty, which will be applied to your score from the testing harness. What to Submit You will submit your completed version of CSet.c, containing nothing but the implementation of the specified C functions and any static helper functions you write. Be sure to conform to the specified function interfaces. Your submission will be compiled with the test driver: gcc std=c99 Wall m64 ggdb3.c.o The test/grading harness writes a collection of text files to report the results. The file Summary.txt contains overall score information. There are also individual log files for each tested feature, and those contain verbose output showing the tests and results. The Student Guide and other pertinent information, such as the link to the proper submit page, can be found at: Version 2.00 This is a purely individual assignment! 5

6 Pledge Each of your program submissions must be pledged to conform to the Honor Code requirements for this course. Specifically, you must include the following pledge statement in the submitted file: // On my honor: // // - I have not discussed the C language code in my program with // anyone other than my instructor or the teaching assistants // assigned to this course. // // - I have not used C language code obtained from another student, // or any other unauthorized source, including the Internet, // either modified or unmodified. // // - If any C language code or documentation used in my program // was obtained from an allowed source, such as a text book or course // notes, that has been clearly noted with a proper citation in // the comments of my program. // // <Student Name> Failure to include this pledge in a submission may result in being assigned a score of zero. Version 2.00 This is a purely individual assignment! 6

7 Appendix: Deep Copy When an assignment is performed with C struct variables, the operation is carried out (logically) by copying the values of the data members from the object on the right side to the corresponding data members of the object on the left side: struct Point { int x; int y; }; P x: 19 y: 71 struct Point P = {19, 71}; struct Point Q; Q = P; Q x: 19 y: 71 So, what's wrong with that? Nothing. But, suppose we have a struct type that includes a data member that is a pointer to something that's allocated dynamically (like a CSet object); assume we have the CSet object A from page 1 and we perform an assignment: CSet B; B = A; The result will be that B and A will "share" the dynamically-allocated array, because the value of A.Data (which is the address of the array) will have been copied to B. A Capacity: B Capacity: 10 The effect of the assignment is to make a shallow copy of A. That is, the data members of A are duplicated, but if A contains a pointer, the target of that pointer is not duplicated. In most cases, we would expect that performing an assignment like the one above would result in producing a full (or deep) copy of A. That is, we'd expect the result would have been: A Capacity: B Capacity: This is often referred to as maintaining the semantic integrity of an operation. That is, certain operations carry certain expectations (e.g., addition of variables yields a new value but does not modify either variable being added). An implementation that does not satisfy such common, reasonable expectations is likely to cause confusion. Version 2.00 This is a purely individual assignment! 7

8 The possible consequences of making a shallow copy are severe. Since the two objects share their dynamic content, changes to that content via one object will be visible via the other object. (Of course, there may be a circumstance in which that's exactly what we would want.) Worse, if the dynamic content is deallocated via one of the objects, the other object will have lost its dynamic content, but that object will still hold a pointer to the no-longer-accessible memory. That's likely to lead to runtime errors. In C solutions, we fix the shallow copy problem by implementing a specialized copy function that makes a deep copy of an object. For the CSet type, we would implement the following function: / Makes a deep copy of a CSet object. Pre: ptarget and psource are proper Post: psource is unchanged If successful: ptarget->capacity == psource->capacity ptarget->usage == psource->usage ptarget[0:ptarget->capacity-1] == psource[0:psource->capacity-1] ptarget->data!= psource->data, unless ptarget == psource ptarget is proper. else: ptarget is unchanged Returns: true if successful, false otherwise / bool CSet_Copy(CSet const ptarget, const CSet const psource); A deep copy function must: deallocate any previous deep content in the target object duplicate the purely shallow content from the source object into the target object allocate memory for the necessary dynamic content in the target object duplicate the deep content from the source object into the target object It should be noted that the deep copy problem is not unique to C. The same scenario would arise in the other languages you are likely to use, including Java (where it is most commonly handled by supplying a clone() method) and C++ (where it is most commonly handled with a copy constructor and an assignment operator overload). In all cases, the same basic steps must be carried out. Version 2.00 This is a purely individual assignment! 8

9 Appendix: Valgrind Valgrind is a tool for detecting certain memory-related errors, including out of bounds accessed to dynamically-allocated arrays and memory leaks (failure to deallocate memory that was allocated dynamically). A short introduction to Valgrind is posted on the Resources page, and an extensive manual is available at the Valgrind project site ( For best results, you should compile your C program with a debugging switch (-g or ggdb3); this allows Valgrind to provide more precise information about the sources of errors it detects. For example, I ran my solution for this project, with one of the test cases, on Valgrind: wmcquain@centosvm CSet > valgrind --leak-check=full --show-leak-kinds=all --log-file=vlog.txt --track-origins=yes -v driver And, I got good news... there were no detected memory-related issues with my code: Memcheck, a memory error detector Copyright (C) , and GNU GPL'd, by Julian Seward et al. Using Valgrind and LibVEX; rerun with -h for copyright info Command: driver HEAP SUMMARY: in use at exit: 0 bytes in 0 blocks total heap usage: 236 allocs, 236 frees, 23,200 bytes allocated ==10669== All heap blocks were freed -- no leaks are possible ==10669== ==10669== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) ==10669== ==10669== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) And, I got good news... there were no detected memory-related issues with my code. That's the sort of results you want to see when you try your solution with Valgrind. On the other hand, here s what I got from a preliminary version of the solution, before I cleaned it up: Memcheck, a memory error detector Copyright (C) , and GNU GPL'd, by Julian Seward et al. Using Valgrind and LibVEX; rerun with -h for copyright info Command: driver HEAP SUMMARY: in use at exit: 548 bytes in 9 blocks total heap usage: 236 allocs, 227 frees, 23,200 bytes allocated Searching for pointers to 9 not-freed blocks Checked 73,848 bytes 12 bytes in 1 blocks are definitely lost in loss record 1 of 9 at 0x4C29BFD: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x4081FF: Init (gradecset.c:2346) by 0x404950: testcsetsubset (gradecset.c:1096) by 0x400B86: main (driver.c:103) 48 bytes in 1 blocks are definitely lost in loss record 2 of 9 at 0x4C29BFD: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x407A5B: Copy (gradecset.c:2140) by 0x405B88: testcsetunion (gradecset.c:1473) by 0x400BE5: main (driver.c:110)... Valgrind may also detect out-of-bounds accesses to arrays and uses of uninitialized values; a Valgrind analysis of your solution should not show any of those either. Version 2.00 This is a purely individual assignment! 9

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

Creating a String Data Type in C

Creating a String Data Type in C C Programming Creating a String Data Type in C For this assignment, you will use the struct mechanism in C to implement a data type that models a character string: struct _String { char data; dynamically-allocated

More information

Pointer Accesses to Memory and Bitwise Manipulation

Pointer Accesses to Memory and Bitwise Manipulation C Programming Pointer Accesses to Memory and Bitwise Manipulation This assignment consists of implementing a function that can be executed in two modes, controlled by a switch specified by a parameter

More information

struct _Rational { int64_t Top; // numerator int64_t Bottom; // denominator }; typedef struct _Rational Rational;

struct _Rational { int64_t Top; // numerator int64_t Bottom; // denominator }; typedef struct _Rational Rational; Creating a Data Type in C Rational Numbers For this assignment, you will use the struct mechanism in C to implement a data type that represents rational numbers. A set can be modeled using the C struct:

More information

gcc o driver std=c99 -Wall driver.c bigmesa.c

gcc o driver std=c99 -Wall driver.c bigmesa.c 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 focuses on array read/write access and

More information

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data; Creating a Data Type in C Bytes For this assignment, you will use the struct mechanism in C to implement a data type that represents an array of bytes. This data structure could be used kind of like a

More information

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic: Pointer Manipulations Pointer Casts and Data Accesses Viewing Memory The contents of a block of memory may be viewed as a collection of hex nybbles indicating the contents of the byte in the memory region;

More information

Pointer Casts and Data Accesses

Pointer Casts and Data Accesses C Programming Pointer Casts and Data Accesses For this assignment, you will implement a C function similar to printf(). While implementing the function you will encounter pointers, strings, and bit-wise

More information

gcc o driver std=c99 -Wall driver.c everynth.c

gcc o driver std=c99 -Wall driver.c everynth.c C Programming The Basics This assignment consists of two parts. The first part focuses on implementing logical decisions and integer computations in C, using a C function, and also introduces some examples

More information

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic Binary Search Tree This assignment involves implementing a standard binary search tree as a Java generic. The primary purpose of the assignment is to ensure that you have experience with some of the issues

More information

For this assignment, you will implement a collection of C functions to support a classic data encoding scheme.

For this assignment, you will implement a collection of C functions to support a classic data encoding scheme. C Programming SEC-DED Data Encoding For this assignment, you will implement a collection of C functions to support a classic data encoding scheme. Data transmission and data storage both raise the risk

More information

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic: Pointer Manipulations Pointer Casts and Data Accesses Viewing Memory The contents of a block of memory may be viewed as a collection of hex nybbles indicating the contents of the byte in the memory region;

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

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

Pointer Accesses to Memory and Bitwise Manipulation

Pointer Accesses to Memory and Bitwise Manipulation C Programming Pointer Accesses to Memory and Bitwise Manipulation This assignment consists of two parts, the second extending the solution to the first. Q1 [80%] Accessing Data in Memory Here is a hexdump

More information

Pointer Accesses to Memory and Bitwise Manipulation

Pointer Accesses to Memory and Bitwise Manipulation C Programming Pointer Accesses to Memory and Bitwise Manipulation This assignment consists of two parts, the second extending the solution to the first. Q1 [80%] Accessing Data in Memory Here is a hexdump

More information

You will provide an implementation for a test driver and for a C function that satisfies the conditions stated in the header comment:

You will provide an implementation for a test driver and for a C function that satisfies the conditions stated in the header comment: Decision-making in C (Possibly) Intersecting Rectangles Background A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of one corner

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

Each line will contain a string ("even" or "odd"), followed by one or more spaces, followed by a nonnegative integer.

Each line will contain a string (even or odd), followed by one or more spaces, followed by a nonnegative integer. Decision-making in C Squeezing Digits out of an Integer Assignment For part of this assignment, you will use very basic C techniques to implement a C function to remove from a given nonnegative integer

More information

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3 PIC 10B Lecture 1 Winter 2014 Homework Assignment #3 Due Tuesday, February 4, 2014 by 5:00pm. Objectives: 1. To redefine the Big 3 : Copy constructor, Assignment operator, and Destructor for a class whose

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

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

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

Simple C Dynamic Data Structure

Simple C Dynamic Data Structure C Programming Simple C Dynamic Data Structure For this assignment, you will implement a program that manipulates a simple queue of integer values. Your program will include the following features: IntegerDT

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

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

Given that much information about two such rectangles, it is possible to determine whether they intersect.

Given that much information about two such rectangles, it is possible to determine whether they intersect. Decision-making in C (Possibly) Intersecting Rectangles Background A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of one corner

More information

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

EE355 Lab 5 - The Files Are *In* the Computer

EE355 Lab 5 - The Files Are *In* the Computer 1 Introduction In this lab you will modify a working word scramble game that selects a word from a predefined word bank to support a user-defined word bank that is read in from a file. This is a peer evaluated

More information

PR quadtree. public class prquadtree< T extends Compare2D<? super T> > {

PR quadtree. public class prquadtree< T extends Compare2D<? super T> > { PR quadtree This assignment involves implementing a point-region quadtree (specifically the PR quadtree as described in section 3.2 of Samet s paper) as a Java generic. Because this assignment will be

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

A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of two opposite corners:

A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of two opposite corners: Decision-making in C (Possibly) Intersecting Rectangles Background A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of two opposite

More information

Disassembling MIPS32 Machine Programs

Disassembling MIPS32 Machine Programs C Programming Disassembling MIPS32 Machine Programs For this assignment, you will implement a C program that produces an assembly-language representation of a MIPS32 machine code program. The input file

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

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

For storage efficiency, longitude and latitude values are often represented in DMS format. For McBryde Hall:

For storage efficiency, longitude and latitude values are often represented in DMS format. For McBryde Hall: Parsing Input and Formatted Output in C Dealing with Geographic Coordinates You will provide an implementation for a complete C program that reads geographic coordinates from an input file, does some simple

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

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

Disassembling MIPS32 Machine Programs

Disassembling MIPS32 Machine Programs C Programming Disassembling MIPS32 Machine Programs For this assignment, you will implement a C program that produces an assembly-language representation of a MIPS32 machine code program. The input file

More information

CS 103 Lab The Files are *In* the Computer

CS 103 Lab The Files are *In* the Computer CS 103 Lab The Files are *In* the Computer 1 Introduction In this lab you will modify a word scramble game so that instead of using a hardcoded word list, it selects a word from a file. You will learn

More information

CS 1044 Program 6 Summer I dimension ??????

CS 1044 Program 6 Summer I dimension ?????? Managing a simple array: Validating Array Indices Most interesting programs deal with considerable amounts of data, and must store much, or all, of that data on one time. The simplest effective means for

More information

Memory Analysis tools

Memory Analysis tools Memory Analysis tools PURIFY The Necessity TOOL Application behaviour: Crashes intermittently Uses too much memory Runs too slowly Isn t well tested Is about to ship You need something See what your code

More information

Accessing Data in Memory

Accessing Data in Memory Accessing Data in Memory You will implement a simple C function that parses a tangled list of binary records in memory, processing them nonsequentially, and produces a simple text report. The function

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so!

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other electronic devices

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

18-642: Code Style for Compilers

18-642: Code Style for Compilers 18-642: Code Style for Compilers 9/25/2017 1 Anti-Patterns: Coding Style: Language Use Code compiles with warnings Warnings are turned off or over-ridden Insufficient warning level set Language safety

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

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

struct Properties C struct Types

struct Properties C struct Types struct Properties 1 The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct types encapsulate data members struct Location

More information

Programs in memory. The layout of memory is roughly:

Programs in memory. The layout of memory is roughly: Memory 1 Programs in memory 2 The layout of memory is roughly: Virtual memory means that memory is allocated in pages or segments, accessed as if adjacent - the platform looks after this, so your program

More information

CSCI-1200 Data Structures Spring 2013 Lecture 7 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Spring 2013 Lecture 7 Templated Classes & Vector Implementation CSCI-1200 Data Structures Spring 2013 Lecture 7 Templated Classes & Vector Implementation Review from Lectures 5 & 6 Arrays and pointers Different types of memory ( automatic, static, dynamic) Dynamic

More information

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page fact sheet. Your fact sheet may contain definitions and examples,

More information

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

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

Decision Logic: if, if else, switch, Boolean conditions and variables

Decision Logic: if, if else, switch, Boolean conditions and variables CS 1044 roject 4 Summer I 2007 Decision Logic: if, if else, switch, Boolean conditions and variables This programming assignment uses many of the ideas presented in sections 3 through 5 of the course notes,

More information

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-32 assembly code is produced:

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-32 assembly code is produced: This assignment refers to concepts discussed in the course notes on gdb and the book The Art of Debugging by Matloff & Salzman. The questions are definitely "hands-on" and will require some reading beyond

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

CS61, Fall 2012 Section 2 Notes

CS61, Fall 2012 Section 2 Notes CS61, Fall 2012 Section 2 Notes (Week of 9/24-9/28) 0. Get source code for section [optional] 1: Variable Duration 2: Memory Errors Common Errors with memory and pointers Valgrind + GDB Common Memory Errors

More information

CSE 361S Intro to Systems Software Final Project

CSE 361S Intro to Systems Software Final Project Due: Tuesday, December 9, 2008. CSE 361S Intro to Systems Software Final Project In this project, you will be writing a dynamic storage allocator for C programs (i.e., your own version of malloc, free,

More information

18-642: Code Style for Compilers

18-642: Code Style for Compilers 18-642: Code Style for Compilers 9/6/2018 2017-2018 Philip Koopman Programming can be fun, so can cryptography; however they should not be combined. Kreitzberg and Shneiderman 2017-2018 Philip Koopman

More information

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices. Memory and Addresses Memory is just a sequence of byte-sized storage devices. 1 The bytes are assigned numeric addresses, starting with zero, just like the indexing of the cells of an array. It is the

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

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

ECE551 Midterm Version 1

ECE551 Midterm Version 1 Name: ECE551 Midterm Version 1 NetID: There are 7 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

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

Scientific Programming in C IV. Pointers

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

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

CA31-1K DIS. Pointers. TA: You Lu

CA31-1K DIS. Pointers. TA: You Lu CA31-1K DIS Pointers TA: You Lu Pointers Recall that while we think of variables by their names like: int numbers; Computer likes to think of variables by their memory address: 0012FED4 A pointer is a

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)

More information

A brief introduction to C programming for Java programmers

A brief introduction to C programming for Java programmers A brief introduction to C programming for Java programmers Sven Gestegård Robertz September 2017 There are many similarities between Java and C. The syntax in Java is basically

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

int32_t Buffer[BUFFSZ] = {-1, -1, -1, 1, -1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, -1, -1, -1, -1, -1}; int32_t* A = &Buffer[5];

int32_t Buffer[BUFFSZ] = {-1, -1, -1, 1, -1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, -1, -1, -1, -1, -1}; int32_t* A = &Buffer[5]; This assignment refers to concepts discussed in the course notes on gdb and the book The Art of Debugging by Matloff & Salzman. The questions are definitely "hands-on" and will require some reading beyond

More information

CS103 Spring 2018 Mathematical Vocabulary

CS103 Spring 2018 Mathematical Vocabulary CS103 Spring 2018 Mathematical Vocabulary You keep using that word. I do not think it means what you think it means. - Inigo Montoya, from The Princess Bride Consider the humble while loop in most programming

More information

CS 2505 Computer Organization I

CS 2505 Computer Organization I Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other computing devices may

More information

Spring 2016, Malloc Lab: Writing Dynamic Memory Allocator

Spring 2016, Malloc Lab: Writing Dynamic Memory Allocator 1. Introduction Spring 2016, Malloc Lab: Writing Dynamic Memory Allocator Assigned: Mar. 03 Due: Mar. 17, 15:59 In this lab you will be writing a dynamic memory allocator for C programs, i.e., your own

More information

CSE 333 Midterm Exam 5/10/13

CSE 333 Midterm Exam 5/10/13 Name There are 5 questions worth a total of 100 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, closed

More information

CSE 333 Midterm Exam Sample Solution 5/10/13

CSE 333 Midterm Exam Sample Solution 5/10/13 Question 1. (18 points) Consider these two C files: a.c void f(int p); int main() { f(17); return 0; b.c void f(char *p) { *p = 'x'; (a) Why is the program made from a.c and b.c incorrect? What would you

More information

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction CS106L Spring 2009 Handout #21 May 12, 2009 static Introduction Most of the time, you'll design classes so that any two instances of that class are independent. That is, if you have two objects one and

More information

ECE551 Midterm Version 2

ECE551 Midterm Version 2 Name: ECE551 Midterm Version 2 NetID: There are 7 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

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

Programming Standards: You must conform to good programming/documentation standards. Some specifics: CS3114 (Spring 2011) PROGRAMMING ASSIGNMENT #3 Due Thursday, April 7 @ 11:00 PM for 100 points Early bonus date: Wednesday, April 6 @ 11:00 PM for a 10 point bonus Initial Schedule due Thursday, March

More information

Structured Programming with C Wrap-up

Structured Programming with C Wrap-up Structured Programming with C Wrap-up Programming Paradigms Programming Paradigm : Defines how to think when you are programming. Way of approaching the task. Common Paradigms; Imperative : Describe all

More information

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

More information

CS201: Lab #4 Writing a Dynamic Storage Allocator

CS201: Lab #4 Writing a Dynamic Storage Allocator CS201: Lab #4 Writing a Dynamic Storage Allocator In this lab you will write a dynamic storage allocator for C programs, i.e., your own version of the malloc, free and realloc routines. You are encouraged

More information

CSE 333 Midterm Exam 7/27/15 Sample Solution

CSE 333 Midterm Exam 7/27/15 Sample Solution Question 1. (24 points) C programming. In this problem we want to implement a set of strings in C. A set is represented as a linked list of strings with no duplicate values. The nodes in the list are defined

More information

Programming Tips for CS758/858

Programming Tips for CS758/858 Programming Tips for CS758/858 January 28, 2016 1 Introduction The programming assignments for CS758/858 will all be done in C. If you are not very familiar with the C programming language we recommend

More information

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions: VIRG INIA POLYTECHNIC INSTITUTE AND STATE U T PROSI M UNI VERSI TY Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted

More information

ECE 2035 Programming HW/SW Systems Spring problems, 5 pages Exam Three 8 April Your Name (please print clearly)

ECE 2035 Programming HW/SW Systems Spring problems, 5 pages Exam Three 8 April Your Name (please print clearly) Your Name (please print clearly) This exam will be conducted according to the Georgia Tech Honor Code. I pledge to neither give nor receive unauthorized assistance on this exam and to abide by all provisions

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

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer May 13, 2013 OOPP / C++ Lecture 7... 1/27 Construction and Destruction Allocation and Deallocation Move Semantics Template Classes Example:

More information

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM 1 Instructions In this lab you will be writing a dynamic storage allocator for C programs, i.e., your own version

More information

Data Management, Spring 2015: Project #1

Data Management, Spring 2015: Project #1 Advice The assignment description is long, and it is important that you follow the instructions carefully. Be sure to read the entire description before you start implementing. Note that we will be testing

More information

377 Student Guide to C++

377 Student Guide to C++ 377 Student Guide to C++ c Mark Corner January 21, 2004 1 Introduction In this course you will be using the C++ language to complete several programming assignments. Up to this point we have only provided

More information

Assignment 1: grid. Due November 20, 11:59 PM Introduction

Assignment 1: grid. Due November 20, 11:59 PM Introduction CS106L Fall 2008 Handout #19 November 5, 2008 Assignment 1: grid Due November 20, 11:59 PM Introduction The STL container classes encompass a wide selection of associative and sequence containers. However,

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

Lecture Notes on Memory Layout

Lecture Notes on Memory Layout Lecture Notes on Memory Layout 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 11 1 Introduction In order to understand how programs work, we can consider the functions,

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer October 10, 2016 OOPP / C++ Lecture 7... 1/15 Construction and Destruction Kinds of Constructors Move Semantics OOPP / C++ Lecture 7... 2/15

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

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++ Introduction to Programming in C++ Course Text Programming in C++, Zyante, Fall 2013 edition. Course book provided along with the course. Course Description This course introduces programming in C++ and

More information