Development Platform
Operating system: Kylin 3.3 (CentOS 7 kernel)
IDE: VS Code (remote development and debugging)
Language: C/C++
Cause of the problem
Due to project requirements, two C++ programs (hereinafter referred to as A and B) need to be modified. Inter-process communication uses shared memory. However, when compiling the code, two of the definitions were commented out because of constant redefinition, which eventually led to inconsistent data structure sizes, and then caused the following two weird problems:
- The same KEY of program A cannot open the shared memory created by program B. Restarting the virtual machine several times will work, and the probability of occurrence is low.
- The data written by program A is different from that read by program B.
Troubleshooting process
Under the guidance of the boss, the following investigations were done
- Determine whether the structure is aligned (#pragma pack directive)
- sizeof confirms whether the size of the structure is consistent
fix
Restore the commented constants and recompile.
Summarize
When you haven't fully read other people's code, don't change it randomly.