Ok, here is the video :
http://www.youtube.com/watch?v=vOsLQ-d-XvA
Ok now i'll break this down so you can understand what its doing in the code if I do not put the * symbol in there.
1 2 3 4 5 6 7 8 9 10 11
|
// rememb.o.matic
#include <iostream>
#include <new>
using namespace std;
int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
| |
So if you type in any number below 4.2 billion it will not error out. However above it say 4.5 million and it will IF you add in the *. But without it, it will not throw an exception. So what its doing is bypassing this :
1 2 3
|
p = new (nothrow) int[i];
if (p == 0)
cout << "Error: memory could not be allocated";
| |
and goes on to the ELSE statement instead. But.. The reason ? well it thinks that p is an integer and not a pointer at this point. The * tells the compiler that it IS a pointer, whether this is right or wrong this is how MinGW is acting.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}
| |
So it goes into a loop at this point and just repeats itself counting the integer i down to the end of its number count that you typed in.
Now this page on this forum explains it better then I can because people get Pointers confused with Arrays.
http://cplusplus.com/forum/articles/10/
Its a BIG misunderstand for people who do not know the difference. And so they think Pointers are arrays. BUT.. Arrays have Pointers. Which is also brought out in that thread.
@ SIK - Try this and it will be the same as if you put in the * for both. Change both of those to this and it will work as expected just like the * Symbol does.
if (p[0] == 0)
As Cali pointed out, it acts differently on the MinGW and as I am showing you in the video. I also went a step further and tried it as Release and it does the same thing. So this is a compiler issue. Its why in all this past year of my putting * in its place has worked for me while on other compilers I do not need it.
Arthar Says...
Depending on that number, the allocation will either succeed or fail. If it fails, the nothrow variant of new[] will return 0 and you can test that with if (p==0)...
If you tried if (*p==0) in the failure case, you'd be dereferencing a null pointer and the application would crash, obviously. |
And see we are seeing the opposite in my video. The * makes sure that is does the job correctly. WHY ? I don't know and that is the problem here. I am wondering if there is a BUG in the debug for MinGW as Cali has pointed out. But its a BIG issue if we are all sitting here arguing about something, and we are all saying same thing but the compiler / Debug shows it different.
@ kev82 -- I love that. Thanks for sharing.
NOTE : I totally understand what Naraku is saying here
asterisk dereferences the pointer to access its value |
but this compiler doesn't seem to work right without the * symbol.