Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4033440
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:50:49+00:00 2026-05-20T11:50:49+00:00

The following declaration adds a couple of operators for compilation in a C++ file.

  • 0

The following declaration adds a couple of operators for compilation in a C++ file. The definition is included in both C and C++ files.

PC-Lint is reporting Error 114: Inconsistent structure declaration for tag ‘Rect’ but I am certain it is safe.

I’m compiling with Visual Studio 2008.

edit – adding the explanation I sent to my client

With regards to the Rect issue; how does knowing that the struct is the same size in C and C++ eliminate the suspicion of ‘undefined behaviour’.

Undefined behaviour occurs if the actual location of fields in the data structure varies according to compilation.

You have to think of all member variable access as ultimately resolving to a pointer, calculated by a pointer to the beginning of the object storage plus an offset depending on what is in that structure.

Packing and data alignment settings influence the value of the offset.

The compiler is allowed to re-order types for optimal access – it is undefined behaviour to assume that just because you declared two members in a given order that they are actually stored in that order. The only thing that declaration order guarantees is initialisation, copying and destruction order.

However, when you are talking about C and C++ compilation of a given structure within the same compiler, with the same offset settings, the chance of actual reordering is effectively zero.

Thus the only thing we need worry about is any difference in the offset of fields.

For a structure containing a simple 4 short integers, simply confirming that the C version and the C++ version are the same size guarantees that their offsets are all the same. To be more careful we can also check that the structure size = 4*sizeof(short).

I think it’s worth adding those checks but once that is done, there’s no need to refactor the code as would be required to use separate types in C and C++ (or to move the functions being used out into free functions).

/**
Mac-compatible rectangle type with some operators added for C++ use.
@ingroup QuickdrawPort
*/
struct Rect {
    short                           top;
    short                           left;
    short                           bottom;
    short                           right;

#ifdef __cplusplus
    Rect(short _top=0, short _left=0, short _bottom=0, short _right=0) :
        top(_top),
        left(_left),
        bottom(_bottom),
        right(_right)
    {}
    #ifdef _WINNT_  // WinDef.h has been included
        const Rect& operator=(const tagRECT& rhs) {
            top = short(rhs.top);
            left = short(rhs.left);
            bottom = short(rhs.bottom);
            right = short(rhs.right);
            return *this;
        }

        operator tagRECT() const {
            tagRECT lhs;
            lhs.top = top;
            lhs.left = left;
            lhs.bottom = bottom;
            lhs.right = right;
            return lhs;
        }

    #endif// _WINNT_
    short height() const { return bottom - top; }
    short width() const { return right - left; }
    bool empty() const { return right==left || bottom==top; }

    bool operator==(const Rect& rhs) const {
        return top == rhs.top &&
            left == rhs.left && 
            bottom == rhs.bottom &&
            right == rhs.right;
    }
#endif  
};
#ifndef __cplusplus
typedef struct Rect Rect;
#endif
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:50:50+00:00Added an answer on May 20, 2026 at 11:50 am

    Am I correct to assume that the header file containing this definition is included in several translation units, some of which are compiled as C++, and some — as plain C?

    If it is true, then you have an ODR violation, which, according to the C++ standard, invokes undefined behavior.

    There are two ways to deal with it.

    1. Ignore this instance of undefined behavior, provided that you know how exactly it manifests on platform(s) you need to support. (Note that undefined behavior can manifest as a correctly working program). To tell the truth, with most compilers you will not have a problem here. However, you must understand that this code works by happenstance, not by the law. Compilers are permitted to use different layout for classes with member functions than without them. (I’m willing to bet that this code will break on CINT, for example).

    2. Eliminate undefined behavior. I’d suggest you to go this way. There are several possibilities to do it. For example, you can inherit “C++ Rect” from “C Rect”, keeping the latter as a plain struct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing to a text file using the following declaration: void create_out_file(char file_name[],long double
I have the following declaration in my build.xml file, in my src folder I
Why do i receive error in the following declaration ? List<int> intrs = new
I have the following declaration of my service: <ServiceControl Id=ServiceStartStop Name=[name] Start=install Stop=both Remove=both
I have a managed function with the following declaration (both interface and implementation): [return:
I am getting Initializer Element is not a constant error for the following declaration.
I have the following declaration in a header file: struct my_struct; int func(struct my_struct*
I'm getting inconsistent accessibility error in the following declaration: public static class Helper {
Consider the following declaration as part of SomeClass private Set<String> blah = new HashSet<String>();
I have a function with the following declaration: void cleanValid(int valid[][4], int &size, int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.