Re: Sample Code Feedback.
- main() has to declared as int, not void;
- There is no need to create int *pa in the heap, because you already know its size (20); in that case you will need to delete pa[] what you missed. As an alternative you could use std::vector;
- Your do/while loop does not check the overrun of pa array.
There may be some other problems in the code you have posted, but I currently cannot test it with some compiler...
Re: Sample Code Feedback.
Ah, I knew about delete and I did forget...
Ok OK OK , now that you brought that up.. My original goal was to create a pointer to an array, and let the program
determine the size of the array at RunTime. I can't leave the brackets blank, I get an error, So i left like that with a 20... But how would I create a dynamic array so that it is only as big as it needs to be?
Thank you very much by the way.
Re: Sample Code Feedback.
I already wrote that you could use std::vector. Or if you use MFC then MFC CArray class
Re: Sample Code Feedback.
Quote:
Originally Posted by
jbarrera03
My original goal was to create a pointer to an array, and let the program
determine the size of the array at RunTime. I can't leave the brackets blank, I get an error, So i left like that with a 20... But how would I create a dynamic array so that it is only as big as it needs to be?
As Victor said, you use a std::vector. Specifically, use the push_back function to add items dynamically. The vector will take care or all the required memory management.
There is hardly ever a need in C++ to use new []; you should use vector instead.
Re: Sample Code Feedback.
I'll add that indentation is supposed to indicate the relationship of one statement to another. If a statement is indented, the implication is that it executes based on the statement above it. Looking at your code
Code:
if (temp1 != 0)
{
pa[i]=temp1;
pa[i+1]=temp2;
i++;
i++;
}
relationships are implied that don't exist. Properly formatted it would look like this.
Code:
if (temp1 != 0)
{
pa[i]=temp1;
pa[i+1]=temp2;
i++;
i++;
}