Dynamic memory

Mar 1, 2013 at 12:25pm
Why does expression with double execute and with int doesn't? May somebody explain?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main()
{
    int a;
    double b;

    int *p=new int [b];        //that one doesn't work
    double *p1=new double [a]; //but that one works

    return 0;
}
Mar 1, 2013 at 12:31pm
The new operator allocates memory for a specified number of items. That has to be a whole number. It wouldn't make sense to allocate half an item. Hence it has to be an integer value.

Also, a should be assigned some specific value before trying to use it.
Last edited on Mar 1, 2013 at 12:32pm
Topic archived. No new replies allowed.