answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Are arrays in C created on the stack or the heap?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is meant by heap in c or cpp?

If you see the word "heap" in the context of C/C++ programming, it is probably referring to one of two ideas. First, if it is written as "the heap", it is probably referring to dynamically allocated memory. We conceptualize memory as either being on "the stack" or "the heap" in main memory. Memory allocation from the heap happens when a call to malloc (or similar functions) are called in C, or when the "new" operator is used in C++. This is in contrast to statically allocated memory, which comes from the load module and is known at compile-time, or from the "stack" which is used at run-time to allocate local scope, or automatic, memory. Another usage of the word heap is a certain data structure called a heap. It is a very common data structure for priority queues and is crucial to the famous HeapSort algorithm. You can easily find more information on this data structure e.g. by searching for HeapSort.


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.


Can an array be passed from a function to the calling portion of the program via a return statement?

It depends on the language, but in most cases yes, you can return arrays to callers via the return statement. In C, arrays can be returned from a function by reference (that is, by pointer) but never by value (arrays cannot be copied automatically). The array must be allocated on the heap, never on the stack (you cannot return references to local variables). However, beware that returning arrays by reference is unsafe because there's no way to determine the upper bound of the array. This is why many C library functions return multiple values through output parameters and use the return value to indicate error conditions. One way to return both the array and its size via a return statement is by returning a structure: struct array_info { void* array_ptr; /* pointer to first element in array */ int size; /* number of elements in the array */ }; Obviously you must cast the array_ptr member to the appropriate type before dereferencing any of the array elements. In C++, C-style arrays work just as they do in C. However, the preferred method is to use a vector rather than a C-style array. A vector is a class template that encapsulates a C-style array with size and reserve, along with a rich set of useful functions (as with all templates, you don't pay for what you don't use). Vectors can be returned from functions both by value and by reference. Vectors also support move semantics making it possible to return vectors allocated on the stack as well as on the heap. C++ also supports an array class template. This is specifically for fixed-size arrays but is otherwise similar to a vector.


C memory model?

Memory Organization for a process -------------------------------------------- CODE SEGMENT: contains executable code DATASEGMENT:contains static and global variable HEAP SEGMENT:Dynamically allocated variables will be in heap STACK SEGMENT:For local or automatic variables PREM G premgnath@gmail.com


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.

Related questions

What is meant by heap in c or cpp?

If you see the word "heap" in the context of C/C++ programming, it is probably referring to one of two ideas. First, if it is written as "the heap", it is probably referring to dynamically allocated memory. We conceptualize memory as either being on "the stack" or "the heap" in main memory. Memory allocation from the heap happens when a call to malloc (or similar functions) are called in C, or when the "new" operator is used in C++. This is in contrast to statically allocated memory, which comes from the load module and is known at compile-time, or from the "stack" which is used at run-time to allocate local scope, or automatic, memory. Another usage of the word heap is a certain data structure called a heap. It is a very common data structure for priority queues and is crucial to the famous HeapSort algorithm. You can easily find more information on this data structure e.g. by searching for HeapSort.


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 the required syntax for creating C arrays?

The required syntax for creating C arrays include the brackets, array size, variety length arrays, codes like std:vector, classPTR, and many more to create C arrays.


Write a program of binary heap in c or c language?

to implement operations on binary heap in c


Can an array be passed from a function to the calling portion of the program via a return statement?

It depends on the language, but in most cases yes, you can return arrays to callers via the return statement. In C, arrays can be returned from a function by reference (that is, by pointer) but never by value (arrays cannot be copied automatically). The array must be allocated on the heap, never on the stack (you cannot return references to local variables). However, beware that returning arrays by reference is unsafe because there's no way to determine the upper bound of the array. This is why many C library functions return multiple values through output parameters and use the return value to indicate error conditions. One way to return both the array and its size via a return statement is by returning a structure: struct array_info { void* array_ptr; /* pointer to first element in array */ int size; /* number of elements in the array */ }; Obviously you must cast the array_ptr member to the appropriate type before dereferencing any of the array elements. In C++, C-style arrays work just as they do in C. However, the preferred method is to use a vector rather than a C-style array. A vector is a class template that encapsulates a C-style array with size and reserve, along with a rich set of useful functions (as with all templates, you don't pay for what you don't use). Vectors can be returned from functions both by value and by reference. Vectors also support move semantics making it possible to return vectors allocated on the stack as well as on the heap. C++ also supports an array class template. This is specifically for fixed-size arrays but is otherwise similar to a vector.


C memory model?

Memory Organization for a process -------------------------------------------- CODE SEGMENT: contains executable code DATASEGMENT:contains static and global variable HEAP SEGMENT:Dynamically allocated variables will be in heap STACK SEGMENT:For local or automatic variables PREM G premgnath@gmail.com


Which components of program state are shared across threads in a multi-threaded process?

a. Register values b. Heap memory c. Global variables d. Stack memory


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.


What is an arrays in c?

array is collection of many data


How do you refresh stack using library functions in C programming?

What do you mean by stack-refreshing? Anyway, there are no stack handling functions in the standard C library.


What is the purpose of using arrays in C language?

The purpose of using arrays in C is to store multiple values in one variable. Then you can make programs that use arrays like lists, printing values from multiple arrays into one line. It take memory in continues block then we can know memory location easily. We can retrieve data quickly.


What are the differences between structures and arrays?

Arrays are collections of repeated data items. Structures are complex data items made up of other data items, including, potentially, other structures and arrays. You can, of course, also have arrays of structures. Array items can be accessed by using its subscript whereas structure items can be accessed using its dot or "arrow" operator in C, C++, C#, Java, and JavaScript.