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

  • Home
  • SEARCH
  • 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 9274189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:17:49+00:00 2026-06-18T16:17:49+00:00

Suppose I have two files: ==File1== extern char* foo; ==File2== double foo; These two

  • 0

Suppose I have two files:

==File1==

extern char* foo;

==File2==

double foo;

These two files seem to compile and link just fine with both g++ and clang++ despite the type mismatch. As I understand it the recommended practice is to put the extern declaration in a header which both files include so File2 will throw a redefinition error.

My questions are:

  • Does this result in undefined behavior according to the c++ standard? If not what goes in foo in File1?
  • Could linkers catch this kind of type mismatch?
  • 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-06-18T16:17:50+00:00Added an answer on June 18, 2026 at 4:17 pm

    Does this result in undefined behavior according to the c++ standard?

    Well, the real question is whether this is undefined behavior or whether it is specified by the standard as being ill-formed (in standard parlance). Because, obviously, it is not correct. I have tried to find something from the standard about this, but to no avail. However, in a number of similar situations, e.g., mismatches of decl/def or throwing funky things at the linker (see section 3.5, 7.5, or search for “extern” or “linkage”), the standard generally ends up saying:

    The program is ill-formed, no diagnosis required.

    So, I would bet it’s pretty safe to assume this is the case here too. This would mean that this is erroneous code, worst than “undefined behavior”, since UB often will have some kind of reasonable behaviour for a specific implementation (although you shouldn’t speculate on what that behaviour would be, and certainly not rely on that speculation). The “ill-formed” term is used very liberally in the standard, and you can more or less infer that it means that the code is FUBAR. This would also mean that the linker is not required, by the standard, to be implemented in a way that allows it to catch this kind of error, and that’s why it compiles and links correctly, but hold on to your socks when you run it.

    Could linkers catch this kind of type mismatch?

    In theory, yes. A linker implementation could encode (with name-mangling) the type of the variable into its external symbol, and thus be able to either restrict linkage to things whose types match (e.g., like overloaded functions), or throw a diagnosis (error) when it encounters a mismatch in types. I think that the former would be too permissive as compared to the standard.

    However, all compilers that I know of do not mangle the names of variables, and thus, you can assume that such a mismatched is “ill-formed, no diagnosis required”.

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

Sidebar

Related Questions

I have two html files namely file1.html and file2.html. File1 is supposed to encode
Suppose I create a symbolic link file2 to a file file1 two directories above
Let us say I have two relatively large tab-delimited files file1.txt, file2.txt. file1.txt id\tcity\tcar\ttype\tmodel
Suppose I have two XML files. First XML File: <?xml version=1.0?> <AccessRequest xml:lang=en-US> <AccessLicenseNumber>Your_License</AccessLicenseNumber>
Suppose i have one folder and inside that another two files are there .
It's a theorical doubt. Suppouse you have a little library with just two files:
Suppose we have two tables Foo and Bar. I have an association table Foo_Bar
Suppose I have two files containing styles, a.css and b.css. I import them into
Suppose I have two .h files: A.h and B.h. Moreover, A.h includes B.h itself:
This is a scjp mock exam question. Suppose I have the following two files:

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.