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

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

A Capacity: 10 Usage: 4 Data:

Pointer Accesses to Memory and Bitwise Manipulation

Creating a String Data Type in C

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

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 Accesses to Memory and Bitwise Manipulation

Pointer Accesses to Memory and Bitwise Manipulation

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

Pointer Casts and Data Accesses

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

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

struct Properties C struct Types

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

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

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

TI2725-C, C programming lab, course

CSE 333 Midterm Exam Sample Solution 7/28/14

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

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

Debugging (Part 2) 1

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

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

Praktische Aspekte der Informatik

Memory Analysis tools

DEBUGGING: DYNAMIC PROGRAM ANALYSIS

Use Dynamic Analysis Tools on Linux

Disassembling MIPS32 Machine Programs

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

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

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

Disassembling MIPS32 Machine Programs

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

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

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

CS 2604 Minor Project 1 Summer 2000

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

Scientific Programming in C IV. Pointers

Intermediate Programming, Spring 2017*

CS 2604 Minor Project 1 DRAFT Fall 2000

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

Pointers and Memory Management

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

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

Simple C Dynamic Data Structure

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

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

Accessing Data in Memory

ECE551 Midterm Version 1

CS 103 Lab The Files are *In* the Computer

Scientific Programming in C IX. Debugging

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

A short session with gdb verifies a few facts; the student has made notes of some observations:

CSE 160 Discussion Section. Winter 2017 Week 3

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

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];

Recall that an assembler translates code written in mnemonic form in assembly language into machine code.

ECE 551D Spring 2018 Midterm Exam

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

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:

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

CSE 333 Midterm Exam 5/10/13

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

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

ECE551 Midterm Version 2

ICHEC. Using Valgrind. Using Valgrind :: detecting memory errors. Introduction. Program Compilation TECHNICAL REPORT

Programming Tips for CS758/858

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

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

Fundamental Concepts: array of structures, string objects, searching and sorting. Static Inventory Maintenance Program

CS 11 C track: lecture 5

CS 1044 Project 2 Spring 2003

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

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

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

CS 2704 Project 1 Spring 2001

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

CMSC 341 Lecture 2 Dynamic Memory and Pointers

18-642: Code Style for Compilers

Structured Programming with C Wrap-up

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

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

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

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

CS 1044 Program 2 Spring 2002

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

CS61, Fall 2012 Section 2 Notes

ECE 551D Spring 2018 Midterm Exam

ECE 2035 Programming HW/SW Systems Fall problems, 4 pages Exam Three 14 November Your Name (please PRINT clearly) Signed.

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

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

The following notes illustrate debugging a linked list implementation with gdb.

6.S096: Introduction to C/C++

CS 2704 Project 3 Spring 2000

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

CS 2604 Minor Project 3 Movie Recommender System Fall Braveheart Braveheart. The Patriot

ECE 15B COMPUTER ORGANIZATION

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

Transcription:

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 on the concept of a "mesa", and make use of the following data type: / Represents a "mesa", defined as a contiguous sequence of array cells, storing the same value, which must be at least MIN_MESA_HEIGHT, and including at least MIN_MESA_LENGTH such cells. The "mass" of such a sequence is defined as the sum of the values in the sequence. A Mesa object is said to be proper if its fields have been set, and each of the following conditions holds: - height and length satisfy the minimum requirement - end = begin + length - 1 - begin >= 0 - mass = sum of the values in the sequence / #define MIN_MESA_HEIGHT 12 #define MIN_MESA_LENGTH 8 minimum height to be a mesa minimum length to be a mesa struct _Mesa { uint16_t height; the height of the mesa uint16_t begin; the first index included in the mesa uint16_t end; the last index included in the mesa uint32_t mass; the product of the mesa's height and length }; typedef struct _Mesa Mesa; The function you will write must traverse a given array of integers, and create a proper Mesa object, if that is possible, but must also detect the case where a given array does not contain a proper mesa. Download the posted tar file for this assignment to your CentOS installation or rlogin, and unpack it. The tar file contains the following C source code and object files: driver.c bigmesa.h bigmesa.c Mesa.h Mesa.o checkanswer.h checkanswer.o Generator.h Generator.o C test driver (read the comments!) header file for assigned function (do not modify!) shell file for implementing your solution header file for Mesa type (do not modify!) 64-bit implementation of Mesa support functions header file for grading code (do not modify!) 64-bit implementation of grading code header for test case generator 64-bit implementation of test case generator You can compile your solution with the given code by using the command: gcc o driver std=c99 Wall ggdb3 driver.c bigmesa.c.o Run the test/grading code by using the command:./driver [-repeat] The driver will generate test cases, call your implementation of bigmesa() on each test, compare your answer to the reference solution, and write results to the file C06Results.txt, showing how your implementation of bigmesa() fared on the test cases that were generated. The results will indicate your answer for each test case, whether your answer was correct, and a total score for the test run. Version 2.00 This is a purely individual assignment! 1

