I'm using viz studio 6 (shouldn't matter, but fyi), and am kind of curious why this works...
#include<iostream>
#using namespace std;
int main(int argc, char* argv[])
{
int *p;
p = new int[10];
for (int i=0;i<10;i++)
p = i;
// curiosity lies here, i increases out of the domain,
// but no error is given, and values are returned!!!
for(i=0; i<15; i++)
cout << p << " ";
cout<<endl;
return 0;
}
/* output:
0 1 2 3 4 5 6 7 8 9 -4.22017e+037 -1.9984e+018 -1.9984e+018 -1.9984e+018 1.35926e-043
*/
Is it actually creating a set of 10 pointers when I use the new, or is it just an infinite array, that only has values if I use it...
If anybody knows why this happens or has a better way to do dynamic array allocation, let me know!
-thx
#include<iostream>
#using namespace std;
int main(int argc, char* argv[])
{
int *p;
p = new int[10];
for (int i=0;i<10;i++)
p = i;
// curiosity lies here, i increases out of the domain,
// but no error is given, and values are returned!!!
for(i=0; i<15; i++)
cout << p << " ";
cout<<endl;
return 0;
}
/* output:
0 1 2 3 4 5 6 7 8 9 -4.22017e+037 -1.9984e+018 -1.9984e+018 -1.9984e+018 1.35926e-043
*/
Is it actually creating a set of 10 pointers when I use the new, or is it just an infinite array, that only has values if I use it...
If anybody knows why this happens or has a better way to do dynamic array allocation, let me know!
-thx