CS105: Introduction to Computer Programming: C++

Assignment #7: Memory Management


Tuesday, March 24th at 11:59pm


Change your BigInt class so that it can hold positive integers of any size. Make sure that everything that used to works still works, and that you're not leaking or corrupting memory.


You know all of the things that your BigInt class can do? It should still be able to do all of those things, but now with arbitrarily large numbers. (Well, okay, not any arbitrary number; just any number that will fit in your computer's memory.)

Instead of storing your digits as a fixed-size array of integers, like this:

class BigInt {
  int digits[20];
...your BigInt class should have a dynamically-allocated array of integer digits, like this:
class BigInt {
  int *digits;
  int length;

That means you'll have to allocate the digit array in your various constructors. For example, if your string constructor was given the string "999", then you should dynamically allocate a 3-digit integer array.

If you do this correctly, you should be able to use the string constructor to create BigInts that have any number of digits:

  BigInt b1( "42" );
  cout << b1 << endl;   // should still work

  BigInt b2( "12345678901234567890" );
  cout << b2 << endl;   // 20 digits should still work

  BigInt b3( "123456789012345678901234567890" );
  cout << b3 << endl;   // 30 digits should work too!


  1. Since dynamically-allocated arrays don't know their own length, you'll probably want to store the length of your dynamically-allocated array as a separate class variable.
  2. Don't leak memory. For every new, there should be a delete. When should your dynamically-allocated array be deleted?
  3. Remember the rule of three: if your class requires either a copy constructor, destructor, or overloaded assignment operator, then it probably requires all three.
  4. Examine each of your BigInt functions carefully. Almost every function will require at least a small change to go from the 20-digit case to the N-digit case.
  5. A copy constructor and overloaded assignment operator look similar, but need to do different things. A copy constructor is responsible for creating data for a new object. The assignment operator acts on an object that has already been created, and gives it a new value.
  6. You can stress-test your implementation with something like this:
      while( true ) {
        BigInt num1("999999999999999999");
        BigInt num2(0);
        num2 = num1;
    Run that code, and in a separate window run the "top" command. You should probably see your program running. In the VIRT column, is the amount of memory used by your program steadily increasing?

Compiling and Running

As usual, you should be able to compile your code using the Makefile from the last assignment by typing:


If all goes well, you shouldn't see any error messages. Then you can run your code by typing:


Whatever you've included in your main function should run, and any output that is generated should appear on your screen.

Turning it in

When you're happy with your code, use the turnin program to submit your BigInt.cc and BigInt.h files. Use assignment7 as the assignment name.

Submission Checklist