addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then...

38
Standard C++ and Microsoft Visual Studio 2005: A Tutorial and Guide to further Reading Authors: Professor Zachary Wartell Revision: 7/9/2022 4:28:15 AM Copyright August 2006, Zachary Wartell @ University of North Carolina at Charlotte All Rights Reserved. Table of Contents 1. Audience............................................... 2 2. Purpose................................................ 2 3. Introduction........................................... 2 4. Accessing MSVS 2005....................................4 5. Creating New Projects in MSVS 2005.....................5 5.1. Creating a New Project..............................5 5.2. Adding a file to the project........................7 5.3. Compiling and Linking to non-standard Libraries.....8 5.3.1. Overview........................................8 5.3.2. GLUT Instructions...............................9 5.3.3. Using the Third Party Libraries................12 6. Debugging............................................. 12 6.1. MSVS 2005 IDE Debugger.............................13 6.2. How pointer and array bugs manifest themselves as Memory Errors...........................................15 6.3. Memory Leaks.......................................16 6.3.1. Choose a Memory Leak Detection Tool............16 6.3.2. Use a Memory Leak Detection Tool...............18 7. C++ Primer Plus Reading Guide for ITCS 4120..............19 7.1. Per Chapter Relevancy to ITCS 4120.................20 8. MSVS 2005 Further Reading.............................22 8.1. MSDN Documentation on MSVC 2005 IDE................22 8.2. MSDN Documentation on C++..........................23 9. Footnotes............................................. 23

Transcript of addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then...

Page 1: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

Standard C++ and Microsoft Visual Studio 2005: A Tutorial and Guide to further Reading

Authors: Professor Zachary Wartell

Revision: 5/24/2023 7:17:54 AM

Copyright August 2006, Zachary Wartell @ University of North Carolina at CharlotteAll Rights Reserved.

Table of Contents1. Audience......................................................................................................................22. Purpose........................................................................................................................23. Introduction..................................................................................................................24. Accessing MSVS 2005................................................................................................45. Creating New Projects in MSVS 2005........................................................................5

5.1. Creating a New Project........................................................................................55.2. Adding a file to the project..................................................................................75.3. Compiling and Linking to non-standard Libraries..............................................8

5.3.1. Overview......................................................................................................85.3.2. GLUT Instructions.......................................................................................95.3.3. Using the Third Party Libraries.................................................................12

6. Debugging..................................................................................................................126.1. MSVS 2005 IDE Debugger...............................................................................136.2. How pointer and array bugs manifest themselves as Memory Errors...............156.3. Memory Leaks...................................................................................................16

6.3.1. Choose a Memory Leak Detection Tool....................................................166.3.2. Use a Memory Leak Detection Tool.........................................................18

7. C++ Primer Plus Reading Guide for ITCS 4120.....................................................197.1. Per Chapter Relevancy to ITCS 4120................................................................20

8. MSVS 2005 Further Reading....................................................................................228.1. MSDN Documentation on MSVC 2005 IDE....................................................228.2. MSDN Documentation on C++.........................................................................23

9. Footnotes....................................................................................................................2310. Terms.....................................................................................................................2411. Citations.................................................................................................................2412. Appendix I – Document Variables........................................................................2513. Appendix II............................................................................................................25

13.1. Setting an Environment Variable...................................................................2513.2. Referencing an Environment Variable [ADVANCED]................................25

14. Appendix III: hello.c..............................................................................................26

Page 2: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

1. Audience

2. Purpose

This document is a brief guide for students for getting up and running in Microsoft Visual Studio 2005 (MSVS 2005) and getting a MSVS 2005 C++ projects setup as required for ITCS 4120. This document contains references to relevant MSVS 2005 on-line documentation available through the MSVS 2005 Help system and importantly also discusses what aspects of MSVS 2005 are not relevant to ITCS 4120.

This document does not cover C++ programming in general, nor graphics API’s such as GLUT, FLTK or OpenGL. See the class website [1] for information related to these topics.

3. Introduction

Microsoft Visual Studio 2005 (MSVS 2005) is a WIMP (Windows-Icons-Menu-Pointer) integrated development environment (IDE ).

MSVS 2005 Programming LanguagesMSVS 2005 supports many programming languages and many types of applications that are irrelevant to ITCS 4120. The purpose the following paragraphs is to (1) give students a basic awareness of the expanse of programming supported by MSVS 2005 and (2) delineate the small subset of this expanse that is relevant to ITCS 4120.

First, for all MSVS 2005 support programming languages in ITCS 4120 we are only concerned with support for Standard C++.

MSVS 2005 Application Types and Application TemplatesFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications including ATL, CLR, MFC, Smart Devices and Win32— 99% of which are irrelevant to ITCS 4120. ITCS 4120 is only concerned with the latter, “Win32” application development. The other listed application types are Microsoft proprietary libraries, Application Programming Interfaces (API’s), and programming frameworks not used in ITCS 4120.

Next, the Win32 applications are configured as one of two different MSVS 2005 “templates”:

Win32 Console Application Win32 Project

ITCS 4120 uses the “Win32 Console Application” template because only it matches Standard C++.

Page 3: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

To recap, from all MSVS 2005 supported languages, application types and templates, ITCS 4120 is only concerned with:

“Win32 Console Application”’s that comply with Standard C++.

MSVS 2005 Standard C++ and Microsoft Proprietary API’sThere is one further issue. Any compiler and linker that supports Standard C++ not only conforms to the C++ syntax and language semantics but also provides the:

Standard C++ run-time library (CRT) Standard C++ Standard Template Library (STL)

The CRT provides operating system independent functions for functions like string manipulation, file accessing, math functions (sin, cos, sqrt), among others. STL provides a generic programming framework for instantiating common data structures such as lists, dynamic arrays, hash tables, etc. STL also contains a number of generically programmed algorithms that can operate over the generic STL containers. In C++ terminology, these generic types are referred as “parameterized types” or more “C++ templates”.

