Sorry, the question has already been asked - I did not find
Story:
I am writing a program for working with DDP, AVL-tree and hash table. And there is such a structure:
struct AVL { int value; unsigned char height; AVL *left, *right; }; In this form, everything works
However, if I swap fields like this: `
struct AVL { AVL *left, *right; int value; unsigned char height; }; Or using #pragma pack:
#pragma pack(push, 1) struct AVL { int value; unsigned char height; AVL *left, *right; }; #pragma pack(pop) That program breaks down.
In the debugger, I traced a problematic place, it is in the function of creating an AVL-tree based on the DDP:
void bst_to_avl(BST *tree, AVL **root) { if (tree && root) { *root = avl_add(*root, tree->value); bst_to_avl(tree->left, root); bst_to_avl(tree->right, root); } } Here is the state in the problem function before calling avl_add:
The correct vertex is created in the avl_add function:
But, after exiting the function by return, it spoils and turns into this:
Naturally, the program does not work properly.
And I don’t understand a few things - how to fix it (if I, from the principle, want just such an order of fields)
And why does it even arise?
Maybe somewhere I made a very stupid mistake and now I just don’t see it.
If necessary, I can put the whole project here, but there are a lot of files
Contents BST.h
#ifndef _BST_H_ #define _BST_H_ typedef struct BST BST; struct BST { BST *left, *right; int value; }; BST *bst_add(BST *root, int num); void bst_free(BST *root); int count_peaks(BST *root); #endif
baadf001... The presence ofbaadf001in the memory is most likely not an accident, and as if it hints, something else happened between these two points. - AnTprintf("%zu\n", offsetof(AVL, right))insideavl_addand insidebst_to_avl. Do these values ​​match? - AnT