c++ - Xcode: malloc size doesn't seem to matter -


this question has answer here:

in codes below, can change parameter of malloc whatever values (at least ones have tried), , looks okay.

for example, can change "3 * sizeof(float)" "3 * sizeof(char)", or zero. , can still "correct" results. heap size doesn't seem matter @ all.

this confuses me. how can happen?

the codes written , run in xcode 5.

float *favorites = malloc(3 * sizeof(float));  favorites[0] = 3.14158; favorites[1] = 2.71828; favorites[2] = 1.41421;  (int = 0; < 3; i++) {     printf("%.4f favorite %d\n", favorites[i], i); }  free(favorites); favorites = null; 

this because doing program invokes undefined behavior. once ub invoked, bets off. may either expected or unexpected result.

now try without allocating memory favoritee

float *favorites ; ... 

and see happen?


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -