Programming Questions
int arr[100];and
int *arr=new int[100];are different My question is what is the difference?
int arr[100]you define an array on the stack. With
int *arr=new int[100]you allocate space on the heap for an array of 100 int and then you reference it with a pointer in the stack.
tooNoob
answered on 01/06/14
int arr[100];This is a normal array. Size of normal arrays can be obtained by using 'sizeof()' operator. Required memory space for this array is determined by the compiler at compiling time and size of the array cannot be arranged at run-time.
int *arr=new int[100];In this, 'arr' is a pointer. Duty of pointers is to hold a memory address of another variable. A pointer can aquire full control of another variable. In above codes. The pointer 'arr' is pointed to an integer array with 100 members. The array is created at run time not compiling time. There may be occations that the required memory space for a particular data type cannot be decided earlier, in such a cases run-time memory allocation is used (Dynamic memory allocation)
Harun123
answered on 03/05/15
Puciek
answered on 01/05/14