Access to struct members and undefined behavior of C code

Authors

  • Gerasimov Volodymyr
  • Karpenko Nadiia
  • Skuratovskyi Ihor

DOI:

https://doi.org/10.34185/1562-9945-5-148-2023-02

Keywords:

undefined behavior, structure, compiler, programming language C.

Abstract

During software development, novice developers usually receive a lot of error messages and just warnings of various kinds. And if the code simply won't run when there are errors, then the program usually starts when there are various warnings. And here it is important to understand what consequences the presence of warnings of various kinds can lead to. This work aims to study the code's undefined behavior when working with struc-tures in the C programming language when issuing a corresponding compiler warn-ing about returning the address of a local or temporary variable. In the procedural programming language C, there is an ancestor of the OOP class — the structure struct, which encapsulates only the state of the entity. And the question arises — is it possible to work with separate components-fields of such a structure analogously to OOP languages? For research, a simple structure was taken, which contains information about the person's name, surname, and phone number. To access parts of the structure, pseudogetters were used — functions that returned a pointer to the corresponding part of the structure. The research was conducted in the Visual Studio 2019 environment under the control of the Windows 10 operating system when the default C language standard - MSVC and the more modern ISO standard C17 (2018) was selected in the project settings. As a result, a truly undefined behavior of the code was obtained, when the re-sult of the work of the code fragment (function call) depends on many factors: the length of the array, the standard of the C language, the position of a certain part in the structure. An attempt to conduct similar research under the control of the Linux Mint oper-ating system using the gcc compiler version 5.4 was unsuccessful. When compiling the code, a similar warning about returning the address of a local variable was also issued, as in the case of Visual Studio. But when the program was launched, it sim-ply crashed with a message about a segmentation error. Thus, both the Visual Studio 2019 compiler and the gcc compiler warned us about undefined code behavior. But this uncertain behavior was radically different for operating systems and compilers. If after gcc under the Linux OS, the code simply does not work at all and the program stops its work with a segmentation error mes-sage, then after Visual Studio under Windows, inexperienced developers with im-proper testing and verification of the code can miss the code that "does not always work", which can lead to unexpected results, not always pleasant, to say the least. And that's why software developers, especially beginners, should pay attention not only to compilation errors but also to warnings, even if the code works.

References

Oshybky i preduprezhdenyia kompyliatora i sredstv sborky C/C++. URL: https://learn.microsoft.com/ru-ru/cpp/error-messages/compiler-errors-1/c-cpp-build-errors?view=msvc-170

Konfiguratsiya kompilyatora: urovni preduprezhdenij I oshibki URL: https://ravesli.com/konfiguratsiya-kompilyatora-urovni-preduprezhdenij-i-oshibki/

Meyers S. Efektivnoe ispolzovanie C++. 55 vernykh sposobov uluchshyt strukturu i kod vashykh programm. – M.: DMK Press, 2006. – 300 s. – ISBN 5-94074-304-8, 0-321-33487-6.

Segmentation Fault in C/C++ - GeeksforGeeks.

URL: https://www.geeksforgeeks.org/segmentation-fault-c-cpp/

Herasymov V. V., Karpenko N. V. Problema nevyznachenoi povedinky kodu na movi C pry roboti zi strukturamy // XI Mizhnarodna naukovo-praktychna konfere-ntsiia "Suchasni problemy i dosiahnennia v haluzi radiotekhniky, telekomunikatsii ta informatsiinykh tekhnolohii", m. Zaporizhzhia, NU "Zaporizka politekhnika", 12-14 hrudnia 2022 r., s. 140 – 142.

Karpenko N. V., Herasymov V. V. Suchasnyi pidkhid do prohramuvannia na movi C vid nulovoho do prosunutoho rivnia: navchalnyi posibnyk — Dnipro, «Lira LTD», 2022. — 418 p.

The Standard – C. URL: https://www.iso-9899.info/wiki/The_Standard

Downloads

Published

2023-12-19