struct_wrapper_dynamic

The following example shows how to create a wrapper to another structure, which helps to manage the size of a structure – how many elements are in total and for how many is there room for? Wrapper structure holds those numbers and additionally it holds the pointer to the structure array which we allocate dynamically.

To test the program workflow, you have been provided with a sample data file. With the sample data, it creates a structure that can hold 2 members. Then it says it wishes to insert two members and enters the details for those. After this, it attempts to increase it by 3 new data lines. Depending on the implementation used, the result will differ. To try out the test data, use stream redirection ( ./program < test_data.txt )

The following code files are given: .c file and the header file. There’s a macro in the codefile called EXPANDABLE, which value decides if the structure can be resized during reading or not. When setting the value to 1, it uses realloc() to expand the memory area. If it is set to 0, the structure size is fixed and cannot be increased.

The expanding is done with the principle, that each time the amount of memory is increased by 2x