1

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

  1. I could see the allocated memory clearly, and free it at the end of main().
  2. 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?

okeyla
  • 65
  • 9
  • 1
    What you've posted is C code. Are you compiling this with a C++ compiler? [Edit] you tags to only feature the correct language, as the answers will differ between the two languages. – 1201ProgramAlarm Oct 13 '18 at 02:12
  • 1) Avoid the global `data` used in `colleague2_way()` 2) Check allocation success. 3) Use more informative function names. 4) Allocation function `colleagueN_way()` deserves free'ing paired function `colleagueN_unway()` 5) Avoid filling `main()` with application specifics 6) Consider [codereview](https://codereview.stackexchange.com) for such reviews of more expanded code. – chux - Reinstate Monica Oct 13 '18 at 03:45

2 Answers2

0

I dont know the specifics of why you want heap memory but stack is faster. Your sample code makes your program seem small so i would do something like:

#define IMAGE_WIDTH 1280
#define IMAGE_HEIGHT 800
#define PIXEL_DEPTH 2

int main(void)
{
    char data[IMAGE_WIDTH][IMAGE_HEIGHT][PIXEL_DEPTH] = {0};

    function1(data); //pass by reference 
    function2(data); //pass by reference
    function3(data); //pass by reference

}

This is like your solution but you dont have to worry about freeing memory and you get faster writes. Like you said different programmers have different solutions, thats just my two cents.

Bwebb
  • 675
  • 4
  • 14
  • not sure why you thumbs me down. 1 and 3 have the char * on the stack and 2 has it in data segment, they are all essentially the same. You asked if theres a better way, and letting the memory get allocated on the stack is better in terms of speed. Hope this helps. – Bwebb Oct 13 '18 at 03:12
  • 1
    From many of my programming teacher, they avoid to allocate such continuous and big area by static way. (The image size for this program might grow up in the future) For small area, your way is straight-forward. – okeyla Oct 13 '18 at 07:29
  • @okeyla Why? I am failing to understand why 2MB on the stack is bad. You dont "gain" memory with heap memory since the program lives in ram anyways, I dont see the gain here. – Bwebb Oct 14 '18 at 07:07
  • [How much stack usage is too much?](https://softwareengineering.stackexchange.com/questions/310658/how-much-stack-usage-is-too-much) – John Kugelman Oct 15 '18 at 18:35
  • @JohnKugelman Thanks for the link John, an interesting read. I still dont understand why people think it would increase the chance of a stack overflow though. I have this memory model in my head https://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-1.html which makes me think stack/heap use the same memory in ram. What i mean is that instead of 2MB on the heap if you had it on the stack the actual memory left (until they collided) would be the same right? If you had 8MB stack and you allocate 2MB heap, that subtracts it out of stack memory, now you have 6MB stack right? – Bwebb Oct 15 '18 at 20:45
0

First lets talk about "Colleague2" way: char pointer data declared globally

You should avoid declaring variables globally until it is really needed. Here, I don't see any reason of declaring data pointer globally. Since it is visible to other functions it's scope is increased and can be accidentally modified by other part of code which is not desirable.

"Colleague1" and "Me" way:
In these both ways, one good thing is the scope of data pointer is limited. But in the "Colleague1" way the memory allocation operation is part of another function, which is actually good because you can do the memory allocation error handling in one place (which is missing in all the 3 ways). Consider a case where you need to allocate same size of memory in some other part of the code then you just need to call this function instead of again doing malloc. If you are sure that you will not need to allocate the same size of memory again then in that case "Me" way is also okay. So, out of these two ways follow whichever way is appropriate but make sure to do the required error handling.

H.S.
  • 11,654
  • 2
  • 15
  • 32
  • Thank you. And do you have any example declaring variables globally and it is really needed? – okeyla Oct 13 '18 at 07:32
  • @okeyla check this [when-is-it-ok-to-use-a-global-variable-in-c](https://stackoverflow.com/questions/176118/when-is-it-ok-to-use-a-global-variable-in-c). – H.S. Oct 13 '18 at 09:08