If a Windows DOS or Mac platform is used the potential for hanging the machine

If a windows dos or mac platform is used the

This preview shows page 2 - 4 out of 9 pages.

If a Windows, DOS, or Mac platform is used, the potential for hanging the machine byoverwriting a return address for a function exists.3. Arrays in FunctionsIndexed Variables as Function Arguments. Since an indexed array variable is just another variable of the array's base type, there is nothing special about sending it to any function that accepts such argument, except that it looks unfamiliar. It is important to understand that the whole array is not being sent to the function, just the elementof the array indicated by the index. PITFALL: Declaring an Array with a Non-Constant Size.If you wanted to have an array whose size you could set at runtime, you might write the following code. Unfortunately, this is illegal, though a few compilers (including g++) allow it.//To test behavior when array bounds are set, used, and violated#include <iostream>using namespace std;intmain(){using namespace std;intx[10] = {0, 1,2,3,4,5,6,7,8,9};intn;cin >> n;int y[n]; //Line 10 - Here we have (illegally) declared //an array of non-constant sizefor( inti = 0; i < n; i++)cout << (y[i] = x[i]) << " " ;cout<<endl;return0;}Borland C++ 5.4 gives the message “a constant expression is required” and aborts compilation.
Background image
The ARM says that the index expression must be a constantexpression. The Standard says that a constant expression can involve only literals (such as 937, true, or 1.23), enumerators (enumidentifiers) and constvalues of integral types initialized with constant expressions, sizeof expressions. We violate the rule that we must have constintegral values initialized by constexpressions.The bottom line is that array size must be knownat compiletime. The alternative is to dynamically allocate space on the heap. That is treated later.Entire Arrays as Function ArgumentsWhen passing arrays, all that is sent to the function is the address of (or pointer to) the first array element. All the function knows is the type as specified in the function declaration and the starting address of the array. The array is not self-defining, unlikeevery other type in C++. When an array is passed to a function, the size of the array is not available to the called function. The consequence is that passing an array parameter is not pass-by-value, since changing the array element in the function changes the argument array element. It isnot pass-by-reference, since the called function doesn't know the size of the array. Sowe call this parameter an "array parameter."Precisely, array parameters pass the address of the first element of the array, by value. Having the address enables the called function to change the argument by indexing the formal parameter and making an assignment. There are only two places in C++ where the empty array index operator may be used: one, where an array is declared with initializers, and in an array parameter fora function:1intx[] = { 1, 2, 3, 4, 5}; //array has 5 int elements.
Background image
Image of page 4

You've reached the end of your free preview.

Want to read all 9 pages?

  • Fall '15
  • Marinolent

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture