answersLogoWhite

0


Best Answer

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).

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

THE DECLARATION SECTION CONTAINS: int num[4]; where int is the type of input that array accepts, num is the name of the array and [4] means array reserves 4 blocks for storage of data.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Static arrays are allocated at compile time. Dynamic arrays are allocated at runtime at the point of instantiation, by calling malloc.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

It will form a memory blocks in seqence order[][][][] like this

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When is memory allocated when declaring an array in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Delete memory release operator in c plus plus?

You can't physically delete memory, you can only delete a pointer to allocated memory, which subsequently releases the memory back to the system. The operator is delete, passing the pointer as the operand. If the pointer points to an array, then you must also use the index operator [] in front of the pointer name.int main(){// pointer to an int type with value 100int* ptr_int = new int(100);// ... use pointer ...// release the integerdelete ptr_int;// pointer to an array 100 int types (with undefined values)int* ptr_int_array = new int[100];// ... use array ...// release the arraydelete [] ptr_int_array;return(0);}


What is contiguous memory in c plus plus?

Contiguous memory refers to a single block of consecutive memory addresses. All data types larger than a char require contiguous memory addresses. For any given data type T, sizeof (T) tells us how many bytes of contiguous memory will be allocated to an object of that type: std::cout << "sizeof (char) == " << sizeof (char) << std::endl; std::cout << "sizeof (int) == " << sizeof (int) << std::endl; std::cout << "sizeof (double) == " << sizeof (double) << std::endl; struct X {/* ... */}; std::cout << "sizeof (X) == " << sizeof (X) << std::endl; When we speak of contiguous memory, we don't usually refer to the number of bytes allocated to a given type; it can be taken as read that those bytes will be allocated contiguously such that the low-order byte refers to the whole object, regardless of its length. Typically we use the term contiguous memory when referring to an array of objects. All objects in an array (the array elements) are exactly the same length (in bytes) and because they are allocated contiguously it is trivial to calculate the offset address of any one element relative to any other element in the same array. This is precisely how the array suffix operator works using only a zero-based index; the operator is nothing more than a convenient method of implementing pointer arithmetic. The upshot is that all arrays permit constant-time random access to any element in the array. Arrays are dense data structures. That is, there is no additional memory required to maintain the structure. The only information we need to keep track of is the start address and the number of elements. However, the downside of contiguous memory allocations is that whenever we wish to increase the amount of memory allocated we often have to move the entire allocation to new memory. The larger the allocation the more costly this becomes. Moreover, inserting new data means we must move elements to make room. This is why variable-length arrays typically reserve additional memory for moderate expansion while new elements are always pushed onto the end of the array rather than inserted in the middle. Linked-lists are non-contiguous data structures which make use of additional memory to maintain links between the elements. As such, the elements need not move once allocated. If we want to change the element sequence or insert a new element into the sequence we simply update the affected links; the elements themselves remain wherever they were originally allocated. Some data structures make use of both contiguous and non-contiguous allocations. A deque (a double-ended queue, pronounced deck) is a typical example because it is usually implemented as a linked-list of separate arrays. Each array is contiguous but the list of arrays is not necessarily contiguous.


What is the lowest subscript of an array in c plus plus?

The lowest subscript of an array in C, or C++ is 0.


What do you mean by free store in c plus plus?

The free store in any language refers to the heap. The three main areas of memory that all C++ programs use are the heap, the call stack and static memory. Static memory is allocated at compile time, is fixed-length and caters for all static variables, global variables and constant variables. Call stacks are also fixed-length and are allocated to threads of execution as they are instantiated (each thread has its own stack). The free store or heap is essentially all remaining memory accessible to our program. To use the heap we must request memory from the system as it is required and release it when we are finished with it.


Can you help me with the C plus plus code of the program which has 10 index of array it adds 5 into every even elements of the array and then it subtracts 10 into the odd elements of the array?

int array[10] = {...}; for (int i = 0; i < 10; ++i) { if (i % 2 == 0) array[i] += 5; else array[i] -= 10; }

Related questions

What is syntax in c plus plus for declaring a variable?

type variable {[optional array size]} {= optional initializer};


When does an array behave like a pointer in C plus plus -- provide sample code to support each case?

An array never behaves like a pointer.Understand that a pointer is a variable -- no different to any other variable, other than its type. like any other variable, it is allocated its own memory (4 bytes in a 32-bit system). Those 4 bytes can store any 32-bit value, from 0x00000000 to 0xFFFFFFFF, but because it is declared to be a pointer, that value actually represents a memory location, somewhere within the 4GB address space. 0x00000000 is a reserved memory location -- what we refer to as NULL.An array is not the same as a pointer in any sense. The array name refers to the actual memory (the starting address) where the array resides. But the array name is not a variable -- unlike a pointer, it is not separate from the memory it refers to -- it is merely an alias that refers to the memory allocated to the array itself.That said, pointers and arrays can appear to be the same. For instance, the strlen() function expects you to pass a const char pointer, and yet you can pass an array name instead and it works just fine:char cArray[12];char * p = cArray;strlen( p ); // Pass a pointerstrlen( &cArray[0] ); // Pass pointer to array element via AddressOf operatorstrlen( cArray ); // Pass an array nameAll three of these functions work exactly the same so it would be easy to assume the array is a type of pointer. But it is not. To understand what's really going on here you have to understand how pointers and arrays are actually passed to function.When you pass a pointer to a function, you don't pass the pointer itself, you pass the memory address stored in the memory allocated to the pointer. In other words, pointers are passed by value, not by reference. Similarly, when you pass an array name, you pass the starting address of the array.That's all there is to it! The function's parameter, a pointer, is allocated its own memory. It is a temporary variable, local to the function. All it expects is a memory location so whether you pass a pointer or an array name, that's exactly what it receives.


How c plus plus objects are allocated memory?

With the new operator.myclass myclasspointer = new myclass;...use the classdelete myclasspointer;


Dynamic memory allocation in c plus plus?

The following function demonstrates a dynamic array of integers. The array is dynamically allocated a random number of elements, from 1 to 100, with pointer p pointing at the start address (&p[0]). Note that the number of elements needn't be random, but unlike a static array which has a constant size and can therefore be allocated in the data segment by the compiler, the number of elements in a dynamic array is not known in advance and must therefore be allocated on the free store at runtime. The number of elements is variable. #include <iostream> #include <time.h> int main() { srand(( unsigned ) time( NULL )); int elements = rand() % 100 + 1; int * p = new int[elements]; // do something with array... // p[0] will return the first element // p[elements-1] returns the last element. delete [] p; // release memory. p = NULL; return( 0 ); }


Delete memory release operator in c plus plus?

You can't physically delete memory, you can only delete a pointer to allocated memory, which subsequently releases the memory back to the system. The operator is delete, passing the pointer as the operand. If the pointer points to an array, then you must also use the index operator [] in front of the pointer name.int main(){// pointer to an int type with value 100int* ptr_int = new int(100);// ... use pointer ...// release the integerdelete ptr_int;// pointer to an array 100 int types (with undefined values)int* ptr_int_array = new int[100];// ... use array ...// release the arraydelete [] ptr_int_array;return(0);}


What is the memory management operator in c plus plus?

There is no memory management operator in C++ -- it is an unmanaged language. You use the C++ new operator to allocate memory, and use the C++ delete operator to release previously allocated memory.


C plus plus uses dynamic memory management?

No, C++ does not use dynamic memory management. The programmer is entirely responsible for releasing dynamic memory when it is no longer required. When static objects fall from scope, their destructors are called automatically, but there is no automatic garbage collection for dynamic objects. Allocated memory remains allocated until the programmer manually releases it, or the thread that owns the memory is terminated.


What are new and delete operators in c plus plus?

New and Delete are the memory management operators in c++,like c language we use malloc() and calloc() functions to allocate memory and free() functiong to release the memory similarily we use new to allocate memory in C++ and Delete to release the allocated memory....


What is contiguous memory in c plus plus?

Contiguous memory refers to a single block of consecutive memory addresses. All data types larger than a char require contiguous memory addresses. For any given data type T, sizeof (T) tells us how many bytes of contiguous memory will be allocated to an object of that type: std::cout << "sizeof (char) == " << sizeof (char) << std::endl; std::cout << "sizeof (int) == " << sizeof (int) << std::endl; std::cout << "sizeof (double) == " << sizeof (double) << std::endl; struct X {/* ... */}; std::cout << "sizeof (X) == " << sizeof (X) << std::endl; When we speak of contiguous memory, we don't usually refer to the number of bytes allocated to a given type; it can be taken as read that those bytes will be allocated contiguously such that the low-order byte refers to the whole object, regardless of its length. Typically we use the term contiguous memory when referring to an array of objects. All objects in an array (the array elements) are exactly the same length (in bytes) and because they are allocated contiguously it is trivial to calculate the offset address of any one element relative to any other element in the same array. This is precisely how the array suffix operator works using only a zero-based index; the operator is nothing more than a convenient method of implementing pointer arithmetic. The upshot is that all arrays permit constant-time random access to any element in the array. Arrays are dense data structures. That is, there is no additional memory required to maintain the structure. The only information we need to keep track of is the start address and the number of elements. However, the downside of contiguous memory allocations is that whenever we wish to increase the amount of memory allocated we often have to move the entire allocation to new memory. The larger the allocation the more costly this becomes. Moreover, inserting new data means we must move elements to make room. This is why variable-length arrays typically reserve additional memory for moderate expansion while new elements are always pushed onto the end of the array rather than inserted in the middle. Linked-lists are non-contiguous data structures which make use of additional memory to maintain links between the elements. As such, the elements need not move once allocated. If we want to change the element sequence or insert a new element into the sequence we simply update the affected links; the elements themselves remain wherever they were originally allocated. Some data structures make use of both contiguous and non-contiguous allocations. A deque (a double-ended queue, pronounced deck) is a typical example because it is usually implemented as a linked-list of separate arrays. Each array is contiguous but the list of arrays is not necessarily contiguous.


What is an array of class objects.how the array of class of class objects is defined in c plus plus?

An array of class objects is just a set of class objects arranged linearly in memory. It is no different than an array of elementary objects. You define it the same way. class myClass { ... }; myClass arrayOfMyClass[100]; // creates 100 objects and fires the constructor 100 times


When you call new will it internally call delete in c plus plus?

No. Calling new returns a pointer to allocated memory. If you re-use a pointer to store the return value, then you must release the memory that it previously pointed at, either by deleting the pointer, or by maintaining a separate pointer to the original memory. Calling new will not release the current memory for you.


What happens when class libraries leak memory in c plus plus?

A memory leak is when allocated memory that is no longer needed is not deallocated. Eventually, the memory pool is unable to satisfy an allocation request, and the program fails. A memory leak is a programming bug. When class libraries leak memory, they need to be fixed, just like any other piece of code that has bugs. If they came from a vendor, then that vendor needs to fix them.