CC++,。
Objective
The objective of this lab is focused on a variety of subjects from C to C++. Here is the summary of the some of the topics that are covered:
- Dynamic allocation of memory in C
- C structs with dynamic allocation of memory
- Introduction to C++ reference type
- Introduction to C++ classes
Note: some of the exercises in this lab are implemented in C and some in C++. For the exercises that you need to compile and run them, please make sure to use the correct compilation commands:
- gcc for C programs
- g++ for C++ programs.
Exercise A: Allocation of Memory on the Heap
This is an in-lab exercise.
Read This First
Before doing this exercise, you should review lecture notes or a C textbook to be sure you know exactly how C library function malloc works.
What to Do
Download the file lab7ExA.c and lab7ExA.h from D2L.Read these files carefully and draw a memory diagram for point one, assuming that all the calls to the library function malloc succeed.
What to Submit
Submit your diagram as part of your in-lab report.
Exercise B: C++ References
This is an In-lab exercise
Read This First
The AR notations that we use in ENCM 339 to show C++ references are different from ordinary types such as: int, double, and pointer notations. When we declare a reference we just provide an alias name for another memory space. In other words, a reference in C++ doesn’t have its own memory space; therefore we show them as a link (a line) between the reference-identifier and the actual allocated memory spaces. There are two little circles on both ends of these links. On one end there is a solid-black circle that represents the reference, and on the other end there is an open circle that represents the actual allocated memory space. Here are a few examples:
1 2 3 4 5
|
int a = 40; int*p = amp;a; intamp; ra = a; int*amp; rp = p; intamp; rra = ra;
|
Notice that all references ra, rp, and rra must be initialized with an expression that represents an actual memory space or another reference.
What To Do
Download the file lab7ExB.cpp from D2L. Then, draw an AR diagram for point one.
Note: You don’t need to compile and run this program but in case that you decided to run it, you should use the g++ command which is the command to compile C++ programs in our ICT 320 lab under the Cygwin. The executable file created will be still a.exe, by default.
g++ -Wall lab7ExB.cpp
What To Submit
Submit your diagrams as part of your in-lab report.
Exercise C: Objects on the Computer Memory
This is also an In-lab exercise
The objective of this exercise is to help you in understanding how C++ objects are shown on a memory diagram, and how a member function of a class uses the ‘this’pointer to access an object associated with a that particular call to the function. For further details please refer to your lecture notes and slides.
What to Do
Download files cplx.cpp, cplx.h, and lab7ExC.cpp from the D2L and draw AR diagrams for: point one and point two.
For this exercise also you just need draw the diagrams. However, if you want to compile and run it from command line in our ICT 320 lab, you should have all of the given files in the same directory and from that directory you should use the following command to compile and create an executable:
g++ -Wall cplx.cpp lab7ExC.cpp
Please notice that you shouldn’t have the header file name(s) in this command.
What To Submit
Submit your diagram as part of your in-lab report.
Exercise D: String Manipulation Using Dynamic Allocation in C
This is a post lab exercise.
The main objective of this exercise is to give you the opportunity to practice dynamic allocation of memory in C, for the purpose of string operations such as copying a string into another string, appending a string to the end of another string, or truncating the size of a string.
Read This First
Creating a kind of wrapper data type that encapsulates several related data is a common practice to build data structures such as vectors, string, linked list, trees in object oriented programming languages such as C++. We will discuss the proper way of applying these concepts during the lectures and possibly future labs.
In this exercise a structure type, called Lab7Text is defined that is similar to the structure given in Exercise A. This structure simply contains two pieces of related data. First one is a pointer that is supposed to point to a dynamically allocated array of characters, used as storage for a null-terminated c-string, called text. Second one is an integer number that represents the length of the stored string in the first data member (number of characters up to but excluding the ‘\0’).
Here is the definition of this structure: