How to Allocate Dynamic Memory (C++)

Open Visual Studio 2013 (or your choice of compiler)., Create a new project., Select "Win32 Console Application" and name your project "DynamicMemory". , Check "Empty project" and click "Finish" to create your project. , Right click on "Source...

14 Steps 2 min read Advanced

Step-by-Step Guide

  1. Step 1: Open Visual Studio 2013 (or your choice of compiler).

    Your start-up screen should look similar to this.
  2. Step 2: Create a new project.

    Click "File" and then "New Project". ,,, Then click "Add" followed by "New Item". (You can also accomplish this by hitting Ctrl+⇧ Shift+A on your keyboard). , Name the file "DynamicMemory". ,,, Declare a variable called "size" of type int.

    Use cout to prompt the user to input how many numbers they would like to store.

    Use cin to store the value they entered into the variable "size". , Dynamic memory is allocated by using the operator "new" "new" allocates memory as a variable (in this case of type int) and returns a pointer to it that we have stored in "p" The "size" variable is then used to dynamically create an array of the exact size that the user has input. , Use cout and the "size" variable to prompt the user to input the requested number of numbers.

    Use cin and a for loop to store the user input into the dynamic array. , Use a cout statement and a for loop to output the contents of your dynamic array. , Enter Ctrl+F5 on your keyboard to run your program.

    Test your program to verify proper execution. , If you attempt to run your program and you receive a build error, check the bottom of your screen and Visual Studio can generally advise you to the nature of your error.

    The most common issue if a program will not compile is a syntax error.

    Be sure to check all your statements end with the proper semicolon ";".

    Visual Studio will highlight most syntax errors in red to help you locate them In the event your program will not compile and no syntax errors are found, try Cleaning your solution by clicking "Build" and then "Clean solution".

    This removes build artifacts from your previous build.
  3. Step 3: Select "Win32 Console Application" and name your project "DynamicMemory".

  4. Step 4: Check "Empty project" and click "Finish" to create your project.

  5. Step 5: Right click on "Source Files" on the right side of your screen.

  6. Step 6: Select "C++ File (.cpp)".

  7. Step 7: You now have a new project and are ready to begin coding.

  8. Step 8: Begin by including the standard iostream header and main function.

  9. Step 9: Prompt the user.

  10. Step 10: Declare a Dynamic Variable.

  11. Step 11: Prompt the user to enter numbers.

  12. Step 12: Output results.

  13. Step 13: Run your program.

  14. Step 14: Troubleshoot if needed.

Detailed Guide

Your start-up screen should look similar to this.

Click "File" and then "New Project". ,,, Then click "Add" followed by "New Item". (You can also accomplish this by hitting Ctrl+⇧ Shift+A on your keyboard). , Name the file "DynamicMemory". ,,, Declare a variable called "size" of type int.

Use cout to prompt the user to input how many numbers they would like to store.

Use cin to store the value they entered into the variable "size". , Dynamic memory is allocated by using the operator "new" "new" allocates memory as a variable (in this case of type int) and returns a pointer to it that we have stored in "p" The "size" variable is then used to dynamically create an array of the exact size that the user has input. , Use cout and the "size" variable to prompt the user to input the requested number of numbers.

Use cin and a for loop to store the user input into the dynamic array. , Use a cout statement and a for loop to output the contents of your dynamic array. , Enter Ctrl+F5 on your keyboard to run your program.

Test your program to verify proper execution. , If you attempt to run your program and you receive a build error, check the bottom of your screen and Visual Studio can generally advise you to the nature of your error.

The most common issue if a program will not compile is a syntax error.

Be sure to check all your statements end with the proper semicolon ";".

Visual Studio will highlight most syntax errors in red to help you locate them In the event your program will not compile and no syntax errors are found, try Cleaning your solution by clicking "Build" and then "Clean solution".

This removes build artifacts from your previous build.

About the Author

J

Jonathan Green

A passionate writer with expertise in crafts topics. Loves sharing practical knowledge.

34 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: