Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • Use of asterisk in C++
    I have notice over many different websites when trying to find the method of finding the size of an array and I notice there has been many records saying
    int arr[100];
    
    and
    int *arr=new int[100];
    
    are different My question is what is the difference?
    Berwinish posted this question on 1/3/13 | cpp, pointer, array
    Answers
  • +
  • 2
  • -
  • With
    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.
  • +
  • 0
  • -
  • 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)
  • +
  • 0
  • -
  • The first one defines an array which will be in the stack, and the latter is creating new array which will be in heap space.
    Log in to write an answer.