This program crashes and I can't find any nonlegal action regarding getting array content and moving pointer. What is wrong?
#include <iostream>
#include <stdint.h>
using namespace std;
int main( int argc, char ** argv ) {
int * p = new int [20];
for(int i=0 ; i<20 ;i++ )
{
p[i]=i;
}
for(int i=0 ; i<20 ;i++ )
{
printf("%d ",*p );
p++;
}
delete [] p;
return 0;
}
newin the first place? You're going through a lot of extra gyration to get pretty much the result you'd get from just makingpan array/std::vector/std::arraylocal tomain. You've gotten lots of advice about how to keep thenew/deletefrom crashing, but none that points out that you shouldn't be using them to start with (and even when you neednew-like capabilities, you should look atmake_sharedandmake_uniqueinstead of usingnewdirectly).