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.

  1. File à New à Project
  2. 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
  3. In the dialog box that appears click on the Application Settings tag. In the Application Settings window select Console application and Empty project
  4. 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:

  1. Select the Source Files directory
  2. Right-click and select Add à Add New Item
  3. If not selected, select the Visual C++ category to the left and Text File (.txt) from the list of templates
  4. 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
  5. 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:

  1. 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:

  1. 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.

  1. 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.
  2. Select Ok to close the program.

  3. As an option you can select Debug à Start Without Debugging.

Debug

Now you will use the debugger to find the bug:

  1. Debugà Start
  2. 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:
  3. Right click on the first line in the function
  4. Select  InsertàRemove Breakpoint. A red ball indicates that a breakpoint is set. Restart the debugger by doing:
  5. Debugà Stop Debugging
  6. 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:
  7. 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:
  8. Click on the second if statement
  9. 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:
  10. Click on the “n1->value = 1” line
  11. 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:
  12. Select Step Into in the Debug toolbar.
    And watch what happens (if interested try opening the other debugging windows).
  13. 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:
  14. if ((n2 = malloc(sizeof(node))) == NULL) {
    And stop the debuger
  15. 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:

  1. Open the project's Property Pages dialog box.
  2. Click the Linker folder.
  3. Click the System property page.
  4. 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

  1. In Solution Explorer, select the project.
  2. From the View menu, click Property Pages.
  3. In the <Project> Property Pages dialog box, in the Configuration list box, choose Debug (or Release)
  4. Open the Configuration Properties folder and locate the settings you want to change.
  5. 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++