Win32 Console Application’s use the CRT and STL. However, Win32 Console Application’s can also use the Microsoft Window’s Win32 C API’s for accessing the core functionality of the Microsoft Window’s operating system, file system, 2D GUIs, etc. In ITCS 4120 students will not be using the Win32 C API’s; students will only use the Standard C++ CRT and STL.

Use of STL is optional depending on the students experience level. STL provides lots of useful data structures but it requires learning basic C++ template syntax. MSVS 2005 Compiler and Linker SettingsCompilers and linkers have numerous command-line options to control things like level of optimization of the generated machine code, whether run-time debugging is support in the final executable module, etc. In the MSVS 2005 IDE these are set using the GUI. By default, MSVS 2005 projects have two default sets of compiler and linker options referred to as “Build Configuration”’s. The default configurations are called “Release” and “Debug”. The Release configuration enables all compiler optimizations but disables support for debugging. The Debug configuration disables compiler optimizations and enables interactive debugging.

ITCS 4120 students should always use the default Debug configuration.

MSVS 2005 SummaryIn summary, from all the application type options available in MSVS 2005, ITCS 4120 students only use:

Page 4: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

1. Standard C++ Language2. Standard C++ run-time libraries (CRT)3. Standard Template Library (STL) – optional4. the Win32 Console Application template5. the Debug Build Configuration

For 2D/3D graphics, details will be discussed elsewhere.

4. Accessing MSVS 2005

All computers in the Woodward 335 computer lab have MSVS 2005 installed.

It is strongly, strongly recommended that students at least complete their first few programming project assignments in Woodward 335 where MSVS 2005 (plus other ITCS 4120 programming tools and libraries) have been tested.

Additionally, any student in CCI or BISOM automatically can get MSVS 2005 from Microsoft through the Microsoft Developer Network Academic Alliance (MSDNAA):

http://www.its.uncc.edu/Desktop/software/license.html

AND

http://msdn04.e-academy.com/elms/StorefrontSupport/MsdnaaStorefrontSupportMain.aspx?campus=uncc_cit&np1=17

There is usually a delay when you first sign up for MSDNAA and when you can begin downloading software from MSDNAA.

Again, if a student chooses to work elsewhere besides Woodward 335, the student should first aim to complete the first couple assignments in Woodward 335 and then simultaneously work on installing MSVS 2005 along with the other required ITCS 4120 tools on their alternative machine.

Warning: Microsoft makes a slimmed down version of MSVS 2005 available to the general public from their website. This version does not support OpenGL which is required in ITCS 4120 all graphics programs.

5. Creating New Projects in MSVS 2005

For most ITCS 4120 programming assignments, students will be given a “skeleton” MSVS 2005 project that will already have all the proper compiler and linker settings and perhaps a few skeleton C++ source files. However, for a few early ITCS 4120 programming assignments, students will create their own MSVS 2005 projects as a learning exercise.

Page 5: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

5.1. Creating a New Project

As described in Section 3 Introduction, MSVS 2005 supports many programming languages and many types of application, but ITCS 4120 will only use:

Standard C++ Standard C++ run-time library (CRT) C++ Standard Template Library (optional depending on student experience) Win32 Console Application project template

This section walks you through creating a MSVS 2005 project suitable for these requirements.

MSVS 2005 has two main types of compilation scripts, solution files and project files, they store a project’s compiler and linker configurations. The suffix for these files are

.sln .vcproj

When creating a MSVS project, you create a single .sln file which references at least one .vcproj file. The projects that 4120 students create will contain one .vcproj file.

An additional compilation script with suffix:

.vcproj.USERNAME.MACHINENAME.user

stores various settings associated with debugging and with the MSVS IDE. For a new project, this file is initialized using global MSVS default values or with values from the project’s default user options file (suffix .vcproj.user)if such a file exists.

Instructions:

SVN checkout a working copy of your SVN repository directory for ITCS 4120 as described in [2]. When you SVN checkout your repository directory into your working copy, you should check it out into a directory named ITCS 4120. For example your full path name might be:

H:\My Documents\Course Work\ITCS 4120

So with cmd, you’d type:

1) Next, start MSVS 2005 and switch to the MSVS application.

>cd "H:\My Documents\Course Work\”>svn checkout "svn://linux01.uncc.edu:3690/zwartell-public/ITCS x120/trunk/students/John Doe" "ITCS 4120"

Page 6: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

2) From the menu bar, select File:New:Project.

3) In the New Project dialoga. Select: Visual C++:Win32:Win32 Console Applicationb. In the Name: textbox enter your project name. Type:

Hello World

c. Select the Browse… button and choose your directory. Use the sub-directory name: Project 1

The full path should be something like:

H:\My Documents\Course Work\ITCS 4120\Project 1

d. Make sure the Create Directory for Solution checkbox is unselectede. Select the OK button

4) In the Win32 Application Wizard dialoga. Select: Application Settings…b. In the options now displayed

i. Select the Empty Project checkbox.ii. Verify that the Application Type is Console Applicationiii. Select the Finish button.

5) Select from menu bar: File:Save All6) Verify that these steps created a directory called:

ITCS 4120\Project 1\Hello World

that contains various files including Hello World.sln andHello World.vcproj

In order to add a C++ source file to the above project, continue with the instructions in the next section.

5.2. Adding a file to the project

This section gives an example of adding a C++ source file to the above project.

1) In the Solution Explorer sub-window of the main MSVS 2005 window:a. Right-click on Source Filesb. Select Add→New Itemc. In the Add New Item dialog box:

i. Under Categories sub-window select Visual C++→Code

Page 7: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

