From 95de9b63215c5203235ab1e5dade4c28233deb45 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Wed, 29 Mar 2017 19:06:12 +0200 Subject: [PATCH] Properly aligned the char array when using STRING_CTE macro. --- m-string.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/m-string.h b/m-string.h index 58c6a87b..208e4b07 100644 --- a/m-string.h +++ b/m-string.h @@ -687,10 +687,18 @@ string_in_str(string_t v, FILE *f) string_clear(tmp); \ } \ -/* Use of Compound Literals to init a constant string */ -#define STRING_CTE(s) \ - ((const string_t){{.size = sizeof(s)-1, .alloc = sizeof(s), \ - .ptr = s}}) +/* Use of Compound Literals to init a constant string. + NOTE: The use of the additional structure layer is to ensure + that the pointer to char is properly aligned to an int. + Otherwise it could have been : + #define STRING_CTE(s) \ + ((const string_t){{.size = sizeof(s)-1, .alloc = sizeof(s), \ + .ptr = s}}) + which produces faster code. +*/ +#define STRING_CTE(s) \ + ((const string_t){{.size = sizeof(s)-1, .alloc = sizeof(s), \ + .ptr = ((struct { int _n; char _d[sizeof (s)]; }){ 0, s })._d }}) #define STRING_INIT_PRINTF(v, format, ...) do { \ string_init (v); \