Dynamic Arrays

Hi!

This is my first post here on cplusplus.com.
I'm fairly new to programming and I was making a small program that keeps taking integers from the user and place them in a dynamic array.
Once the user enters the number zero, the input loop stops and then the program calculates the average of all the numbers entered. I already got the code to work perfectly fine, but I have a couple of questions:

1. I learned how to create dynamic arrays from this website, but I didn't really get why we use this code
 
  int i, *input = new int[i];

Can someone please simplify and explain it to me? (keeping in mind that I'm a noob!) I do have some background about pointers, but the new int part is confusing me a little.

2. Reading through the forums here, I picked up that I have to delete the dynamic array after using it (I'm guessing to save memory), but every time I use delete[] input; , I get a debugging error.

So the second question is: why do I get an error when I add delete[] input; ?


Any feedback is VERY appreciated!
Thanks!
Last edited on
1) Where exactly do you add delete[] input; in your code?
2) What error message do you get, exactly? (You may want to copy-paste it.)

Small observations I have about your code so far:

1) You should unlearn to use goto. By not using goto you are forcing yourself to structure your program (mainly by using loops). This increases the quality of your code, because it is easier to understand and maintain.

2) As a C++ programmer you should unlearn using namespace std; and prefer writing the std:: prefix instead, as in:

1
2
std::cout << std::endl; // instead of
cout << endl;


This will also increase the quality of your code; one reason is that everybody reading your code will know instantly which elements are from the C++ standard library and which are your own.
Hi Catfish666,

First I'd like to thank you so much, especially for your observations! I REALLY appreciate the feedback on what's good practice and what isn't!

I tried placing the delete[] input; in multiple places actually to get rid of the error, but mainly, I placed it right after the output
( cout << "The average is " << average << " and the largest number is " << largestNum << endl; )

The error I get says
Debug Error!
Program: ...\projects\Lab Assignment (test)\debug\Lab Assignment (test).exe

HEAP CORRUPTION DETECTED: after Normal block (#209) at 0x00c9B789
CRT detected that the application wrote to memory after end of heap buffer.


Thanks again for your help and if you have any other helpful tips or comments, please let me know!
Here are some sites to help you become proficient:
http://www.mochima.com/tutorials/
http://www.icce.rug.nl/documents/cplusplus/
http://www.parashift.com/c++-faq/
http://en.cppreference.com/w/
http://www.horstmann.com/cpp/pitfalls.html

About the error message, it seems that your program writes outside of the allocated memory, at some point.
Are you sure the user does not input more than arraySize elements?

You could enforce that at most arraySize elements are read by using a for() loop to read the user input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstddef>

// ...

std::size_t arraySize = 10; // prefer using size_t for sizes and lengths

for (std::size_t i=0; i < arraySize; ++i)
{
    int temp;

    std::cout << "Please, enter a number: ";
    std::cin >> temp;

    if (temp == 0)
        break;
    else
        input[i] = temp;
}


The code above has some problems too: one is that the user can never enter the number 0.

Another, greater problems is that we don't check if the input operation succeeded. What if the user inputs "hello" instead of a number?

Anyway, the proficient C++ way is to use std::vector instead of dynamically allocated arrays. Read a tutorial about vectors here:

http://www.mochima.com/tutorials/vectors.html

The major benefit of using vectors is that we don't need to worry about their size. Vectors grow in size as we add new elements.
I did think about the problems you're talking about but I just couldn't find a way around to check if the user inputs a text instead of a number.
How do programmers proficiently do these types of checks?

I think I'll just do this the right way then and learn vectors as you suggested. I can't thank you enough, Catfish666! You really helped me here.
And thanks a lot for all the references you provided! I'll make sure I check them all.

Wish you all the best!

Regards

I just realized, the code I provided doesn't even set a "actualSize" variable, to keep track of the used length of the array. Oh well.

doctorX wrote:
How do programmers proficiently do these types of checks?

When input fails, the stream goes into an error state. (Internally its error flags are set.)
You can check for this by putting the input operation as the condition in an if() or a while(), as in:

1
2
3
4
5
6
7
int i;

while (std::cin >> i)
{
    // this loop will stop as soon as the user
    // enters something that a number can't be extracted from
}


http://www.parashift.com/c++-faq/istream-and-while.html

As of C++11 (the C++ standard of year 2011) you could also use the regex (regular expressions) library to validate input. Note that this library is not yet available in libstdc++ of GNU GCC and MinGW.

http://www.cplusplus.com/reference/regex/
Ohh ok. I totally get the logic behind the loop part, I just didn't know that std::cin is allowed as the loop's condition.

I started going through the references you posted and I got to say, they really open your eyes!

So again, thanks for all your help, Catfish666! I really appreciate it :)
Topic archived. No new replies allowed.