ii. Under Templates sub-window select C++ File (.cpp)iii. In the Name textbox type:

main.cppiv. Select the Add button.

At this point, you’ve created a .cpp file. When you later need to create a .h header file you would follow a similar procedure by right-click’ing on Header Files instead of Source Files.

2) In the main.cpp file window:a. Cut & paste the following (nastily formatted!) text:

#include <iostream>

int main (int argc, char** argv) {

using namespace std;cout << "Hello World!" << endl;

return 0;}

b. Select all the text in the main.cppc. Select Edit→Advanced→Format Selection

You should now have a nicely formatted, trivial C++ program.

d. Place the mouse cursor over the line “return 0;”e. Slide the mouse cursor slowly right to left over the edge of the window. The

cursor will changed between 3 shapes: a text entry bar, an arrow pointing diagonally upward and to the right, an arrow pointing diagonally upward and to the left. These cursors let you type in text, select the line or create breakpoints respectively.

f. Position the cursor so that it appears as an upward-left arrow over the left, grey border of the main.cpp sub-window.

g. Left-click. A red circle appears in the grey border indicating you’ve set a breakpoint on the “return 0;” line

3) From the menu bar, select Build→Configuration Manager. In the Configuration Manager dialog box, in the Active Solution Configuration drop-down list, make sure the Debug configuration is selected and close the dialog box.

4) From the menubar, select Build→Build Solution to compile the project.

5) From the menubar, select Debug→Start Debugging. The following should occur:

-a console window should popup with the text “Hello World!”-a yellow sideways arrow should appear over the break point in the MSVS 2005

Page 8: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

main.cpp file sub-window, indicating the program is stopped at the location.

6) From the menubar, select Debug→Continue.

This will continue executing the program, so the program simply exits.

7) Save your work and exit MSVS 2005

8) To turn your program use svn add and commit as described in [2] and verify that the repository received your changes as described in [2].

5.3. Compiling and Linking to non-standard Libraries

5.3.1. Overview

Sections 5.1 and 5.2 describe how to create MSVS 2005 project and generate the compilation scripts (.sln and .vcproj files) suitable for 4120 projects and how to create a trivial C++ program. This section describes how to setup a MSVS 2005 project in order to compile and link with libraries implementing API’s outside of the Standard C++ CRT and STL.

Regardless of what compiler & linker and IDE one uses, in order to program with a library that is not part of the Standard C++, the programmer must give the compiler, linker and OS the following 4 key parameters:

1. include paths - the directory path names where the compiler should search for C++ #include files

2. link-time library paths - the directory path names where the linker should search for the API’s compiled libraries

3. library names - the file names of all the API’s compiled libraries plus the file names of any further libraries required by these API’s compiled libraries you’re using.

4. load-time library paths – Libraries can either be statically or dynamically linked into a executable file. Statically linking means the library’s machine code is stored in the executable file. Such libraries are called static (or ‘archive’) libraries. Dynamic linking means the file with the library’s machine code resides in a location determined by the operation system’s rules and conventions. In Microsoft Windows these libraries are called dynamically linked libraries (DLLs) [6] When you execute a program the operating system’s loader examines the executable to determine which DLLs much be loaded, finds those DLLs, loads those DLLs and links into the executable loaded in memory at load time.

(Standard C++ libraries and common OS libraries are placed in standard locations that the compiler, linker and OS loader will automatically find).

Page 9: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

In Item 1 and Item 2, a path to a single library can be done by either specifying a relative directory path, an absolute path, or an path relative to an environment variable or a combination of the these three. The compiler and linker will use the first path in the list where a given library’s files can be located.

Item 1, Item 2 and Item 3 are set in the .vcproj file. For Item 4 operating systems have a set of rules for determining the sequence of paths where the loader searches for DLLs needed by an executable (such as [5]). DLLs that are built into the OS and the compiler’s run-time environment are automatically placed in a standard location that the loader will search. Further, any library API that has a full installation program stores their DLLs in a similar standard location that the loader will search. For example, OpenGL and the C++ run-time libraries (CRT) should be already installed by MSVS 2005 and Windows.

However, when using DLLs from APIs that are not installed and registered with the OS, you have to specify the load-time library paths yourself (Item 4). In ITCS x120, we will use the .vcproj.user settings file to indicate additional DLL search paths by adding to the PATH environment variable. Note, this addition to the PATH environment variable is only active when running your programs from within the MSVS 2005 IDE. If you want to run your compiled programs from outside the IDE, a quick solution is to copy the non-registered DLLs to the directory containing the executable file.

5.3.2. GLUT Instructions

These instructions describe compiling and linking a program using the GLUT and OpenGL libraries. The OpenGL DLLs are automatically installed and registered by Windows and MSVS. GLUT is not automatically installed. For 4120, the GLUT DLL is kept in a separate student directory rather than installing and registering it with the OS. This guarantees that students’ programs link against the 4120 provided version of this and other 4120 specific DLLs. (Trouble can occur if a student’s program links against some other incompatible version of the DLL that was installed and registered by another application).

1) When you checkout your SVN repository into your ITCS 4120 directory you will get a subdirectory such as:

ITCS 4120\Third Party Libraries\glut\ This directory tree contains the compiled glut library plus other GLUT program examples.

2) Next, add an environment variable called UNCC_ITCS_GRAPHICS to your user environment variable settings and set UNCC_ITCS_GRAPHICS to the full path to your ITCS 4120 directory such as:

H:\My Documents\Classes\ITCS 4120

Page 10: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

Section 13.1 Setting an Environment Variable describes how to set environment variables.

3) Start MSVS 2005.

4) As described in Section 5.1 create a new project called: Hello World – OpenGL and GLUT within your ITCS 4120\Project 1 directory. Verify that this creates a new directory:

ITCS 4120\Project 1\Hello World – OpenGL and GLUT

