answersLogoWhite

0


Best Answer

It depends, especially if you are going to use C++.

If you allocated the variable using the malloc call or any of its derivatives you must use the corresponding 'free' subroutine call to delete them.

If you use the more modern C++ 'new' operator, then use the 'delete' operator to remove the memory dynamically in the program.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you delete variables that are dynamically allocated?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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 difference between stack and heap memory in C?

stack is memory allocated for temporary variables used by subroutinesheap is memory allocated for long term data structures (e.g. linked lists, trees) that are likely to change sizeBoth are forms of dynamically allocated memory (i.e. allocated/deallocated at runtime as needed), but the allocation/deallocation method and their place in physical/virtual memory are differentStatically allocated memory (i.e. allocated at compile/link time) is used for variables and data structures that must exist as long as the program is running and cannot change in size while the program is running.


Is declaration of variables allocated memory?

Constants, static variables and global variables are allocated in the program's data segment at compile time. Local variables are allocated on the stack at runtime. Variables cannot be allocated on the heap, you must use a constant, static variable, global variable or local variable to store the start address of a dynamic memory allocation. The variable must be a raw pointer or a reference handle (a smart pointer).


What is a pointer variable in C?

Pointer variables point to data variables. They are mostly used to point to dynamically allocated data variables, but can actually point to anything (e.g. statically allocated variables, array elements, anywhere inside a variable, program machine code, I/O device descriptors, nonexistent memory). Misuse of pointer variables, either unintentionally or intentionally, is a major cause of nearly impossible to debug software problems in programs written in C (and C++).


Is memory allocated at runtime for the static variables?

No. Static memory is allocated at compile time. Static variables are allocated within the program's data segment which is a physical part of the executable. When you load the executable into memory, the operating system sets aside enough memory for the entire executable and copies it, byte for byte, into that memory. So when the program is executed, the data segment is already allocated.

Related questions

Write an Algorithm to delete a last element from the array?

// Assuming you dynamically allocated this array using "new"... delete array[arraysize - 1]; arraysize--;


What is the code to delete variables that are dynamically allocated?

In C++, dynamically allocated variables are best encapsulated in a class. In this way, the variable is automatically deleted when the object falls from scope. #include<iostream> class f () { int* p; public: f (const int i=0): p {new int{i}} {} f (const& f other): p {new int{*other.p;}} {} ~f() {delete p; } f& operator=(const f& other) { *p = *other.p; } f& operator= (const int i) {*p=i;} operator int () { return *p; } }; int main() { { f value = 42; // use value... } // f automatically falls from scope here, releasing the memory allocated to value.p }


What is smart pointer?

Smart pointers are C++ regular pointers except that they automatically delete the object pointed to at the appropriate time.Thus ensuring proper destruction of dynamically allocated objects.


Can you delete array?

If the array was allocated with new, then delete it with delete []. Otherwise, if it was allocated with malloc() then delete it with free. Otherwise, you cannot delete it because it was pre-allocated at link-load time by the compiler.


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 difference between stack and heap memory in C?

stack is memory allocated for temporary variables used by subroutinesheap is memory allocated for long term data structures (e.g. linked lists, trees) that are likely to change sizeBoth are forms of dynamically allocated memory (i.e. allocated/deallocated at runtime as needed), but the allocation/deallocation method and their place in physical/virtual memory are differentStatically allocated memory (i.e. allocated at compile/link time) is used for variables and data structures that must exist as long as the program is running and cannot change in size while the program is running.


Is declaration of variables allocated memory?

Constants, static variables and global variables are allocated in the program's data segment at compile time. Local variables are allocated on the stack at runtime. Variables cannot be allocated on the heap, you must use a constant, static variable, global variable or local variable to store the start address of a dynamic memory allocation. The variable must be a raw pointer or a reference handle (a smart pointer).


What do you mean by run time memory allocation?

When the allocation of memory to the program is done on need, during the execution of a program, it is called as the dynamic memory allocation. Memory is allocated from a free memory pool called as heap.The only way to access this dynamically allocated memory is through pointers. Dynamically allocated memory can be freed at run time and again added to heap.


Where dynamic variables are stored?

Dynamic variables are stored in a memory heap allocated to them at run time.


What is a pointer variable in C?

Pointer variables point to data variables. They are mostly used to point to dynamically allocated data variables, but can actually point to anything (e.g. statically allocated variables, array elements, anywhere inside a variable, program machine code, I/O device descriptors, nonexistent memory). Misuse of pointer variables, either unintentionally or intentionally, is a major cause of nearly impossible to debug software problems in programs written in C (and C++).


How much time does it take to delete an array value?

C compiler delete only those memory which is dynamically created. So we can't delete the value of array.


What is the maximum size of memory available for dynamic memory allocation?

The maximum memory that can be dynamically allocated depends on the size of the heap memory. Dynamic blocks of memory can be allocated in system heap until it is not full.