CS253: Software Development with C++

Spring 2018

HW 4

CS253 HW4: Library                


Management is delighted with your HW3 work, but is unsatisfied:


unserialize throws (in the sense of throw/try/catch) a descriptive std::string if the requested type isn’t the next thing in the input stream. No conversions—short doesn’t match int, and int doesn’t match short.                 

Don’t confuse the base classes ostream and istream with ofstream and ifstream or ostringstream and istringstream.                 

Binary Serialized Format                

In HW3, the tagged short 300 was represented as these eight bytes of hexadecimal digits and spaces:                 

    73 11 2c

Now, the same value will be represented by three bytes. The first byte has the value 0x73, the second byte has the value 0x11, and the third byte has the value 0x2c. No extra spaces, no extra newlines:                 

    output_stream << char(0x73) << char(0x11) << char(0x2c);

These bytes don’t always correspond to printable ASCII values, so you can’t just dump them out to the screen. You have to view them as hex, if you want to understand them. To analyze a file containing such binary values, use the command xxd.                 


If you encounter “STACK FRAME LINK OVERFLOW”, then try this:

    export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a


Your Makefile must, by default, produce the library hw4.a, like this:                 

    ar rcs hw4.a obj1.o obj2.o

Do not include main() in your library.                 


You will have to write a main() function to test your code. Put it in a separate file, and do not make it part of hw4.a. You will also have to create a header file (the name is up to you) that declares the functions, and #include it in your test program that contains main().                 

We will test your program like this:                 

    tar -x <hw4.tar
    g++ -Wall -o testprog our-test-program.cc hw4.a

We will supply a main program to do the testing that we want. You should do something similar.                 

Sample Run                

Here is a sample run of a test program, where % is my prompt:                 

    % cat functions.h 

    #include <string>
    #include <iostream>

    void serialize(bool, std::ostream &);
    void serialize(short, std::ostream &);
    void serialize(int, std::ostream &);
    void serialize(long, std::ostream &);
    void serialize(char, std::ostream &);
    void serialize(const std::string &, std::ostream &);

    void unserialize(std::istream &, bool &);
    void unserialize(std::istream &, short &);
    void unserialize(std::istream &, int &);
    void unserialize(std::istream &, long &);
    void unserialize(std::istream &, char &);
    void unserialize(std::istream &, std::string &);


    % cat coolidge.cc 
    #include "functions.h"
    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <cassert>

    using namespace std;

    int main() {

        cout << "Test begins\n";

        ostringstream oss;
        serialize(short(8), oss);		  // ctor “cast”
        assert(oss.str() == "s" "\x10\x08");  // beware: \x00 ≡ \0 ≡ end of string

        serialize(0x123456789abcdef0L, oss);
        assert(oss.str() == "l\x80\x12\x34\x56\x78\x9a\xbc\xde\xf0"s); 

        try {
            bool b1, b2;
            istringstream iss("tf");
            unserialize(iss, b1); unserialize(iss, b2); assert(b1 && !b2);

            int i;
            iss.str({'i', '\x2e', '\x1d', '\xc0'});
            unserialize(iss, i); assert(i == -123456);

            string str;
            iss.str("S\x10\x0c"s "kakistocracy");
            unserialize(iss, str); assert(str == "kakistocracy");
        catch (string msg) {
            cerr << "Unserialize error: " << msg << '\n';

        // Ensure that fstreams also work
        ofstream ofs;
        serialize("!@#$%^&*()", ofs);   // expected to quietly fail

        cout << "Test ends\n";
        return 0;

    % g++ -Wall -Wextra -Wpedantic coolidge.cc hw4.a
    % ./a.out
    Test begins
    Test ends


The requirements are the same as those for HW3, plus:                 

Tar file                

How to submit your homework:                

    ~cs253/bin/checkin HW4 hw4.tar

How to receive negative points:                

Turn in someone else’s work.                 

User: Guest                 

Edit History Source

Modified: 2018-04-02T17:06                 

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building