I wrote a code to convert kilos to pounds and stone. A snippet is shown below.
float *weightoutput = weight_conversion(weight); with the function as below. I used a C-style array,
float* weight_conversion(float x){ float *stones_pounds = new float[2]; //C-style array float z, y; z = x*0.15747304441776971; // (1 stone = 6.3503 kilo) y = x*2.2026; // (1 kilo = 2.2026 pounds) stones_pounds[0] = z; stones_pounds[1] = y; return stones_pounds; } I have read in multiple posts that if you use "new", you have to use to "delete" to free up memory.The question I have is, how could I delete stones_pounds in this setup. Since stones_pounds is used till the last line within the function in return, i don't think i can use delete [] stones_pounds within the function. Since it is not a global variable, i cannot delete it within the main function as well. So how do I go about using the delete[] operator to free up memory? Is the only alternative changing the structure of the code to facilitate the delete[] operator?
std::vectororstd::array? edit: in this case you could actually return astd::pair<float, float>which would probably be clearernew[]at all (by using astruct).struct?x? what isz? what isy? These variable names give no meaning to the input or to the results. Unless you might need to have 10, 50, or even a 1000 values returned (i.e. if it is always only two values), a data structure with clear names (as well as a clear parameter name to the function) would make this code much clearer as to what it is doing.