5) As described in Section 5.2 add a new file main.cpp to the project and cut-and-paste the code from 14 Appendix III: hello.c.

6) If the MSVS 2005 main window is missing a Solution Explorer sub-window, then from the menubar select View→Solution Explorer

7) In MSVS 2005 Solution Explorer sub-window, right-click on the projectHello World – OpenGL and GLUT, select the Properties popup menu. In the Property Pages dialog box:

a. In the left-most sub-window, expand by left-clicking the Configuration Properties and then C/C++.

b. In the left-most sub-window, select C/C++→Generalc. In the right-most sub-window, in the Additional Include Directories textbox,

enter the following:

"$(UNCC_ITCS_GRAPHICS)\Third Party Libraries\glut\include"

This step accomplishes Item 1 in key parameters for the GLUT library. (OpenGL is already installed by MSVS 2005 so no additions are needed here).

d. In the left-most sub-window, expand by left-clicking Linker and select Linker:General

e. In the right-most sub-window, in the Additional Library Directories textbox, enter the following:

"$(UNCC_ITCS_GRAPHICS)\Third Party Libraries\glut\lib\glut\$(ConfigurationName)"

This step accomplishes Item 2 in key parameters for the GLUT library.

f. In the left-most sub-window, expand by left-clicking Linker and select Linker:Input

Page 11: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

g. In the right-most subwindow, in the Additional Dependencies textbox, enter the following:

glut32.lib opengl32.lib glu32.lib

This accomplishes Item 3 in key parameters [1 ] for glut.

h. Returning to the left-most sub-window and the Configuration Properties select Debugging. In the right-side subwindow, for Working Directory add:

$(ProjectDir)

then for Environment add:

PATH=$(UNCC_ITCS_GRAPHICS)\Third Party Libraries\glut\lib\glut\Debug;%PATH%

This accomplishes Item 4 in key parameters for GLUT.

You can add more ; separated paths between the final ';' and '%PATH%' as needed for other DLL libraries.

8) From the MSVS 2005 menubar, select: File→Save All

9) In the source code change the line:

glutCreateWindow ("hello");

to

glutCreateWindow ("hello from YOUR NAME");

10) From the MSVS 2005 menubar, select: Build→Build Solution

11) From the MSVS 2005 menubar, select: Debug→Start Debugging

12) Use SVN to add and commit your files as describe in Citation [2].

Congratulations! If all has gone well, you have now successfully created a C++ project suitable for ITCS 4120; learned how to add source files and compile and link against non-standard API’s; and learned a tiny bit about the interactive debugging and text formatting capabilities in MSVS 2005.

5.3.3. Using the Third Party Libraries

Page 12: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

Each student’s SVN repository directory contains a subdirectory "Third Party Libraries". The repository directory contains svn links to distribution packages for various Open Source or UNCC developed libraries that will be used in this course. Within cmd type:

This lists the svn external links set for "Third Party Libraries" and shows what SVN repository directories each library is being retrieved from. Notice a number of the repository directories end with a directory name "precompiled."

The full distribution packages for Open Source libraries are generally quite large. They contain all the source code for the library software plus the source code for many example programs and utilities. By default the svn externals link property set on your "Third Party Libraries" library link to a reduced versions of the distribution packages to save disk space. These reduced versions include the precompiled versions of the libraries so you don’t have to compile the libraries themselves.

If you want to retrieve the full distribution package of a particular library follow the instructions in "Third Party Libraries\README.html". You may want to do this to either

examine the example programs provided with these libraries or recompile a given library if your MSVS 2005 on your particular workstation gives

error messages indicating incompatibility with the precompiled version of a library (see the above README.html for more details).

6. Debugging

ITCS 4120 is programming intensive class. You need strong programming and debugging skills. Depending on your C++ skills you may need to read up on C++ before working through this section (see Section 7: C++ Primer Plus Reading Guide for ITCS 4120).

6.1. MSVS 2005 IDE Debugger