Although the given code will compile correctly, the resulting program will not satisfy the requirements of the assignment, since the supplied implementation of the required function doesn't do anything useful. So, you must correctly complete the implementation of the bigmesa() function. You may need to add include directives to your.c file, as needed for any C Standard Library features you use. You may write secondary "helper" functions if you like; if so, those must be defined and declared within the supplied bigmesa.c file. Such functions should be declared as static. The testing code in driver.c creates a large "buffer" around the array that bigmesa() will analyze. One purpose of the buffer is to protect from possible segfault violations if your implementation of bigmesa() wanders slightly outside the boundaries of the array. Another purpose of the buffer is to confuse your solution if you wander outside the boundaries of the array. You will implement the following function: / Given an array of nonnegative integers, a mesa is a sequence of at least MIN_MESA_LENGTH identical values. The length of a mesa is the number of values in the sequence, and the mass of a mesa is the sum of the values in the sequence. bigmesa() determines the mesa of maximum mass in the array that is passed into it. bigmesa() reports its results by setting three extern variables: mesastart, mesaend and mesamass. Pre: A[0:A_Size - 1] are nonnegative integers. Returns: a Mesa object that is proper, or NULL, if no mesa was found, Restrictions: - uses only its parameters and local automatic variables - does not read input or write output - does not use a second array. - uses only pointer arithmetic whenever accessing an array element. / Mesa bigmesa(const uint16_t const A, uint16_t A_Size); What to Submit You will submit your modified version of the file bigmesa.c to the Curator, this time via the collection point C07. That file must include any helper functions you have written and called from your version of bigmesa(); any such functions must be declared (as static) in the file you submit. You must not include any extraneous code (such as an implementation of main() in that file). Your submission will be graded by running the supplied test/grading code on it. Version 2.00 This is a purely individual assignment! 2

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, the Internet, or any other unauthorized source, either modified or unmodified. - If any C language code or documentation used in my program was obtained from an authorized source, such as a text book or course notes, that has been clearly noted with a proper citation in the comments of my program. - I have not designed this program in such a way as to defeat or interfere with the normal operation of the Curator System. <Student Name> Failure to include this pledge in a submission may result in score of zero being assigned. Change Log Version Posted Pg Change 2.00 Mar 9 Base document. Version 2.00 This is a purely individual assignment! 3

