Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't built-in arrays safe?

Tags:

c++

arrays

The book C++ Primer, 5th edition by Stanley B. Lippman (ISBN 0-321-71411-3/978-0-321-71411-4) mentions:

An [std::]array is a safer, easier-to-use alternative to built-in arrays.

What's wrong with built-in arrays?

like image 976
omidh Avatar asked Oct 24 '15 15:10

omidh


People also ask

Is array size fixed in C++?

Array is a type consisting of a contiguously allocated nonempty sequence of objects with a particular element type. The number of those objects (the array size) never changes during the array lifetime.

What is an array in C++?

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store: string cars[4];

Can a function modify an array C++?

As we pass the pointer, we can directly modify the array inside the function. Consider the following program that computes the square of each element of the first five elements in the Fibonacci sequence to demonstrate the passing of an array to function.

How do you resize an array in C++?

Once an array has been allocated, there is no built-in mechanism for resizing it in the C++ programming language. Therefore, we can avoid this problem by dynamically generating a new array, copying over the contents, and then deleting the old array.


1 Answers

  1. A built-in array is a contiguous block of bytes, usually on the stack. You really have no decent way to keep useful information about the array, its boundaries or its state. std::array keeps this information.

  2. Built-in arrays are decayed into pointers when passed from/to functions. This may cause:

    • When passing a built-in array, you pass a raw pointer. A pointer doesn't keep any information about the size of the array. You will have to pass along the size of the array and thus uglify the code. std::array can be passed as reference, copy or move.

    • There is no way of returning a built-in array, you will eventually return a pointer to local variable if the array was declared in that function scope. std::array can be returned safely, because it's an object and its lifetime is managed automatically.

  3. You can't really do useful stuff on built-in arrays such as assigning, moving or copying them. You'll end writing a customized function for each built-in array (possibly using templates). std::array can be assigned.

  4. By accessing an element which is out of the array boundaries, you are triggering undefined behaviour. std::array::at will preform boundary checking and throw a regular C++ exception if the check fails.

  5. Better readability: built in arrays involves pointers arithmetic. std::array implements useful functions like front, back, begin and end to avoid that.

Let's say I want to sort a built-in array, the code could look like:

int arr[7] = {/*...*/}; std::sort(arr, arr+7); 

This is not the most robust code ever. By changing 7 to a different number, the code breaks.

With std::array:

std::array<int,7> arr{/*...*/}; std::sort(arr.begin(), arr.end()); 

The code is much more robust and flexible.

Just to make things clear, built-in arrays can sometimes be easier. For example, many Windows as well as UNIX API functions/syscalls require some (small) buffers to fill with data. I wouldn't go with the overhead of std::array instead of a simple char[MAX_PATH] that I may be using.

like image 110
David Haim Avatar answered Oct 06 '22 01:10

David Haim