Home>

I'm currently doing numerical calculations at the university using the C language.

I'm having trouble

When using the same program on my PC,
There is a phenomenon that results are different when using a laboratory computer.
Also, if you put printf in a specific place,
Results that should have been different begin to match.

What you ’ve done

I have been told that the above problem occurs when I refer to a memory leak or outside the array area.
I want to look back at the program or use a debugging tool to solve it, but I'm stuck.

Tools and options used so far
● valgrind (option is --tool = memcheck --leak-check = full)
Gcc -D_GLIBCXX_DEBUG -fsanitize = address -fsanitize = leak
● cppcheck

etc, but no error in the end
(In valgrind, it is 0 bytes except for still reachable and suppressed, so it is judged that there is no error.)

Question

If i know any tools or options that solve the above problems, please let me know.

It may be better to put the code on it,
Because it is quite redundant, I will stop it.
Sorry for the fluffy question.

c c++
  • Answer # 1

    I wonder if it is a UB as I did in the postscript request section.

    Explanation that you may not know too much about Undefined Behavior (UB) and the surrounding C ++ terms

    Standards and Processing System-cpprefjp C ++ Japanese Reference

    I don't want to say that C ++ er is "legal" or "illegal", but what does it mean in the end? Mystery-Qiita

    If you don't want to give up around anything that is undefined behavior,the compiler is allowed to do anything.

    Specific examples are

    Bookworm: Old New Thing: Undefined behavior causes time travel (although there are many others, time travel is the fastest)

    Bookworm: Counterattack of a very clever compiler

    is famous.

    It is very easy to follow undefined behavior in C/C ++

    Devil from the nose: indeterminate value version-Qiita

    C/C ++ should be able to calculate the absolute value of the difference safely before becoming null safe-Qiita There is no sharpness at the beginning such as

    .

    Some of the standard patterns that follow undefined behavior can be detected by maximizing compiler warnings, code analysis, etc., using UndefinedBehaviorSanitizer, etc. Often there is nothing but a lot of printf and steady trial and error.

    In order to reduce this as much as possible, it is important to conduct unit tests (UnitTest) and loosely couple the code, and gradually increase the number of safe code.

    There are various UnitTest frameworks and I think GoogleTest is famous, but I use iutest.