Question by cold_cold_cool: C programming question on arrays for embedded systems………….?
lets say I have a loop,
data = (float *)malloc(sizeof(1000));
// do something to data
does the compiler create only one memory location even thought the malloc() function is called several times?
Is it simpler for program efficiency in embedded systems to define arrays ahead of time?
Answer by Mn
Wow – what a load of data this generates.
First – sizeof(1000) is probably 4 bytes – the number of bytes that is required to hold the constant integer 1000
Second – malloc operates slightly differently on different OS’s. Despite you only asked for 4 bytes you probably got at least 256.
Third – every time you do a malloc another block of memory is acquired. Depending on the implementation and the way you set the loop up you could get back the same block every time
you meant to say
data = (float *)malloc(1000)
Add your own answer in the comments!