C
Chris Thomasson
Simple question, does the program below fall victim to undefined-behavior?
___________________________________________________________________
#include <stdio.h>
#include <assert.h>
typedef struct fooa_s fooa;
typedef struct foob_s foob;
struct fooa_s {
int whatever;
};
struct foob_s {
fooa fooa_;
fooa* pfooa;
};
#define FOOB_STATICINIT(mp_this) { \
{ 0 }, &(mp_this)->fooa_ \
}
static foob g_foob = FOOB_STATICINIT(&g_foob);
int main(void) {
printf("(%p/%p/%p) == (&g_foob/&g_foob.fooa_/g_foob.pfooa)\n",
(void*)&g_foob, (void*)&g_foob.fooa_, (void*)&g_foob.pfooa);
assert(&g_foob.fooa_ == g_foob.pfooa);
/*-------------------------------------------------------*/
puts("\n\n____________________________________________\n\
press <ENTER> to exit...");
getchar();
return 0;
}
___________________________________________________________________
___________________________________________________________________
#include <stdio.h>
#include <assert.h>
typedef struct fooa_s fooa;
typedef struct foob_s foob;
struct fooa_s {
int whatever;
};
struct foob_s {
fooa fooa_;
fooa* pfooa;
};
#define FOOB_STATICINIT(mp_this) { \
{ 0 }, &(mp_this)->fooa_ \
}
static foob g_foob = FOOB_STATICINIT(&g_foob);
int main(void) {
printf("(%p/%p/%p) == (&g_foob/&g_foob.fooa_/g_foob.pfooa)\n",
(void*)&g_foob, (void*)&g_foob.fooa_, (void*)&g_foob.pfooa);
assert(&g_foob.fooa_ == g_foob.pfooa);
/*-------------------------------------------------------*/
puts("\n\n____________________________________________\n\
press <ENTER> to exit...");
getchar();
return 0;
}
___________________________________________________________________