I've made this small application to get some practize using dynamic memory allocation with new/delete and to get better at defensive programming. All it does is loop through an array of ints and prompt the user for input, it then prints them all on one line. (The size of the array is determined by user input from the command line)
Code:
/* A simple program to demonstrate the use of dynamic memory allocation and defensive programming */
#include <iostream>
int main(int argc, char *argv[])
{
int n = 0; /* Variable to hold number from commandline */
int i = 0; /* Index variable for program loop */
int * Input_ptr; /* Pointer for memory allocation */
/* Check to see if program was run with correct number of parameters */
if(argc != 2)
{
std::cerr << "Invalid number of parameters." << std::endl;
exit(1);
}
/* Check to see whether or not the first parameter is a number greater than 0 */
if(!atoi(argv[1]))
{
std::cerr << "Wrong parameters, please input a number greater than 0." << std::endl;
exit(1);
}
/* Allocate memory according to input from commandline */
n = atoi(argv[1]);
Input_ptr = new (std::nothrow) int[n];
/* Check for memory overflow */
if(!Input_ptr)
{
std::cerr << "Memory overflow error." << std::endl;
exit(1);
}
/* Main program loop */
for(i; i < n; i++)
{
std::cout << "Enter value " << (i+1) << ": ";
std::cin >> Input_ptr[i];
}
/* Print values to screen */
std::cout << "You have entered: ";
for(i = 0; i < n; i++)
{
std::cout << Input_ptr[i];
/* Check to see if last letter has been printed, otherwise, print comma */
if(!(i == n-1))
{
std::cout << ", ";
}
}
/* End program */
delete[] Input_ptr;
return 0;
}
I'm not quite done with the defensive programming part, i still need to evaluate the input the user enters into the array, if for example someone enters letters rather than numbers, the loop goes through the entire array and ignores the "std::cin". Then when the program prints out the values in the next loop i get some obscure value, i'm fairly sure this is because cin enters a failstate mode, due to invalid input, but i'm not sure how to fix this?
The other problem i have is with the 3rd if statement, the one where i check for memory overflow. If i run my program with a command like this one: "main.exe 130982123098", i _should_ be getting the overflow error message, but instead it just enters the loop and lets me start inputting values, and i'm not patient enough to keep inputting numbers just to see how far i will get before i run out of memory. The computer im compiling on has 1024 Mb of memory, even coupled with the swap file, this should still result in an overflow error, amirite?
My guess is that it has something to do with the fact that i'm overflowing the int i'm storing the values for the loop in. (int n to be exact) Should i rather use something else, like a long int? What if the user then overflows that? What should i do to solve this one?
Oh, and how is my program style-wise, the only reason i'm using "std::nothrow" is because i still haven't gotten around to exceptions yet, so if that is bad practice or something, just bear with me.
Thanks alot for helping
Edit: The boards seems to be screwing up my indentation...