cstring - char* as an argument to a function in C -


While passing four * as an argument for the function, should the function work on that string for free? Otherwise the data will be "lost" and the program will leak data. Or the four are handled by the compiler in a special way so that everyone can be freed from all the time and it will be destroyed automatically, which is the scope? I pass the function "string" so that not for the four * examples already present. Or should you use four instead? Just to determine a certain limit for the logic input seems so dumb.

Keep this simple principle in mind: always at the same level In other words, a function should never try to free the memory, which has not been allocated, "free memory that you allocated it. A small example to illustrate this:

  #include "graphics.h" // Graphics API will receive a canvas object for us, this new can be allocated / or the previously allocated object A canvas from the pool of * canvas = graphics_t_anvas (); // if draw_image () frees the canvas, which violates the above principle. // The program's behavior will be unspecified. So, just draw the image // and return Draw_image (canvas); // It is also infringement // free (canvas); // The right thing to do is to give the Canvas object back to the graphics API so that it is freed on the same level where it was allocated. Graphics_reature_convance (canvas);  

Note that the function name is graphics_free_canvas () not something or nothing, because the API can choose to empty it or return it to the bridge. . The point is, this is a very bad programming practice, which we did not accept the ownership of a resource, unless we specifically tell otherwise.


Comments