Find print min path

I’m a little bit confused about the function in main.c, find_print_min_path, which takes an image as parameter. However we are supposed to call the functions calculate energy, calculate min energy column and calculate optimal path which takes an additional parameter, w which is the column, where we should stop. This w is not given as parameter in the function find_print_min_path. Can you tell me please where I’m supposed to get it ?

Jerry :slightly_smiling_face:

Hey @Jerry.Takou,

you can just give it the total width for w and w0, since the function find_print_min_path is not supposed to carve any seam from the image. So, w is always the total width for this function.
This will change, when you have to implement find_and_carve_path. Because there, you’ll get a n parameter, which stands for “n iterations” of the algorithm. For every step, w0 stays the total width of the original image, but w (the boundary) gets smaller everytime :slight_smile:

Hope that helped,

Great ! Thanks :grinning:

You are welcome! :smiley:
Feel free to mark the thread as solved, in case it works for you ^^
Otherwise feel free to ask further questions :smile:

I would like to ask a further question about using malloc in this task.

I think we are supposed to use malloc for the energy and seam arrays but not image?
can we use malloc for both of them already at the very first lines?

first: their pointer type should be *uint_32? (without const?)
space for energy h*w*sizeof(uint_32)?
sapce for seam h*sizeof(uint_32)?

second: should we think about the case whether they are successfully allocated or not?
(energy == NULL) then we should exit?
same for seam?
or it is not necessary?

last but not least:
at the end, should we also free their spaces by using free()?

sorry for the question bombs…just struggling with the \

Hello @Lux,

yes, you need to allocate space for seam and energy in find_and_carve_path.
The pointer types are implicitly given by the header files :slight_smile:

Idk if we are supposed to check for the case if the space was allocated successfully. Maybe a tutor should answer this question, but I don’t think that this is a thing for us.

Yes! You need to free manually allocated memory. Otherwise, you would cause a memory leak.

1 Like

If you allocate memory, you should generally be aware that it might fail and handle such a case gracefully by freeing the used memory if you can and exit with an error.

1 Like

(created a new topic for this)

You do not need to explicitly allocate memory depending on your implementation.

Well, I once asked here if we are supposed to use dynamic allocation and it seems like we are. But yea, maybe one could do it without but I decided to be on the safe side :slight_smile:


I would very strongly recommend against any such implementation where you do not allocate memory on the heap.

TL;DR: Use malloc.

1 Like

:confused: well sh** :zipper_mouth_face:

thanks for the hint. :grin:

But still, the question: Are all the tests that thrown at us constructed in a way that we HAVE to use memory allocation on the heap or can we pass the tests by doing it the other way? I realized that it is more an assumption by me rather than a possible hint by you (?).

It’s not unusual to have evaluation tests that test your program on larger images.

Double negation, hm… I will allocate on the heap. :joy: