c - Will this result in a seg fault -
below code :
p = (float *) malloc(sizeof(float) * n); p1 = p; // p1 float* p--; // result in seg-fault?
i guessing yes because trying access memory outside allocated space. please confirm?
edit after seeing hobbs' answer not resist asking too.
i believe not result in seg fault
printf("%f",p[n]); // because n legally allocated p
you're not accessing memory @ all; you're doing pointer arithmetic. using pointer arithmetic obtain pointer isn't inside object or 1 past end of array undefined behavior, happen, including segfault. however, on reasonable systems, nothing happen unless try *p
. @ point, happen, receiving garbage values, crash/segfault, global thermonuclear war.
Comments
Post a Comment