H04 Midterm Practice

The value data not a pointer to the value stored

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSVSetNth(CSparseVector *csv, int index, void *valueAddr); A few implementation details: • The non-empty values should be stored in a CVector. The value data (not a pointer to the value stored elsewhere) is stored contiguously with its index as an entry in the CVector. If the client were storing values of type char, each non-empty value would require 5 bytes total. • We will ignore supporting a client free function. • You do not need to be concerned with any alignment restrictions. We provide the CSparseVector struct and the implementation of the CSVAlloc function below. Your code must work with this code unmodified. –3– typedef struct { CVector *vec; int valueSize; int maxIndex; } CSparseVector; /* Function: CSVAlloc * Usage: CSparseVector *csv = CSVAlloc(sizeof(short), max); * -------------------------------------------------------* Allocates a new sparse vector to hold values of the specified size. * Valid indexes for this sparse vector are [0, maxIndex]. * All values are initially empty. Raises an error if allocation * failed or if maxIndex or valueSize are not positive. */ CSparseVector *CSVAlloc(int valueSize, int maxIndex) { CSparseVector *csv = malloc(sizeof(CSparseVector)); assert(csv != NULL && maxIndex > 0 && valueSize > 0); csv->valueSize = valueSize; csv->maxIndex = maxIndex; csv->vec = CVectorAlloc(sizeof(int) + valueSiz...
View Full Document

This note was uploaded on 02/06/2014 for the course CS 106X taught by Professor Cain,g during the Winter '08 term at Stanford.

Ask a homework question - tutors are online