Below are subsections from the MSVS 2005 Help documentation (also found on the web via http://msdn2.microsoft.com). You should read through this documentation and use the program in:

ITCS 4120\Examples\List

to test out the various debugging features. Students do not have permission to commit changes to the Examples repository subdirectory (it is linked to a shared repository directory accessed by all students). Therefore, you should svn copy

>cd "Third Party Libraries" >svn propget svn:externals

Page 13: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

ITCS 4120\Examples\List

to

ITCS 4120\Project 1\List

Do not do a file system copy! (Doing a file system copy will copy over the hidden SVN directory[3] (.svn or _svn) from Examples\List and will not allow you to svn commit ‘Project 1\List’). To perform a SVN copy, within cmd type:

Do the rest of your assignment from the ITCS 4120\Project 1\List working copy directory.

Read the following MSDN documentation. (Note, these hyperlinks change occasionally but you can always search for the title of the section starting from MSDN Library):

1. MSDN Library  > Development Tools and Languages > Visual Studio .NET > Developing with Visual Studio .NET > Building, Debugging, and Testing > Debugging > Using the Debugger 

Read the following subsections

a. Debugging Basics b. Execution Control – read all the hyperlinked material linked off this pagec. Breakpoints – read all the hyperlinked material linked off this page. You

should understand: breakpoints, conditional breakpoints, disable/enabling breakpoint, the breakpoint window, creating/destroying breakpoints

d. Debugging Tools for Inspecting Your Program – read all following hyperlinked materials linked off this page. (The subsections listed below are most relevant to 4120):i. Using the Autos Window ii. Using the Locals Window iii. Using the This (Me) Window iv. Using the QuickWatch Dialog Box v. Using the Watch Window vi. Using the Call Stack Window

As you learn the debugger play with List.sln. Do the following:

1. Step through the entire program in debugging mode using various combinations of ‘Step Over’, ‘Step Into’, ‘Step Out’. Examine the variables in the Watch window, Auto Window and Local Window. Examine their contents.

>cd "ITCS 4120\Project 1\">svn copy "svn://linux01.uncc.edu:3690/zwartell-public/ITCS x120/trunk/examples/List" .>svn commit List –m "-added skeleton code"

Page 14: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

2. Put a breakpoint on List::Node::traverseAll. Run the program. Each time the code stops in ::traverseAll examine the call stack window. Move through different call frames in the call stack and examine the local variables for each call frame. Then continue program execution (MenuBar-Debug-Continue).

a. Note that in the variable watch windows you can expand the List::Node::next field to examine the complete linked list.

3. Set breakpoints at the List constructor and destructor (List::List and List::~List) and the List::Node constructors and destructors. Then with these breakpoints set do the following:

a. Run the program (Debug→Start Debugging) from start to finish. Study the behavior noting when the constructors and destructors are called.

b. Rewrite the function ‘main’ so that ‘List list’ is a ‘List* list’ and uses dynamic allocation and deallocation instead of automatic allocation/deallocation. Rerun with above breakpoints. Again study the behavior noting when the constructors and destructors are called.

svn commit these changes using the log message:

-exercise 6.1 3.b

c. Comment out the call to the delete operator in main. Rerun again. Does the destructor ever get called?

svn commit these changes using the log message:

-exercise 6.1 3.c

Uncomment the code to activate it again.

d. Comment out the code inside List::~List so that no List::Node’s are deleted. Rerun and study the constructor/destructor behavior.

svn commit these changes using the log message:

-exercise 6.1 3.d

Uncomment the above code.

e. Return the ‘main’ function to its original form (‘List list’). Comment out the code inside List::~List so that no List::Node’s are deleted. Rerun and study the constructor/destructor behavior. Note, that ‘list’ is automatically constructed and deallocated but the List::Node’s are never

Page 15: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

deallocated.

svn commit these changes using the log message:

-exercise 6.1 3.e

Uncomment the above code.

svn commit these changes using the log message:

-exercise 6.1 final

6.2. How pointer and array bugs manifest themselves as Memory Errors

Use the program in:

ITCS 4120\Examples\pointers and arrays

As in 6.1, svn copy the directory

ITCS 4120\Examples\pointers and arrays

to directory

ITCS 4120\Project 1\pointers and arrays

Then svn commit the above directory.

This assignment should be used in conjunction with class lecture slides “Thinking in C++ (!Java)” [Revision: 1.3+].

Using the debugger, step through and examine how the each of the functions called 'function_ouchX' manifest their bugs as memory errors. Examine local variables such as ‘two’ in the watch window and the cout output and see how this or other local variables get overwritten.

Cut and paste the following questions document questions.txt or questions.doc in 'ITCS 4120\Project 1\pointers and arrays' and write your answer to these questions and svn add and commit the document.

Question 1: What goes wrong with ‘function_ouch1’? How does the programming error manifest itself? How is the cout output affected? What run-time errors occur?Question 2: What goes wrong with ‘function_ouch2’? How does the programming error manifest itself? How is the cout output affected? What run-time errors occur?

Page 16: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

Question 3: What goes wrong with ‘function_ouch3’? How does the programming error manifest itself? How is the cout output affected? What run-time errors occur?

Examine the code in function ‘matrixMultiplication’. Run this function.

Question 4: How long does it take to compute 10M multiplications using standard C++ array indexing?Question 5: How long does it take to compute 10M multiplications when we check for out of bounds array indices?

6.3. Memory Leaks

6.3.1. Choose a Memory Leak Detection Tool

Two free memory leak detection tools are available: MSVS 2005 Memory Leak Detection tool and Visual Leak Detector (VLD). VLD is more powerful, but currently the precompiled version may not be compatible with all MSVS 2005 installations. Students should first test to see if VLD works on their computer. If so, use VLD. Otherwise, use the MSVS 2005 Memory Leak Detection tool. (If you are ambitious, you can download from the VLD website the VLD source code and compile it on your particular workstation using your particular OS and version of MSVS 2005).

Visual Leak DetectorVisual Leak Detector (VLD) is a free tool for reporting memory leaks.

In your SVN directory are two related items:

1. ITCS 4120\Third Party Libraries\VisualLeakDetector – This is pre-compiled version of VLD and its documentation, etc.

2. ITCS 4120\Examples\Memory Leak 1 – This is a MSVS 2005 C++ example that links with Visual Leak Detector (VLD) and allows you to see the type of output report that VLD makes when a program has a memory leak. This example has all the required path settings (Items 1,2,3 and 4) setup to link and use VLD. You will have to add these settings to the settings of any programs you write if you want to use VLD. Further, you must include the line #include <vld.h> in your main.cpp file.

If you can compile and run Memory Leak 1 without errors then VLD is compatible with your machine. Use VLD to detect memory leaks in your programs.

By default VLD dumps its output in the Output tabbed window in the bottom of the MSVS IDE. The output list the line numbers of code that did a new that had no corresponding delete. Double clicking on the line in the ‘Output’ window will jump

Page 17: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

to the line in your code.

Run and experiment with Memory Leak 1.

You can tell VLD to dump its diagnostics into a .txt file by altering the file vld.ini in your MSVS project subdirectory. Memory Leak 1 includes an example file vld.ini which is set to dump the diagnostics to the file memory_leak_report.txt

MSVS 2005 Memory Leak DetectionMSVS’s standard C++ Run-Time (CRT) library can be configured to perform memory leak detection. The program ITCS 4120\Examples\Memory Leak 2 in your SVN directory similar to Memory Leak 1 but it uses the MSVS 2005 leak detection instead of VLD.

When a program ends, the CRT library will print to the Output tabbed window a list of the file and line numbers of every new operator that dynamically allocated an object where that object was not deleted before the program exited. Double clicking on the line in the Output window will jump to the line in your code. Run and experiment with Memory Leak 2.

In general to use MSVS 2005 Memory Leak detection in your own programs your must:

1. Insert the lines:

#define CRTDBG_MAP_ALLOC#include <stdlib.h>#include <crtdbg.h>

as the first 3 lines in each and every .cpp file in your project.

2. Insert the lines:

/** \brief redefine 'new' to use debug version. Note this must go after all #include's */#define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )

right after the last #include in each and every .cpp file in your project.

3. Insert the line:

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

as the first execute line in the ‘main’ function.

For more information see:

MSDN Library > Development Tools and Languages > Visual Studio .NET > Developing with Visual Studio .NET > Building, Debugging, and Testing > Debugging > Debugging Visual C+

Page 18: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

+ > Detecting and Isolating Memory Leaks  > Enabling Memory Leak Detection

6.3.2. Use a Memory Leak Detection Tool

After determining which Memory Leak detection program you can use do the following exercise.

SVN copy (see 6.1) the directory:

ITCS 4120\Examples\List

to directory:

ITCS 4120\Project 1\List Leak Check

SVN commit the above new directory using the log message:

-exercise 6.3.2 start

Depending on the memory leak detection tool you choose to use, make all necessary changes in order to enable leak detection in your source code. SVN commit these changes with the log message:

-exercise 6.3.2 added memory leak detection

Return to Section 6.1 and repeat exercise 3 a through e with the following modifications. For each svn commit instead of using a log message like:

-exercise 6.1 3.d

use the log message

-exercise 6.3.2 3.d

Also, create a document memory leak.txt in Project 1\List Leak Check. For each part 3 a through e cut and paste the output message from the memory leak detector into this document and label it as ‘Output 3a’, ‘Output 3b’, etc. SVN add and commit this document as well.

7. C++ Primer Plus Reading Guide for ITCS 4120

This section is a guide for reading C++ Primer Plus by Stephen Prata for students in ITCS 4120, etc. Students who are not using C++ Primer Plus may still find this guide useful as it indicates what C++ features/topics are most important to 4120. A good online reference is

Page 19: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

www.cplusplus.com

This guide assumes the reader is a Computer Science student with significant programming background in Java and some minor exposure to C or C++. The guide indicates what chapters and topics in C++ Primer Plus are most relevant to ITCS 4120. The guide also indicates:

areas of C++ that Java-only programmers are likely to find new or difficult features of C++ that 4120 students will need to succeed in 4120. features of C++ that 4120 students could theoretically avoid in their own 4120

project code without incurring penalties in their grades (but at the expense of becoming less of a professional C++ programmer )

OverviewChapters 1, 2 and 9 cover high level organization principles of C++ that differ from Java. Chapter 3, most of 4, 5, and 6 should be easy reading for Java programmers. Chapter 7 and 8 cover material where again C++ differs from Java. Chapter 10 covers the basics of C++ classes most of which should feel familiar to Java coders. Chapter 13 covers details of how C++ implements various OOP concepts. These details differ from Java.

C++ features covered in Chapter 11, 14 and 15 are not strictly necessary for “getting by” in 4120.

For Java-only programmers (or programmers otherwise not familiar with pointers), pointers are perhaps the most difficult issue in C++ and are unavoidable. Chapter 4 introduces pointers and Chapter 12 discusses dynamic memory allocation. Significant issues in pointers are discussed in various other chapters as well. In the detailed overview below, a chapter section that discusses pointers are marked with the word pointer in bold type with highlighting.

Subsection and topics that are integral to basic C++ programming and are significantly different that Java are marked in bold type.

7.1. Per Chapter Relevancy to ITCS 4120

Chapter 1 & 2 OVERVIEW: This covers the basic structure of a C++ program. Must read.

Chapter 3: Dealing with Data OVERVIEW: This covers C++ basic types. By and large all these concepts

should already be familiar to students from prior programming experience with Java, etc.

Chapter 4: Compound Types OVERVIEW: This covers arrays, C-style strings, the C++ string class, C++

structures, unions, and enum’s, and pointers

string, arrays, etc. - You need to carefully understand how C++ arrays, C-style strings, and the C++ string class differ from analogous Java structures.

Page 20: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

C++ struct’s should seem familiar as compared to a Java class. C++ enum’s are pretty straight forward. C++ union’s don’t have a direct analog in traditional Java; Students can probably

get through ITCS 4120 without them. pointers – Java-only programmers will find pointers the toughest concept of C++.

C++ Primer Plus does a pretty good job of explaining pointers. Of Chapters 1-6, the material on pointers is likely where Java-only programmers will spend most of their time learning conceptually new material.

Chapter 5: Loops and Relational Expressions OVERVIEW: 95% of this chapter is quite similar to Java, but a few items might

take you by surprise such as the , and ?: operators and typedef’s. Also pay attention to the semantics of the assignment operator, =, and equality operator, ==, in C++ especially as they relate to C++ pointers. Otherwise Chapter 5 should seem quite familiar to Java programmers.

Chapter 6: Branching Statements and Logical Operators OVERVIEW: 95% of this chapter is quite similar to Java. Chapter 6 should

seem quite familiar to Java programmers.Chapter 7: Functions: C++’s Programming Modules

OVERVIEW: C++ functions differ in a number of important ways from Java methods. Hence Java-only programmers should expect digesting this chapter to take longer than many of the other chapters. Some key issues:

o function’s can exist outside of a classo function prototypeso function arguments

formal vs. actual parameter, pass-by value using pointer parameter to alter argument arrays as arguments

o pointers and const – some subtle issues hereo functions and strings -o functions and structures -o recursion – a number of graphics algorithms use recursion!o pointers to functions – this important for callbacks which are used

frequently in graphics and GUI API’sChapter 8: Adventures in Functions

OVERVIEW: Roughly 50% of this chapter covers features of C++ that novice C++ coders might want to avoid in 4120; while the other 50% covers concepts that are important to basic C++ development

inline functions – students code need not use these features; but API’s used in 4120 use these occasionally

reference variables – since OpenGL is a C API, C++ references won’t be used in OpenGL calls; however, in writing general C++ code understanding what references are and when to use them is important.

default arguments and overloading – student written code need not use these features; although some API’s used in 4120 may use these features.

Page 21: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

function templates – students will not need to write new templates, but understanding how to use them is useful if students choose to use the C++ Standard Template Library (STL).

Chapter 9: Memory Models and Namespaces OVERVIEW: This chapter covers important concepts related to C++ allocation

of memory and the C++ namespace. Java-only programmers will find these C++ mechanisms similar to Java packages but there are significant differences.

Chapter 10: Objects and Classes OVERVIEW: This chapter introduces objects and classes in C++. Many high

level concepts are shared with Java. Some key differences from Java:o C++ destructoro C++ this pointer

Chapter 11: Working with Classes OVERVIEW: This chapter covers intermediate to advanced C++ concepts.

4120 projects can mostly be accomplished without using these C++ features. operator overloading – not necessary for 4120

(professional C++ graphics code uses this feature a lot but its not necessary for 4120)

friends – not necessary for 4120(professional C++ code with sophisticated information hiding use friend feature a lot, but for 4120 you won’t be graded on this aspect of your code)

conversions – not necessary for 4120Chapter 12: Classes and Dynamic Memory Allocation

OVERVIEW: This chapter discusses using new and delete and some of the subtleties of using dynamic memory. Theoretically, a student could probably implement all 4120 projects without using dynamic memory (i.e using static or automatic memory allocation alone); the student’s code would just be amateur C++ code. If a student’s 4120 project satisfies the project’s requirements but avoids using dynamic allocation points will not be deducted from the project grade. This allows for less sophisticated C++ students to still succeed in 4120. Chapter topics:

o default constructors, copy constructors, assignment operator and subtletieso returning objectso pointers to objects

Chapter 13: Class Inheritance OVERVIEW: This chapter discusses class inheritance in C++. 4120 students are

expected to be already familiar with all of the OOP concepts in this chapter already, but students may not be familiar with the C++ specifics. 4120 projects are typically more algorithmically intensive than they are OOP design intensive. Hence, 4120 projects won’t be graded on the quality of the code’s OOP design. Theoretically, 4120 projects could probably be implemented using minimal OOP principles. Unless other specified in a project assignment document, use of basic OOP principles is encouraged, but not required.

Chapter Topics:

Page 22: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

inheritance and public derivation – 4120 students should already understand these concepts

protected access – 4120 students should already understand these concepts. C++ details maybe new to students.

constructor initializers - 4120 students should already understand most of these concepts but C++ details will be new to students.

upcasting and downcasting and object pointers – the details are likely new to 4120 students. Importantly, a pointer to a base class can be assigned an address of any derived class object.

virtual member functions, static vs. dynamic binding – 4120 students are expected to understand polymorphism in general (PREREQUISITE). The C++ implementation differs from Java, however.

abstract base classes, pure virtual functions, etc. – not necessary for 4120 student written code; however APIs used in 4120 use these occasionally.

Chapter 16: The string Class and the Standard Template Library OVERVIEW: This chapter discusses the string class and the basics of the STL.

Use of STL is encouraged in 4120 but not required.Chapter 17: Input , Output, and Files

OVERVIEW: This chapter discusses reading and written to files and the standard input. Parts of this chapter likely going to be needed for 4120 projects.

Appendix G OVERVIEW: List of basic STL data structures. These can be very useful in

4120 but requires the student spend additional time to learn C++ template’s which implement generic types. See also www.cplusplus.com.

8. MSVS 2005 Further Reading

MSVS 2005 Help system accesses tons of MSVS 2005 documentation over the web at the Microsoft Developer Network (MSDN) website. As described in Section 3 Introduction, MSVS 2005 supports lots of languages and lots of application categories irrelevant to ITCS 4120.

8.1. MSDN Documentation on MSVC 2005 IDE

From MSVS 2005 main window, selecting “Help:How Do I” should open a window called “How Do I in Visual C++”.

The primary relevant reading material is found by following these links in this window:

Getting Started Development Environment

More importantly note the “Navigation” section at the bottom of the page. Each MSDN page has such as section and this is how you navigate through the MSDN documentation hierarchy.

Page 23: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

Pages of particular relevance are:

MSDN Library  > Development Tools and Languages > Visual Studio > Visual C++ > Getting Started > How Do I in Visual C++

MSDN Library  > Development Tools and Languages > Visual Studio > Integrated Development Environment for Visual Studio > Building, Debugging, and Testing 

MSDN Library  > Development Tools and Languages > Visual Studio > Integrated Development Environment for Visual Studio > Building, Debugging, and Testing > Debugging in Visual Studio

Note: MSDN website’s hyperlink locations change occasionally so you may have to manually navigate to these listed sections instead of following the hyperlinks directly from this document.

8.2. MSDN Documentation on C++

MSVS 2005 and MSDN contains its own set of C++ documentation; however, because MSVS 2005 supports so many other languages and supports so many non-standard C++ libraries and C++ Microsoft-only language extensions, new C++ students often find using MSVS 2005 C++ documentation too confusing. The help system tends to give query results that aren’t relevant to standard C++. Therefore, it is recommended to a C++ book or www.cplusplus.com instead for C++ knowledge.

9. Footnotes

[1] Note, strictly speaking for this example program, main.cpp, we could leave out a number of the fltkXXX.lib libraries, the OpenGL .lib’s (opengl32.lib glu32.lib), and some of the remaining .libs. (These remaining .lib’s are Win32 API .lib’s used by FLTK and OpenGL). However, for pedagogical purposes, in this example we include all the FLTK, OpenGL, and dependent libraries that will be used during all the ITCS 4120 projects.

[2] In detail, the IDE path is expanded to a command-line set of options passed to the compiler and linker. (Such as /I INCLUDE_DIRECTORY_NAME). If an environment variable is referenced that is not defined, the resulting command-line set of options will be corrupted. Appending the "\.", etc. to the specified path guarantees that even if the environment variable is undefined, the expanded command-line set of options will contain some valid path and avoid corrupting the command-line options.

[3] The directory, .svn or _svn, is where SVN stores internal information about what repository directory your working copy came from. If you copy Examples\List in its entirety onto Project 1\List, the Project 1\List\.svn directory's internal info. still associates Project 1\List with the repository directory svn://linux01.uncc.edu:3690/zwartell-public/ITCS

Page 24: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

x120/trunk/examples. This repository directory is shared by all student repositories and is read-only by students. Students cannot commit changes to these read-only repository directories.

In sum, if you copy SVN working copy directories around and want to add the copied files to new and different repository directory, you must remove all the .svn directories from the copied directory (and any of its subdirectories).

10. Terms

IDE – “integrate development environment”. A basic IDE provides:

a text editor with special purpose editing and formatting abilities for writing source code. A few common abilities include

o syntax highlightingo function name auto-completiono point & click jumping to function and structure definitions

a WIMP interface for compiling programs a WIMP interface for creating compilation scripts to tell the compiler what files to

compile a WIMP interface for interactively debugging by

o setting breakpoints (either conditional or non-conditional) o stepping through your program line-by-line o viewing/changing values of variables and objects during program

executiono viewing the run-time call stack of active functions during program

execution

11. Citations

[1] http://www.cs.uncc.edu/~zwartell/ITCS 4120/current/web page/ITCS 4120 - Wartell.html . ITCS 4120, Zachary Wartell.

[2] ‘SVN for ITCS 4120.doc’ '[http://www.cs.uncc.edu/~zwartell/ITCS%204120/current/projects/SVN%20for%20ITCS%204120.doc]

[3] http://www.ds.uncc.edu/ [4] “2 - FLTK Basics :Writing Your First FLTK Program”, FTLK Documentation: FLTK

1.1.x, http://www.fltk.org.[5] Search Path Used by Windows to Locate a DLL. http://msdn2.microsoft.com/en-

us/library/7d83bc18(VS.80).aspx[6] http://en.wikipedia.org/wiki/Dynamic-link_library

Page 25: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

12. Appendix I – Document Variables

ITCS 4120 – Anywhere in this document where the link ITCS 4120 appears you should substitute the directory of you working copy of your ITCS4120 student repository directory

13. Appendix II

13.1. Setting an Environment Variable

To add an environment variable to your Windows operating system’s user account settings: Right-click “My Computer”. Select “Properties”. Select the “Advanced” tab in the “System Properties” Dialog. Select the “Environment Variables” button. Under “User variables for XXXX” select “New”. For "Variable Name" type:

UNCC_ITCS_GRAPHICS

for “Variable Value” enter the full, absolute path to your directory. For example:

H:\Documents and Settings\zwartell\My Documents\classes\ITCS 4120

13.2. Referencing an Environment Variable Audience: [ADVANCED] This is for advanced students who desire to customize the compilation process.

Note that if you reference an environment variable in the paths for Item 1 and Item 2 when using the MSVS 2005 IDE, you must be aware of the following:

1. variable syntax is $(VARIABLE_NAME) instead of the MS-DOS/Windows command prompt syntax of %VARIABLE_NAME%.

2. you should put "’s around the path name such as:

"$(VARIABLE_NAME)\."

3. you should append a "\." or some subdirectory (if applicable) to the environment variable name in the IDE’s path list (as shown above). This is necessary incase the environment variable is not defined on some other computer in which you try to compile [2].

To add an environment variable to your user operating system settings in Windows: Right-click “My Computer”. Select “Properties”. Select the “Advanced” tab in the “System Properties” Dialog. Select the “Environment Variables” button. Under “User

Page 26: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

variables for XXXX” select “New”. For variable name type:

ITCS_4120_FLTK

for “Variable Value” enter the full, absolute path to you FLTK directory. For example:

C:\Documents and Settings\zwartell\My Documents\classes\ITCS 4120 (public)\Fall 2006\Third Party Libraries\fltk

14. Appendix III: hello.c

This code is straight from the OpenGL Redbook examples.

/* * hello.c * This is a simple, introductory OpenGL program. */#include <GL/glut.h>

void display(void){/* clear all pixels */ glClear (GL_COLOR_BUFFER_BIT);

/* draw white polygon (rectangle) with corners at * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */ glColor3f (1.0, 1.0, 1.0); glBegin(GL_POLYGON); glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0); glEnd();

/* don't wait! * start processing buffered OpenGL routines */ glFlush ();}

void init (void) {/* select clearing color */ glClearColor (0.0, 0.0, 0.0, 0.0);

/* initialize viewing values */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);}

/* * Declare initial window size, position, and display mode

Page 27: addr: terrain scene gr paged ob navigati nav 4120/current... · Web viewFor C++, MSVS 2005 then supports the development and programming of many different types of C++ applications

* (single buffer and RGBA). Open window with "hello" * in its title bar. Call initialization routines. * Register callback function to display graphics. * Enter main loop and process events. */int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100); glutCreateWindow ("hello"); init (); glutDisplayFunc(display); glutMainLoop(); return 0; /* ANSI C requires main to return int. */}