c - I allocate memory in function, and return char*, how should I free it? -


if want allocate memory in function:

char* allocate() {     char *cp = (char*)malloc(10);     ...     return cp; } 

can use returned content in cp main()? , how free cp?

can use returned content in cp in main()?

yes, can.

and how free cp?

using

free(cp); // replace cp name of pointer if using in function 


also, should not cast result of malloc(and family) in c


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -