answersLogoWhite

0


Best Answer

An array is nothing more than a block of contiguous memory addresses divided into one or more units of equal length. We call these units elements. As a result, all elements of an array must be allocated contiguously.

Other than padding for memory alignment purposes, an array offers the most compact storage for multiple elements of the same type. And because the elements are allocated contiguously, it is trivial to traverse the array from one element to the next using nothing more than a pointer variable of the same type as the array. Each increment or decrement of the pointer increases or decreases the address by 1 element. The array suffix operator [] does the same thing, except the index is a zero-based offset from the start of the array. The first element is index 0 because it is 0 elements from the start of the array while the nth element is found at index n-1.

When we create an array we have the choice of storing the objects themselves in the array or storing a pointer to the objects. With the latter, the objects need not be allocated contiguously, however we consume more memory because the array of pointers consumes additional memory that we wouldn't need to consume were all the objects allocated in the array itself. We also have the additional cost of dereferencing those pointers in order to access the objects being referred to. However, arrays of pointers are advantageous when the objects are larger than a pointer (in bytes) or are of polymorphic type. Copying or moving a large object in memory is more expensive than copying or moving a pointer, so if we need to sort an array of large objects, an array of pointers is generally more efficient despite the indirection. But with polymorphic types we have to use pointers because polymorphic types are not guaranteed to be the same length so we must store a pointer to the common base type if we wish to retain polymorphic behaviour.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: The elements in an array must be of the same basic data type but why must they be stored in contiguous memory?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are the different types of array explain with examples?

There are two main types of array:1) Single dimensional array: These are arrays for which only one index is required to access the element stored in an array. They are stored in a contiguous memory location.Eg:int arr[10];//declarationarr[7] = 7;//initialization2) Multidimensional array: These are arrays for which more than one index is required to access the element stored in a specific location in the array. These are stored in a contiguous memory location row-by-row.Eg:int arr[5][5];//two dimensional arrayint arr[5][5][5];//three dimensional array


What is contiguous storage allocation?

Contiguous means to share an edge or boundary, touching, adjacent, neighbouring and so on. Thus contiguous storage allocation is any allocation that consumes two or more contiguous storage elements. In the case of contiguous memory allocation, this means two or more contiguous memory addresses are allocated. A one-dimensional array is an example of a contiguous memory allocation, where one array element (a data type) is immediately followed by the next.


How are elements of an array stored in memory?

Computer memory is linear so a one dimensional array can be mapped on to the memory cells in rather straight forward manner.To find the actual address of an element one needs to subtract one from the position of the desired entry and then add the result to the address of the the first cell in the sequence.Having said that therefore it is necessary to know the starting address of the space allocated to the array and the size of the each element, which is same for all the elements of an array.The the location of the Ith element would be B+I*S where B is the base address(Starting address of the array) and S is the size of each element of the array.


What is Arrays of Pointers?

An array of pointers is a contiguous block of memory that contains pointers to other memory locations. They essentially allow non-contiguous memory locations to be treated as if they were an actual array.


What is faster access the element in an array or in a list?

Array is always faster to read from disk/access any element in the array is quicker since elements in a array are stored in contiguous location in the memory, you need the pointer to the head or 0th element in the array and then it much quick to navigate to the next on index based. But you need to know INDEX of the element for best results List (say linked list) will be slower since not always elements are stored in contiguous location in the memory as well it involves a function call which is can be assembler/cpu expensive. However getting an individual object from an array is faster if you know the index of the object. Walking through a linked list is faster than walking through an array, if you use a non-recursive algorithm. --Vinay Solanki

Related questions

What is array in program?

An aggregate of elements of the same type that occupy contiguous memory.


What an array?

An array is an aggregate of data elements of the same type. Arrays are allocated in contiguous memory. An element of an array can be another array type, also known as a multi-dimensional array.


What are the different types of array explain with examples?

There are two main types of array:1) Single dimensional array: These are arrays for which only one index is required to access the element stored in an array. They are stored in a contiguous memory location.Eg:int arr[10];//declarationarr[7] = 7;//initialization2) Multidimensional array: These are arrays for which more than one index is required to access the element stored in a specific location in the array. These are stored in a contiguous memory location row-by-row.Eg:int arr[5][5];//two dimensional arrayint arr[5][5][5];//three dimensional array


What is contiguous storage?

Contiguous means to share an edge or boundary, touching, adjacent, neighbouring and so on. Thus contiguous storage allocation is any allocation that consumes two or more contiguous storage elements. In the case of contiguous memory allocation, this means two or more contiguous memory addresses are allocated. A one-dimensional array is an example of a contiguous memory allocation, where one array element (a data type) is immediately followed by the next.


What is contiguous storage allocation?

Contiguous means to share an edge or boundary, touching, adjacent, neighbouring and so on. Thus contiguous storage allocation is any allocation that consumes two or more contiguous storage elements. In the case of contiguous memory allocation, this means two or more contiguous memory addresses are allocated. A one-dimensional array is an example of a contiguous memory allocation, where one array element (a data type) is immediately followed by the next.


Why array is a primitive data structure?

An array is a primitive data structure because all elements are stored in contiguous memory. Unlike complex structures like lists and trees, elements are not stored in nodes that provide structural information. Structure is created from the elements themselves, insofar as each element is exactly the same length (in bytes) thus it is trivial to access an element in constant time from its zero-based index alone.


In what are various cells of memory allocated consecutively?

Contiguous memory address are allocated to an array or vector.


How are elements of an array stored in memory?

Computer memory is linear so a one dimensional array can be mapped on to the memory cells in rather straight forward manner.To find the actual address of an element one needs to subtract one from the position of the desired entry and then add the result to the address of the the first cell in the sequence.Having said that therefore it is necessary to know the starting address of the space allocated to the array and the size of the each element, which is same for all the elements of an array.The the location of the Ith element would be B+I*S where B is the base address(Starting address of the array) and S is the size of each element of the array.


What is Arrays of Pointers?

An array of pointers is a contiguous block of memory that contains pointers to other memory locations. They essentially allow non-contiguous memory locations to be treated as if they were an actual array.


What is faster access the element in an array or in a list?

Array is always faster to read from disk/access any element in the array is quicker since elements in a array are stored in contiguous location in the memory, you need the pointer to the head or 0th element in the array and then it much quick to navigate to the next on index based. But you need to know INDEX of the element for best results List (say linked list) will be slower since not always elements are stored in contiguous location in the memory as well it involves a function call which is can be assembler/cpu expensive. However getting an individual object from an array is faster if you know the index of the object. Walking through a linked list is faster than walking through an array, if you use a non-recursive algorithm. --Vinay Solanki


How do you represent a one dimensional array in memory?

A one-dimensional array is always represented as a single contiguous block of memory. The size of the allocation is determined by the array's type and the number of elements of that type. For instance, if you create an array of 10 elements where each element is 4 bytes in length, the total allocation will be 40 bytes. The array name is a reference to the start of the allocation and individual elements are accessed via an indexed offset from this reference, such that the first element is at offset 0, the next is at offset 1, and so on.


What is an array of 5 x 46?

An array is a contiguous memory allocation divided into one or more elements of equal size. A 5 x 46 array is an array of 5 elements where each element is another array of 46 elements. In other words it is an array of arrays. We can the array a two-dimensional array because it has 5 elements in one dimension (the rows) and 46 in the other dimension (the columns). If an individual column element is 4 bytes long, then each row element consumes 46 x 4 = 184 bytes of memory while the entire array consumes 5 x 184 = 920 bytes in total. We can also think of the entire array as being a one-dimensional array of 5 x 46 = 320 elements of 4 bytes each.