Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should arrays be used in C++?

Since std::list and std::vector exist, is there a reason to use traditional C arrays in C++, or should they be avoided, just like malloc?

like image 609
Andreas Avatar asked May 23 '12 10:05

Andreas


People also ask

When should we use array in C?

An array is a variable that can store multiple values. For example, if you want to store 100 integers, you can create an array for it.

What is the disadvantage of array in C?

Shifting is required for insertion or deletion of elements in an array. An array doesn't check boundaries: In C language, we cannot check, if the values entered in an array are exceeding the size of that array or not. Data that is entered with the subscript, exceeds the array size and will be placed outside the array.

Should I free arrays in C?

In short, following are the main points regarding it: If the array is declared statically, then we do not need to delete an array since it gets deleted by the end of the program/ block in which it was declared. If the array is declared dynamically, we need to free the memory allocated to it using the free() function.

Can we use array in C?

C supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array. You can pass to the function a pointer to an array by specifying the array's name without an index. C allows a function to return an array.


1 Answers

In C++11 where std::array is available, the answer is "yes, arrays should be avoided". Prior to C++11, you may need to use C arrays to allocate arrays in the automatic storage (i.e. on the stack).

like image 175
Sergey Kalinichenko Avatar answered Oct 14 '22 08:10

Sergey Kalinichenko