Malloc pointer return

Pretty simple question here, I'm a little unsure of the memory allocation in C.

If I have the following

CGPoint* tileForCoordinates (CGPoint position, short width, short height) { CGPoint *tileCoordinate = (CGPoint*)malloc(sizeof(CGPoint)); tileCoordinate->xTile = (position.xPosition / width); tileCoordinate->yTile = (position.yPosition / height); return tileCoordinate; } 

and I wanted to name it in another source file or something else, would I declare a pointer and higher and return it? If so, when I call from another class, for example.

 CGPoint *currentTilePosition = tileForCoordinates(curPosition, 50, 50); 

What happens to the pointer returned by malloc? Should he be released or what story? :)

+4
source share
5 answers

Can you pass tileCoordinate as an argument to a function call? Thus, it will be easier for the subscriber to remember malloc / calloc and for free.

+4
source

A very simple rule: if you return the pointer * malloc * ed or pass the function to the user, pass the pointer back to release or the document that you * malloc * ed it and that they should free it.

I have seen many solutions for this (including a complete abstraction of memory management), and personally I prefer the API, which is called free, because it’s clear what to do when it comes time to free the object and the API gives the last attempt to do any additional cleaning work.

+3
source

The subscriber will have to free indicate the malloc ed pointer with your function.

However, if you are packing the tileForCoordinates function into a dynamic library, you must also provide a companion function to free memory and not have it. This is because the caller may be bound to the C runtime in a different way than your library. For example, if your library statically refers to the C runtime while the caller dynamically references it, or vice versa, if the caller frees memory, it will crash. In such cases, you can provide a function similar to the following:

 void freeTileCoordinates( CGPoint **tileCoordinate ) { // Also add additional checks for NULL pointer free( *tileCoordinate ); *tileCoordinate = NULL; } 

Usage example:

 CGPoint *point = tileForCoordinates( ... ); // use point ... // now free it freeTileCoordinates( &point ); 
+3
source

To answer "what happens to the pointer returned by malloc ()?"

The pointer declared by malloc() will be the value in the stack frame of the currently executing function. When a stack frame (and therefore *tileCoordinate ) goes out of scope when the function returns, this pointer will cease to exist.

However, since you are returning the pointer value of the calling function, it now exists in the current frame of the stack (after returning). This value refers to the *currentTilePosition variable.

the memory allocated by malloc() is a completely different story; dynamic allocated memory exists on the heap. You must free up any memory you allocate in the same implementation that performs the allocation. This implies calling free() on currentTilePosition as soon as you do this, usually in the same file.

+3
source

Generally, all you need is malloc , you also need free . As another rule, if your api is doing the allocation, your api should also do the exemption, as at some point in the future you can change the main mechanism. In addition, on Windows, if memory is allocated by the DLL, it must be freed by the same DLL or a crash occurs.

So, in the general case, the template is as follows:

 MyType* foo = myapi_dosomething(x,y,z); if (!foo) die("No Foo!"); use_object(foo); myapi_free(foo); foo=NULL; // just in case 
+1
source

Source: https://habr.com/ru/post/1345969/


All Articles