answersLogoWhite

0

You call free...

int *a = (int*) malloc (100 * sizeof (int)); /* allocate 100 ints */

... check to make sure a != NULL

... use a[0] through a[99] as desired

free (a); /* release the memory */

In C++, you can use malloc/free, but it is better to use new/delete...

int *a = new int[100]; // allocate 100 ints

... check to make sure a != NULL

... use a[0] through a[99] as desired

delete [] a; // release the memory

User Avatar

Wiki User

14y ago

What else can I help you with?

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--;


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.


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.


How do you delete variables that are dynamically allocated?

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.


How do you use character in c plus plus?

If you are referring to the character object 'char,' then here are a couple of uses:To create an object, use this:char object = 'a';To create an array of chars, use this:char array[10];To dynamically allocate an array of chars, use this:char array = new char[10];(Don't forget to delete the object with 'delete [] array')


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.


How can I fix Stack overflow at line 156?

A stack overflow is usually the cause of an array that is too small to be able to hold the intended data. To fix a stack overflow, the array must be locally declared (this means not dynamically allocated off of the heap) and then you must change the amount of "slots" in the array to something that is big enough to hold your data.


How delete an array of object within the object in C?

First locate the position of an array by search after than use a delete function to delete an array


How do you get rid of an array in Excel?

Select the entire array and press the Delete key. There are usually only problems if you want to delete some, but not all of the cells in the array.


Can you change the base address of an array during run time?

If the compiler allocated the array at compile time, or if the array was automatically allocated as a local variable, then no, you cannot change its base address at run time. If you allocated the array at run time from the heap, you can change its base address by allocating a new array, copying the old elements from old to new and deleting the old array.


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 ); }


Will an array element be deleted when you retrieve it from the array?

You cannot delete from an array.