There's different coding style with different programmers. Colleagues and I are working on image data processing and we have 3 different ways.
Colleague1:
int IMAGE_WIDTH=1280;
int IMAGE_HEIGHT=800;
char* colleague1_way() //just allocate when he wants
{
char* mem = malloc(IMAGE_WIDTH*IMAGE_HEIGHT*2);
return mem;
}
int main(void)
{
char* data;
data=colleague1_way();
function1(data); //pass by pointer
function2(data); //pass by pointer
function3(data); //pass by pointer
free(data);
}
Colleague2:
int IMAGE_WIDTH=1280;
int IMAGE_HEIGHT=800;
char* data; //set it as global memory
void colleague2_way()
{
data = malloc(IMAGE_WIDTH*IMAGE_HEIGHT*2);
}
int main(void)
{
colleague2_way();
function1(); //void input, proceed data inside function
function2(); //void input, proceed data inside function
function3(); //void input, proceed data inside function
free(data);
}
Me:
int IMAGE_WIDTH=1280;
int IMAGE_HEIGHT=800;
int main(void)
{
char* data = malloc(IMAGE_WIDTH*IMAGE_HEIGHT*2);
function1(data); //pass by reference
function2(data); //pass by reference
function3(data); //pass by reference
free(data);
}
My idea is
- I could see the allocated memory clearly, and free it at the end of main().
- function1~3 might be in another cpp file, so it's easy to handle.
Anyone could give me a comment and is there any better way? Also, if it's in C++, any good ways?