C++/Array of user defined length
Our C++ teacher has taught us that one cannot create an array of user defined length.So most of us students made it our goals to create our own ways to make such an array.
Here is my idea which very unfortunately does not work:
1] I create a class called 'constant' with an integer say 'num' as datamember.
2] In the public section I create 2 functions:
One a constructor which uses the user provided value for
The other is get_value() which accesses num when called.
3]In the main() I create an object of type constant with its num initialised to a user provided value.
Now I try to create an array whose size is constant.num using the get_value function.
The result is: I never get past the compiler.
Any hints about this method in making it successful?
Chennai Tamil Nadu India Asia
I do not know how fast you expect experts to answer posted questions but we do _not_ sit here just waiting for questions to pop into our in boxes, we actually have three days in which to answer. Also time zones come into play on the Internet – if you post a question while I am asleep then you are going to have to wait until I have woken up and got around to checking my email, and sorted any more urgent matters (clients, business, government etc.). Remember we are volunteers. AllExperts do not pay us to answer your questions. It all happens online using email and the web. So if the Internet is slow so are responses, if my Internet connection is down then I cannot collect questions or post answers until it is fixed. Quite frequently the AllEpxerts site is dead or really, really slow when I wish to post an answer so I have to wait until it comes back online until I can post answers…
In this case however there was very little delay. My answered questions log on AllExperts reveals that you posted the question at 09:52 am on April 20th 2007 (GMT) and I posted my answer to you at 12:30 pm 20th April 2007 (GMT) – less than 3 hours later. When you consider you were the third question I had answered and questions take time to write I think that in fact I was extremely timely. So I fail to understand why I lost 2 points for timeliness in your rating. You did not actually pick up the answer until the 22nd April so if anyone is tardy here it is you or you have misunderstood the difference between me posting my answer and maybe you receiving an email saying it was posted. I should check you email service it could be there was a delay there. Also if you were really that bothered I suspect you could have checked the AllExperts directly to see if I had posted an answer.
If you expect a more immediate response to questions then I suggest you find yourself a personal tutor who you can approach in real time on a one-to-one basis. They will most likely require a fee however.
I suspect that your teacher has in fact said that using the locally defined arrays you are using at the moment you cannot size them using a run time value.
This is perfectly true. You cannot and this is because the compiler has to allocate the space for such arrays on the stack frame of the function call of the function in which they are defined. The same goes for static and globally defined arrays – they compiler has to allocate space for them in static/global memory areas.
As the compiler allocates the space it seems reasonable that it has to know the size of the memory you are asking it to allocate (even if you did not realise you were asking). You will _not_ get around the compiler by hiding values that are only known at runtime up in const-clothing! If the compiler cannot work out the size of your array then it will fail the compilation.
However, languages like C and C++ have another sort of memory called dynamic memory, in which objects can be allocated and deallocated from the free store (or heap) at runtime.
I shall give a very quick 1 minute introduction to the subject as far as C++ is concerned. I expect your teacher may also cover this area at some point.
In C++ the new and delete operators are used to create and destroy objects dynamically at runtime (note: often (but not always) static implies compile time and dynamic implies runtime). These operators come in 2 forms: The plain scalar forms new and delete for use with single object allocation such as an int or single class instance. The second form is the array form new  and delete  for creating and destroying arrays of objects at runtime. Both forms of new return a pointer – either to the object or to the first object. Likewise both forms of delete take a pointer returned by the _matching_ form of new. The usual standard forms of new also throw a std::bad_alloc exception if they cannot obtain the resources (i.e. memory) to complete the request.
The rules are:
1/ if your program creates an object or objects at runtime then it is responsible for ensuring such objects are destroyed when no longer required so that any resources held by such objects are released (including, but not limited to, the memory they occupy).
2/ if you create with new then destroy with delete. If you create with new  then destroy with delete .
So in your case you could create an array of n ints at runtime like so:
unsigned int n(0);
std::cout << "Enter size of array (1 <= n <= 100): ";
std::cin >> n;
// Check n is between 1 and 100 inclusive...
// Create an array of n ints
int * n_ints = new int[n];
// Do stuff with the array
n_ints = 1;
n_ints[n-1] = n;
// Done with array so delete using _correct_ array form of delete
delete  n_ints;
Now this is obviously fragile – if you forget to delete dynamically created object then resources can be lost. If you use the wrong form of delete, forget to use delete  for an array and just use the plain scalar form of delete for example, then again things will not be done correctly (in the example not all objects will be properly destroyed).
This is why most of us try not to use such techniques on an ad-hoc basis and if we do then we wrap their usage in smart resource management types such as smart pointer types that help manage the cleanup automatically.
One good example is that I rarely use dynamically allocated built in arrays these days. I prefer to use std::vector instead as it adds many useful features such as automatically growing as needed and when an instance is destroyed it destroys its element objects. See for example "The C++ Standard Library A Tutorial and Reference" by Nicolai M. Josuttis for more on such matters.
See "Effective C++" especially the "Memory Management" chapter for more on dynamic memory usage and abusage.