pointers - What is the difference between float* and float[n] in C/C++ -


this question has answer here:

what difference between using

float* f; 

and

float f[4]; 

or treated exact same way? using 1 on other potentially cause run memory allocation issues? if not, there situations treated differently?

don't know if relevant, i'm using type float* function argument. example:

void myfun(float* f){      f[0] = 0;      f[1] = 1;      f[2] = 2;      f[3] = 3; } 

which compiles , runs fine (i'm not sure why - think since didn't allocate memory f throw kind of exception).

float f[4] 

is array allocated automatic storage (typically stack)

float* f; 

is pointer, has no allocation other size of pointer. can point single floating point value, or array of floats allocated either automatic or dynamic storage (heap)

an unallocated pointer typically points random memory, if pass myfun function, undefined behavior. may seem work (overwriting whatever random memory pointing too), , except (trying write invalid or inaccessible memory)

they treated same in many situations, , not in others. i.e. sizeof(f) different.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -