最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Unnamed struct ABI (C library, C++ users) - Stack Overflow

programmeradmin5浏览0评论

In a C library header I have a struct defined like this:

typedef struct
{
  int bar;
} foo;

I would like to change the definition so that the struct is tagged, like this:

typedef struct foo
{
  int bar;
} foo;

Does this change the ABI for C++ user of the library? Or even worse, break old sources in some way?

In a C library header I have a struct defined like this:

typedef struct
{
  int bar;
} foo;

I would like to change the definition so that the struct is tagged, like this:

typedef struct foo
{
  int bar;
} foo;

Does this change the ABI for C++ user of the library? Or even worse, break old sources in some way?

Share Improve this question edited Jan 19 at 12:41 ThePirate42 asked Jan 16 at 20:43 ThePirate42ThePirate42 8926 silver badges24 bronze badges 14
  • 3 [C++] Unless you can amend all occurrences this will break the One Definition Rule, which for C++ is "...Each definition consists of the same sequence of tokens..." – Richard Critten Commented Jan 16 at 20:50
  • 3 Why do you need to do this? – Barmar Commented Jan 16 at 20:54
  • 2 Not exactly your question, but there is the possible perverse case where the C code also has a struct foo { float potato;} ; Then this would make a bad situation totally broken. – Avi Berger Commented Jan 16 at 21:18
  • 1 @EricPostpischil Since they asked about ABI compatibility, I think that implies that they're not planning on recompiling all the users. – Barmar Commented Jan 16 at 21:45
  • 3 C and C++ have different naming rules here. Please focus on one language. – Lundin Commented Jan 17 at 7:26
 |  Show 9 more comments

1 Answer 1

Reset to default -1

Updating the struct definition to include a tag does not impact the ABI for C or C++ users of the library. This is because the memory layout, size, and alignment of the struct remain unchanged. Adding a tag is purely a syntactical change, providing a name for the struct without altering its underlying representation or behavior.

发布评论

评论列表(0)

  1. 暂无评论