Microsoft
Visual Studio .NET Tutorial
Creating a Visual Studio
.NET C++ Program
First you
need to create a project. A project organizes all files used for developing,
compiling, debugging and building your project.
- File à New à Project
- Select from Project Types Visual C++
Project on the left hand side, and select Win32
Project as a template on the right hand side
Fill in
Project Name and select Location for your project.
Click OK
- In the dialog box that appears click on the
Application Settings tag. In the Application Settings window
select Console application and Empty
project
- When finished, a Solution explorer on the
right hand side of the window gives an overview of your newly created project.
Three empty directories are created: Source Files, Header Files and Resource
Files
Adding
Files
The next
step is to add one or more source files:
- Select the Source Files directory
- Right-click and select Add à Add New Item
- If not selected, select the Visual C++
category to the left and Text File (.txt) from the list of
templates
- Fill in Name with a .c
extension (i.e. test.c) and select Location for your file.
The location of your project is initially suggested as a location. Select
Open
- The new test.c file is now created under the
Source Files directory and visible in the Solution Explorer to the
right
Write
Code
Add the
following code to the source file:
#include
<stdio.h>
#include
<stdlib.h>
typedef
struct node_s node;
struct
node_s {
int
value;
node
*next;
node
*prev;
};
node
*create_list(void);
int
main(void)
{
node
*p;
node *head =
create_list();
for (p = head; p
! = NULL; p = p->next)
printf("%d\n", p->value);
return
0;
}
node
*create_list(void)
{
node *n1,
*n2;
if
((n1 = malloc(sizeof(node))) = = NULL) {
printf("Could not allocate memory\n");
exit(1);
}
if ((n2 =
malloc(sizeof(node))) == NULL) {
printf("Could not allocate memory\n");
exit(1);
}
n1->value =
1;
n1->next =
n2;
n1->prev =
NULL;
n2->value =
2;
n2->next =
NULL;
return
n1;
}
Notice that
the code has a bug.
Compile
Now we
compile the written code:
- Build à Compile
This will
create an object file in the directory: <your location>\Debug (called
<your filename>.obj)
The code
contains a bug that the compiler detects. So you will get a warning, but just
ignore it.
Build
Before the
application can be run it must be built:
- Buildà Build <your
project>
This will
create an executable file in <your location>\Debug called <your
projectname>.exe.
Execute
And finally
the application can be executed.
- Debugà Start
(It is not
necessary to compile and build an application before starting. If it is not
compiled, Visual Studio will ask for a start of the
compiler)
Output: You will get an error message saying that the
application referenced memory at “0xcccccccc”. An invalid pointer often causes
this kind of error. This is the bug of your program and generally a common bug
in C.
- Select Ok to close the program.
- As an option you can select Debug
à Start Without
Debugging.
Debug
Now you
will use the debugger to find the bug:
- Debugà Start
- Click Ok to close the exception message box.
A little yellow error should point to the line that triggered the exception.
To examine what caused the error, insert a breakpoint at the beginning of the
function:
- Right click on the first line in the function
- Select InsertàRemove Breakpoint. A red ball indicates that a
breakpoint is set. Restart the debugger by doing:
- Debugà Stop Debugging
- Debugà Start
The code stops
running when the breakpoint is encountered. One way to find a bug is to watch
how the variables in “the bug area” change during code execution, so you:
- Click on the variables button in the
Debug toolbar.
A window with the variables in the function is opened. Since
you are not interested in debugging the malloc function you can:
- Click on the second if statement
- And click on the Run to Cursor
button on the Debug toolbar.
The yellow arrow should now point to the
second if statement. Notice also that the n1 value is changed in the variables
window. Do the same for the second if statement:
- Click on the “n1->value = 1” line
- And click on the Run to Cursor button
on the Debug toolbar.
Now you are interested in seeing what happens when
each statement is executed:
- Select Step Into in the Debug
toolbar.
And watch what happens (if interested try opening the other
debugging windows).
- Repeat until the exception message box is
shown.
Now you can see that in the Variables window n2 is NULL, and hence
you know that the exception was caused by a invalid pointer (n2). So you
change the second if statement to:
- if ((n2 = malloc(sizeof(node))) == NULL)
{
And stop the debuger
- Click Debug à Stop debugging in the
toolbar.
Execute
again
Rebuild and execute the code. The numbers one
and two should be written, along with
Press
any key to continue, which indicates that the program
exited.
Change Stack
Size
For some
strange reason you think you need a bigger stack for the program. So you
right-click on the <Project-name> in the solution explorer window
and:
- Open
the project's Property Pages dialog box.
- Click
the Linker folder.
- Click
the System property page.
- Modify
one of the following properties:
- Stack
Commit Size
- Stack
Reserve Size
The reason
for doing this was to show, how to change project settings in Visual Studio
.NET
Build
Release
When using
the Microsoft compiler the default project configuration is Debug,
which produces slower and bigger executables. You should build your application
using the Release configuration when you are done
debugging.
To change the settings for the Debug (or Release)
configuration
- In
Solution
Explorer, select the project.
- From
the View menu, click Property Pages.
- In
the <Project> Property Pages dialog box, in the
Configuration list box, choose Debug (or Release)
- Open
the Configuration Properties folder and locate the settings you want to
change.
- Edit
the value of the settings in the properties grid.
Edited for
Microsoft Visual Studio .NET. Anna-Brith A.
Jakobsen, august 2004.
Lars Ailo Bongo, University of Tromsø,
Fall 2001, Visual Studio C++