What's the point of Dynamic Allocation?

Hi there folks. I'm back with a new pointless discussion.
Lookie, here is a program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int size;
    cout << "What size do you want, sire? ";
    cin >> size;

    int arr[size];
    for (int i=0; i<size; i++)
    {
        arr[i] = i;
    }
    for (int i=0; i<size; i++)
    {
        cout << arr[i] << endl;
    }
    return 0;
}

The above program runs fine and gives:
What size do you want, sire? 10
0
1
2
3
4
5
6
7
8
9

Now, I was under the impression that compiler should give an error at the line where I declare the new array with a variable as array size. It should say that I need to enter a definite number there. I was just compiling this program to check since I remember that a long time ago when I was doing some college assignment stuff I needed to ask the user how much staff would he want in his office and that was to be done the exact same way. And I did the exact same way. And the compiler sure returned a big ugly error at that time.

I was reading in some info/tutor article about dynamic allocation and it said that this is not the right way to declare an array and you will have to do
arr = new int[size]
(Which I guess is what Dynamic Allocation means)
Now, folks, please solve this since I don't know who to believe, my past experience + the forum post OR current compiler behaviour + output.

Endnote: If you have something better to do, please do not waste your time on this discussion. I am not trying to get a problem solved, but a puzzle.
Now, I was under the impression that compiler should give an error at the line where I declare the new array with a variable as array size.


If a compiler is standards compliant, it would.

Your compiler probably allows it as an extention. But it isn't legal in the C++ standard and should produce an error (and it will, on other compilers).

I know GCC allows this as an extension. I don't think VS does.
Yeah yeah, yep. Right...
I am using CodeBlocks with GCC on Linux (Ubuntu). And the school homework I referred to, was probably done in some Visual Studio 6. (I was Linux ignorant at that time. :P Or for that matter, computer ignorant.)

But, may I probe further by asking if you (or anyone) knows what gcc is doing instead? (I mean what's running behind the extension?)
Like maybe it is automatically using the dynamic allocation by intelligent guessing of my motives (which could be dangerous because computers are so dumb.) Or it is formulating a new method of being able to allocate memory in runtime which doesn't need to know the size of allocated memory "prior" to execution?
It's very likely that it's taking your size as 0 or 1, which means you're rewriting random address that just happen to be behind the address that your array points to. It's legal, but not very smart.

More proper would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int size;
    cout << "What size do you want, sire? ";
    cin >> size;

    int* arr = new int[size]; // Allocate memory for an int array with the size being determined
                              // at runtime (a dynamically allocated array does NOT mean that the
                              // array is dynamic, in the context of having a dynamic size, the size
                              // is fixed whenever you instantiate it.)
    
    for (int i=0; i<size; i++)
    {
        arr[i] = i;
    }
    for (int i=0; i<size; i++)
    {
        cout << arr[i] << endl;
    }
    delete [] arr; // Deallocate memory of an array
    return 0;
}
Last edited on
It's very likely that it's taking your size as 0 or 1, which means you're rewriting random address that just happen to be behind the address that your array points to.

That makes me ask another silly question. What about the index of arr[ ]?
Why doesn't it point out that the index exceeds the size of an array when I access it by arr[i]?

And thanks for pointing out that this is not a good method. I really feel that the best standard should be followed by all programmers. (If that had happened, maybe there was a lesser chance of ghost bugs). I will go with the dynamic memory allocation. Thank you.
Last edited on
Because that will require an extra check and will be slower.
You can test it comparing vector::at() (which throw an exception in out of range), with vector::operator[].
madmaxsantana wrote:
But, may I probe further by asking if you (or anyone) knows what gcc is doing instead? (I mean what's running behind the extension?)

http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

Notice that the allocation is done on the stack, not on the heap. You can achieve a similar effect in VS using this here -> http://msdn.microsoft.com/en-us/library/wb1s57t5%28VS.71%29.aspx
Because that will require an extra check and will be slower.
You can test it comparing vector::at() (which throw an exception in out of range), with vector::operator[].


Whoa, dude. I am an amateur. I don't have any idea of vectors yet. (Well I know there are some in math, but not the C Vectors.)
What will require an extra check? If you are referring to my question about the index of the array in the above code, I am totally lost. A compiler is supposed to check for errors, slow or fast.


And @[m4ster r0shi]: Thanks a lot. Now I'm a lot clearer.
Last edited on
hello my friend..y will not comment you're source code,just try to answer you're question."What's the point of Dynamic Allocation?"..well the point is something like this(in my words).for example: there are 2 ways to allocate memory static and dynamic.
let's say you allocate memory static for a string: char Text[100].so in this string you can put max 100 characters.it's perfect.let's say that for that string you need 1 mb of ram memory..now if you're program will work only with small words for example,not over 30 characters,then what is the point of using space for 100???.u just waste memory..now if we think bigger,take the example of the application "word"(microsoft office word),or any text editor.there you can write how manny pages u want,how many mb of memory u have on you're pc.now y don't think you would be happy to write 1 page and that document to be 100 mb(just like 100 pages for example)..That is the point of Dynamic Allocation.you make space exactly how much you need.no more,no less.
Y hope y did not bored u with all the writing,and was to the point.
@Mihlay07: Thanks buddy. Although I knew it before, but you made the concept a whole lot clear by giving a beautiful example. I shall give the same example to my students (when I have some) and mention your nick as well. Thanks. My question was a little different from that but I guess your post contained definitely an appropriate answer.
1
2
int array[100];
array[1000000] = 42; //this is legal (it's not a syntax error) 

Your compiler could yield a warning, but is not an error.
Now if you're in execution time, how will you check for an out of range index?
1
2
3
4
if(index < size) //this is the extra check
   Out_Of_Range;
else
   return array[index];


vector is like a wrapper for a dynamic array, so you don't have to worry about the memory management (new and delete), it knows its size, etc. Read the reference, you'll find it useful.
Topic archived. No new replies allowed.