I had allocated the memory but not freed it, where to write the free(Coordinates), or the compiler do it automatically.
If Compiler doesn't do it, then how can i do it what would happen if i call this function multiple times.
Is it true that each time a new memory is allocated.???
Hello!!! I am using Ubuntu 14.04 and Code::Block13.12, to write a program in C Language.
I had allocated the memory but not freed it, where to write the free(Coordinates), or the compiler do it automatically.
If Compiler doesn't do it, then how can i do it what would happen if i call this function multiple times.
Is it true that each time a new memory is allocated.???
1. you correctly use malloc() inside the function to create the structure and then return the prointer to main()
2. the run time system will not free it for you (like in Java where the garbage collector frees unreference objects)
3. every time you call DisplayCoordinate(); it will allocate storage on the heap and you will sooner of later run out
4. in main() once you have finished with the structure you can free it, e.g.
so long as the pointer points to a valid memory block you can call it anywhere
if it does not (you have already freed the block or pointer is corrupt) you get undefinded behaviour
failing to call free() (or delete in C++) means you can run out of heap space
The original code is correct except for nofreeing the allocated memory. As said, this can be done e.g. after procesiing the data in main.
There are however only few cases where using malloc in embedded application serves a reasonable purpose. In a first order I would either assihn globally static memory for LAT_LONG, or if the data is only used in a level below main, an automatic variable in the respective function. A pointer to the LAT_LONG structure would be passed to the lower functions.