Hello ,
I wanted to ask why this works?
int *p = new int[ 1 ];
for ( int i = 0; i < 5; i++ )
{
p[i] = i;
}
for ( int i = 0; i < 5; i++ )
{
p[ i ] = 2 * p[ i ];
cout << "\n" << p[i] << endl;
}
Since ,we have allocated only for 1 element!
Also , if I have something like this:
typedef struct {
int v;
int arr[ 5 ];
} mystruct;
mystruct * str = new mystruct[ 2 ];
and try to access:
for (int i = 0; i < 7; i++ )
{
a[ i ] = str[ i ].arr[ 0 ];
}
is it possible?Is it ok?
Because I allocated 2 elemnts of the struce and I am scanning 7.