How does Lua UTString ensure maximum string alignment?

I am reading the lua (5.3.0) source code, and in lobject.h

I found it using a strange method for manipulating a string:

/*
 ** Header for string value; string bytes follow the end of this structure
 ** (aligned according to 'UTString'; see next).
 */
 typedef struct TString {
     CommonHeader;
     lu_byte extra;  /* reserved words for short strings; "has hash" for longs */
     unsigned int hash;
     size_t len;  /* number of characters in string */
     struct TString *hnext;  /* linked list for hash table */
} TString;


/*
** Ensures that address after this type is always fully aligned.
*/
typedef union UTString {
    L_Umaxalign dummy;  /* ensures maximum alignment for strings */
    TString tsv;
}UTString;


/*
 ** Get the actual string (array of bytes) from a 'TString'.
 ** (Access to 'extra' ensures that value is really a 'TString'.)
 */
#define getaddrstr(ts)  (cast(char *, (ts)) + sizeof(UTString))
#define getstr(ts)  \
check_exp(sizeof((ts)->extra), cast(const char*, getaddrstr(ts)))

      

I found a good answer on the reason for using such a method in there . But I'm wondering oh ensures maximum alignment for strings

what does that mean? Why do we need maximum alignment

and how to provide?

+3
c string alignment lua lua-5.3


source to share


No one has answered this question yet

See similar questions:

8
Why are Lua inner strings stored the way they do?

or similar:

7432
How to check if a string contains a substring in JavaScript?
3999
How do I replace all occurrences of a string?
3799
How do I read / convert an InputStream to a string in Java?
3602
Does Python have a substring method "contains"?
3515
How do I make the first letter of a string uppercase in JavaScript?
2873
How do I iterate over the words of a string?
2853
How can I convert String to int in Java?
2664
How can I check if a string contains a specific word?
2273
How to check if a string contains a substring in Bash
1653
How to align checkboxes and their labels, consistently cross-browsers



All Articles
Loading...
X
Show
Funny
Dev
Pics