0

I'm a beginer. I'm confused about the difference between them. I have read a few answers and realized one of the difference is that the dynamic array can be deleted while the normal array can't. But are there any other differences? Such as their functions, sizes or whatsoever?

Well I have read such an example, and I don't see any difference if I replace the dynamic array {p= new (nothrow) int[i];} with a normal array {int p [i];}.

    #include <iostream>
    #include <new>
    using namespace std;
    int main ()
    {
      int i,n;
      int * p;
      cout << "How many numbers would you like to type? ";
      cin >> i;
      p= new (nothrow) int[i];
      if (p == 0)
      cout << "Error: memory could not be allocated";
      else
      {
        for (n=0; n<i; n++)
        {
          cout << "Enter number: ";
          cin >> p[n];
        }
        cout << "You have entered: ";
        for (n=0; n<i; n++)
        cout << p[n] << ", ";
        delete[] p;
      }
      return 0;
    }
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
user2488113
  • 9
  • 1
  • 2
  • i find a answer on stackoverflow, have a look http://stackoverflow.com/questions/2672085/c-static-array-vs-dynamic-array – user1808932 Jun 15 '13 at 04:19
  • @user1808932 he is asking more than that.... – pinkpanther Jun 15 '13 at 04:24
  • Best place for your soln:: www.google.com – Abhineet Jun 15 '13 at 04:38
  • The primary difference is that a properly functioning C++ compiler must accept code like `int *p = new int[n];`, but must reject `int p[n];` unless `n` is a constant. At a guess, you tested with g++, which does not function correctly in this regard (accepts `int p[n]`, even when `n` is not constant). In the end it's mostly irrelevant though -- you should really use `std::vector p(n);` instead (dynamic, safe, and accepted by all C++ compilers that work even sort of close to correctly). – Jerry Coffin Jun 15 '13 at 04:53
  • @Abhineet: Google doesn't work when you don't know what to look for. Recommend him a good book instead. – SigTerm Jun 15 '13 at 05:50
  • @SigTerm - Yeah I know that a book of concepts will help much better than Google but searching on Google will make him to stumble upon a lot of precise keywords to look upon and various other topics related. – Abhineet Jun 15 '13 at 06:06
  • @Abhineet: The reason I think it is better to recommend a book is because google today has plenty of noise. If you start googling basic stuff, you'll surely hit some tutorials, but you'll be overwhelmed with sea of information (some of which will be of dubious quality/origin). That's why I think it'll be better to start with a book that'll gradually introduce concepts and explain them. Unfortunately, because I started with C++ years ago(and already had experience in another language), I cannot recommend any good book myself. The book I used (Bjarne Stroustroup's) definitely isn't for everybody – SigTerm Jun 15 '13 at 06:31

2 Answers2

5

But are there any other differences?

Compile and run this, see what happens:

int main(int argc, char** argv){
    char buffer[1024*1024*64];
    buffer[0] = 0;
    return 0;
}

Explanation:

  1. Well, normal array is placed either on stack or within code segment (if it is global variable or static local variable). At least on windows/linux PCs. The stack has limited size (although you can change it using ulimit in linux and compiler settings on windows). So your array is too big for the stack, your program will instantly crash upon entering the function with that array ("segmentation fault" on linux, "stack overflow" or "access violation" on windows (forgoet which one)). Default limit for array size is 1Megabyte on windows (x86), and 8 megabytes on linux.

  2. You cannot determine size of a block allocated with new. int *p = new int[146]; std::cout << sizeof(p) << std::endl. will print sizeof(int*), not size of allocated memory. However, sizeof works on arrays.

  3. Theoretically, using dynamic memory allocation, you can allocate as much memory as you want (operating system may impose limits, though, and on 32bit system maximum allocated block size will be 2..3 GB). You can also control resource usage by freeing the memory, so your program won't be eating system ram/swap file for no reason.

  4. Dynamic arrays are not automatically freed, you have delete them manually.

That's just a brief overview.

Dynamic memory allocation provides finer resource control and removes some limitations of local variables.

Speaking of which, although you CAN use new/delete, if you want to use arrays with variable size, you should use std::vector instead. Manual memory management is prone to errors, so you should make compiler do it for you when possible. As a result, it is advised to at least study STL(Standard Template Library) , smart pointers, RAII, Rule of Three.

SigTerm
  • 26,089
  • 6
  • 66
  • 115
0

Dynamic is just that. You can change the size of the resulting array at runtime rather than compile time. There are compiler extensions that allow you to use static arrays (int array[CONSTANT]) like a dynamic array in that you can specify the size at runtime but those are nonstandard.

Dynamic arrays are also allocated on the heap rather than the stack. This allows you to use all of the available memory if necessary to create the array. The stack has a limited size that depends on the OS.

Allocating memory on the heap allows you to for example do this:

int* createArray(int n) {  
    int* arr = new int[n];  
    return arr;  
}  

int main()  
{  
    int* myArray = createArray(10);  
    // this array is now valid and can be used  

    delete[] myArray;  
}

I'm sure there are numerous other intricacies between dynamic vs static arrays but those points are the big ones.

eker676
  • 36
  • 1
  • No, you can't change the size. YOu can delete it and allocate new one, unless, of course, you're talking about malloc/realloc/free. – SigTerm Jun 15 '13 at 05:38