answersLogoWhite

0

How memory is allocated in case of arrays?

Updated: 8/19/2019
User Avatar

Wiki User

13y ago

Best Answer

1 byte for 1 index item in 16-bit operating system

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How memory is allocated in case of arrays?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Where the Memory is allocated for a variable in a program?

if a variable is of value type memory is allocated on stack memory.. if it is of reference type,memory is allocated on heap memory..


Dynamically allocated array waste storage and time?

It's actually not true. In order to make a good program which can work with big arrays you have to use dynamic arrays because you can cleam memory used by dymanic arrays any time. For static arrays is not true, memery which was reserved for static arrays will be available for other applications only when you finish working with your application (which is working with static arrays).


Why memory is divided into initialized and uninitialized areas?

The program's data segment. This area of memory is allocated by the linker and is used to store the program's global variables, static variables, static arrays and constants. Constants are always initialised, as are static variables, but global variables and static arrays need not be initialised.


Are arrays in C created on the stack or the heap?

That depends on where you define them. Arrays defined inside functions are declared on the stack (like other variables defined in functions). Arrays defined outside of any function, or using the static keyword inside a function are allocated in the static data area of the program. Other arrays may be allocated using malloc() (or "new" in C++); these are allocated on the heap.


How is memory allocated to arrays?

It depends where the array is declared and whether or not it is fixed-length or variable-length. The only thing all arrays have in common is that the memory is allocated contiguously. Fixed length arrays are arrays where the length is known at compile time while variable length arrays have unknown length and are allocated at runtime. Fixed-length arrays can be allocated statically, on the call stack (local memory) or on the heap (the free store) while variable length arrays must always be allocated on heap. Fixed-length arrays should use a constant variable to refer to the array length consistently while variable-length arrays should use a (mutable) variable. Arrays allocated on the heap must also use a pointer variable to keep track of the start address. The array (or pointer) and its length should always be declared in the same scope. Storing both in a data structure makes it easier to keep track of the array and its length. Static arrays are always fixed length and are allocated at load-time, before entry to the main function (even if declared in the main function or in another function entirely). The memory remains allocated until the program terminates. If there is insufficient memory available, the program itself will fail to load. It is recommended you use static memory sparingly and avoid the use of globals whenever possible. Local arrays are also fixed length but are allocated on the call stack at runtime as and when they come into scope. The memory is released automatically when the array falls from scope. The stack is fixed-length and therefore has limited capacity, so local arrays should be used sparingly. Variable length arrays and fixed-length arrays allocated on the heap are allocated and released manually (programmatically) at runtime. It is the programmer's responsibility to keep track of the array's start address. If there is insufficient memory available, it is the programmer's responsibility to handle the exception. Examples: // Fixed-length array at global scope // Note that all globals are implicitly static: const int a=10; int A[a]; // static array of 10 integers // Fixed-length array at file scope // Note that all file scope variables are explicitly static: static const int b=20; static int B[b]; // static array of 20 integers // Fixed-length arrays at function scope: void f () { const int size=100; static int C[size]; // static array of 100 integers (allocated at load-time) int D[size]; // local array of 100 integers (allocated when f comes into scope) int* E = malloc (size * sizeof (int)); // heap array of 100 integers // Note: if E is NULL, the allocation failed! // ...use arrays... free (E); // release the allocation before E falls from scope! E = NULL; // good housekeeping! } // D is released here // C, D and E will fall from scope here, however C is not released // Variable-length arrays at function scope: void g (const int n) { int* F = malloc (var * sizeof (int)); // heap array of n integers // ...use array... free (F); F = NULL; } // F falls from scope here.


What is sparse array?

A sparse array (or sparse matrix) is an array where only a few elements are actually allocated storage space in memory, the unallocated elements are forced to zero for calculation purposes. Such arrays usually use pointers to reference the storage for the allocated elements.


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.


In what are various cells of memory allocated consecutively?

Contiguous memory address are allocated to an array or vector.


When is memory allocated when declaring an array in C plus plus?

It depends how the array is declared (fixed size or variable size) and where it is declared (global scope or local scope). If the array is declared in global scope (outside a function) and is fixed size, it will be allocated in static memory. If it is variable size, the pointer is stored in static memory while the array itself is allocated on the heap. The pointer in static memory points to the start address of the array in heap memory. If the array is declared in local scope (inside a function) and is fixed size, it will be allocated on the stack in whichever thread the function was called. If it is variable size, the local pointer is stored on the stack while the array is allocated on the heap. The pointer will fall from scope when the function returns so the array must not be allowed to outlive the function in which the pointer is declared. If the array must outlive the function that allocates the array, the pointer must be declared at a higher scope in the call stack and must be passed by reference to or returned by value from the function that allocates the array. If you provide your own memory manager, however, an array may be allocated wherever the memory manager's memory pool is allocated, be it in static memory, the stack or the heap. A memory manager essentially allocates an array of bytes which you can then utilise as you see fit (the array of bytes will be allocated as per the previous description for arrays in general).


How system knows what range of memory it has allocated while using free?

use free() how does the system know what range of memory it has allocated use free() how does the system know what range of memory it has allocated


Difference between dynamic and static memory allocation?

Static memory allocation is memory allocated on the "stack" and cannot be resized after the initial allocation, while dynamic memory allocation is memory allocated in the "heap", and can be dynamically expanded and shrunk as necessary.


What is the limitation of the linear array?

The main limitations of linear arrays are 1. The prior knowledge of number of elements in the linear array is necessary 2. These are static structures. Static in the sense that memory is allocated at compilation time their memory used by them cannot be reduced or extended. 3. Since the elements of these arrays are stored in the these arrays are time consuming this is because of moving down or up to create a space of new element or to occupy the space vacated by the deleted element.