-4

I can't really understand the difference between Dynamic and static allocation,they say Dynamic allocation happens while executing the program and static only while compiling and we can't allocate manually while execution but,

#include <iostream>
using namespace std;
int main()
{
   int size , a = 0;

   cout << "Enter the size of Array: ";
   cin >> size;

   int A[size][size];
   for(int i = 0 ; i < size ; i++)
   {
        for(int j = 0 ; j < size ; j++)
            cout << a++ << '\t';
        cout << endl;
    }

    system("pause");
    return 0;
}

This program will allocate the Array size while execution.

WildFire
  • 79
  • 1
  • 9
  • 2
    This is not valid C++, standard doesn't allow Variable Length Arrays. And stack is little compared to heap, you can't put huge objects on stack. – Yksisarvinen Sep 21 '18 at 12:31
  • `int A[size][size];` is not legal C++, it is legal C. In any case dynamic memory allocation is about a lot more than array sizes. It's really about the **lifetime** of the object allocated. Dynamically allocated objects live until you deallocate them, that's not true with non-dynamically allocated objects. – john Sep 21 '18 at 12:33
  • 3
    you dont need pointers for dynamic arrays in C++, thats a myth coming from the C legacy. Use `std::vector` if you need a dynamic array – 463035818_is_not_an_ai Sep 21 '18 at 12:35
  • And, furthermore, arrays allocated in this manner exist only until the function that declares them returns. There's no choice in the matter. When the function returns, it's gone. If you need the array to remain in place, and continue to be used, you're out of luck. Do you really believe that large applications, with hundreds of thousands of lines of code, can be implemented simply by writing a single function that allocates all the arrays that it could possibly need, just once, and whose size can never change after the fact? – Sam Varshavchik Sep 21 '18 at 12:38
  • ***I can't really understand the difference between Dynamic and static allocation*** This is because you were tricked by an illegal construct (VLAs are not legal in standard c++) that dynamically allocates some memory. When say some memory I believe most VLA implementations allocate memory on the stack which is only a few MB at most. – drescherjm Sep 21 '18 at 12:47
  • 1
    Possible duplicate of [Why should I use a pointer rather than the object itself?](https://stackoverflow.com/questions/22146094/why-should-i-use-a-pointer-rather-than-the-object-itself) – Fureeish Sep 21 '18 at 12:50
  • 2
    For the reasons given above I think you will find that pretty much *all* programming languages use pointers. Different languages simply hide them in different ways. What `C++` does is provide an extremely low level way of dealing with pointers directly. Of course in modern `C++` you rarely need to do that unless you are doing something advanced or inherently low level (*someone just needs to tell all the c++ programmers that*). – Galik Sep 21 '18 at 12:54
  • OP mentioned "specifically C++", but languages like C# and Java have pointers too. Although the terminology used by those languages is "references", they're like C++ pointers and not like C++ references. (Not sure if we're picking on C++ in particular, or if its a broad question about all programming languages.) – Eljay Sep 21 '18 at 14:04
  • To answer the title of your question: Pointers contain addresses. In many systems, pointers are used to access hardware devices at specific addresses. Pointers can also be used to change the value of variables passed to functions (The preference in C++ is to use references instead). – Thomas Matthews Sep 21 '18 at 14:22
  • I think the real question is why would we need to use `new` if we have VLAs. – drescherjm Sep 21 '18 at 16:13

1 Answers1

3

The real point of dynamic allocation is that you control the lifetime of the objects being allocated. Dynamically allocated objects exist until you deallocate them. It's not really anything to do with arrays, although that is often the context in which beginners are first taught about allocation.

Consider these two functions

int* bad()
{
    int x = 123;
    return &x;
}

int* good()
{
    int* x = new int(123);
    return x;
}

Both functions create an int and return a pointer to that int.

The bad function is incorrect because the x variable is destroyed when the function exits, so it returns a pointer to an object which has been destroyed.

The good function creates an int dynamically, that object will never be destroyed (unless the program deletes it). So this function is correct.

Incidentally int size; ... int A[size][size]; is not legal C++. Some compilers allow it, but other compilers would not.

john
  • 85,011
  • 4
  • 57
  • 81