Appendix: Quick Introduction to struct Types The struct mechanism in C has many similarities to the class mechanism in Java: Bundling of a collection of data values (fields) as a unit; data values are referenced by name. Can be passed as parameters or used as return values from functions. Can be assigned; values of corresponding data members are copied from source to target. Deep content (i.e., dynamically-allocated members) is not copied on assignment. There are also important differences: All fields are public; there is no access control. Functions cannot be members of a struct variable. May be accessed by name or by pointer; Java objects can only be accessed by reference. Can be created by static declarations or by dynamic allocation (not used in this assignment). Here's a common use for a struct type in C: struct _Rational { int64_t top; int64_t bottom; }; typedef struct _Rational Rational; We could create a Rational object in either of these ways (statically or dynamically): Rational R; Rational pr = malloc(sizeof(rational)); In both cases, the fields in the new Rational object are (logically) uninitialized. For now, we will only consider the first case, in which the object was created by a static allocation (so it's stored on the stack). When we create a struct variable statically, we can also initialize it (but only in the variable declaration): Rational R = {37, 63}; We can implement functions to carry out operations on Rational objects (they just can't be member functions). For example: Rational Rational_Add(const Rational Left, const Rational Right) { Rational Sum; create object to hold the result initialize that object Sum.Top = Left.Top Right.Bottom + Left.Bottom Right.Top; Sum.Bottom = Left.Bottom Right.Bottom; } return (a copy of) it to the caller return Sum; Note how the fields of a Rational object are accessed here; we use the name of the object, followed by the field-selector operator ('.'), followed by the name of the field: Sum.Top What about accessing struct variables by pointer? Here's an alternate approach to implementing an addition function for Rational objects: Version 2.00 This is a purely individual assignment! 4

C06: Array Accesses in C void Rational_Add(Rational const psum, const Rational Left, const Rational Right) { } initialize Sum psum->top = Left.Top Right.Bottom + Left.Bottom Right.Top; psum->bottom = Left.Bottom Right.Bottom; Here, psum is assumed to point to an existing Rational object created by the caller. We can access members of psum in two ways: or (psum).top = Left.Top Right.Bottom + Left.Bottom Right.Top; psum->top = Left.Top Right.Bottom + Left.Bottom Right.Top; In the first approach, psum is actually a name for the object that psum points to, but we have to put parentheses around that due to precedence rules in C. In the second approach, the arrow operator ('->') takes a pointer to a struct variable as its left parameter and the name of a field in that struct variable as its right parameter. Most C programmers prefer the second syntax. Version 1.01 This is a purely individual assignment! 5

Appendix: C06: Array Accesses in C 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 (www.valgrind.org). 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 a project from CS 2506 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: ==10805== Memcheck, a memory error detector ==10805== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==10805== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info ==10805== Command: driver ==10805== ==10805== HEAP SUMMARY: ==10805== in use at exit: 0 bytes in 0 blocks ==10805== total heap usage: 236 allocs, 236 frees, 23,200 bytes allocated ==10805== ==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) That's the sort of results you want to see when you try your solution with Valgrind. On the other hand, here s (part of) what I got from a student s solution: ==12020== Memcheck, a memory error detector Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info Command: driver HEAP SUMMARY: in use at exit: 3,336 bytes in 28 blocks total heap usage: 239 allocs, 211 frees, 23,548 bytes allocated Searching for pointers to 28 not-freed blocks Checked 73,896 bytes==10805== 40 bytes in 1 blocks are definitely lost in loss record 2 of 28 at 0x4C29BFD: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x4085A4: Init (gradecset.c:2355) by 0x40607F: testcsetdifference (gradecset.c:1516) by 0x400D1B: main (driver.c:123) 40 bytes in 1 blocks are definitely lost in loss record 3 of 28 at 0x4C29BFD: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x406FE0: testcsetcopy (gradecset.c:1832) by 0x400D7A: main (driver.c:130) 1 errors in context 1 of 57: Conditional jump or move depends on uninitialised value(s) at 0x407CF9: Equals (gradecset.c:2119) by 0x40802B: checkunion (gradecset.c:2207) by 0x405F0B: testcsetunion (gradecset.c:1478) by 0x400CBC: main (driver.c:116) Uninitialised value was created by a heap allocation at 0x4C29BFD: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x40160E: CSet_Union (CSet.c:338) by 0x405EED: testcsetunion (gradecset.c:1477) by 0x400CBC: main (driver.c:116) Version 1.01 This is a purely individual assignment! 6

C06: Array Accesses in C Invalid read of size 4 at 0x40127D: CSet_Contains (CSet.c:151) by 0x4015B7: CSet_isSubsetOf (CSet.c:310) by 0x404A9C: testcsetsubset (gradecset.c:1055) by 0x400C5D: main (driver.c:109) Address 0x51f81b8 is 0 bytes after a block of size 40 alloc'd at 0x4C29BFD: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x40483D: testcsetsubset (gradecset.c:1011) by 0x400C5D: main (driver.c:109) As you see, Valgrind can 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. In fact, for this assignment, you are unlikely to see any memory leaks whatsoever, but you may encounter other kinds of errors that Valgrind can detect. So, try it out if you are having problems. Version 1.01 This is a purely individual assignment! 7