antoni wrote:Unfortunately, after removing 02 flag, result changed, but it was still senseless.
Does this mean your code is non-functional with -O0? Then you have a bug for sure.
antoni wrote:So, by inserting and removing 02 in selected places, I received various results, none of which was good. Have you any ideas how to deal with this problem?
This usually happens when you have UB in your code. The optimizer is not perfect, but in the last couple of years I've found only one bug, meaning it is quite good. It is more likely the strange behaviour is a result of UBs in your code.
As for the flags, I suggest to specify the -mcpu or -march, that means a lot to the optimizer. Also add "-fno-builtins", because gcc likes to replace your memcpy implementation with a memcpy call
Sometimes it could do this for malloc too. Btw, do you really need the large code model?
antoni wrote:Also, how should I debug this program? Should I turn off optimization and then debug it, or should I debug it with optimization?...
You can do both. Normally you would compile with -g -O0 for debugging, and -O2 for production, but only if you have no UBs, and -O0 and -O2 compiled code does exactly the same. Until then feel free to use -g -O2 and debug that (it is harder a bit, because functions get taken apart and inlined, but works).
Cheers,
bzt