Does the confliction between declaration and definition cause undefined behavior?
For example, in one source file:
extern int a[10];
int main()
{
(void)sizeof(a);
return 0;
}
and in a second source file we have:
int a[20];//different with the first source
Does the code above cause undefined behavior? As far as I know, in C it says:
All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined.
Are int[10]
and int[20]
compatible(in c
's view)? And what about C++ standard?
Besides, if there is no second source file, is it legal to use sizeof(a)
(a
has just a declaration) ?
c++ c language-lawyer undefined-behavior
|
show 4 more comments
For example, in one source file:
extern int a[10];
int main()
{
(void)sizeof(a);
return 0;
}
and in a second source file we have:
int a[20];//different with the first source
Does the code above cause undefined behavior? As far as I know, in C it says:
All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined.
Are int[10]
and int[20]
compatible(in c
's view)? And what about C++ standard?
Besides, if there is no second source file, is it legal to use sizeof(a)
(a
has just a declaration) ?
c++ c language-lawyer undefined-behavior
1
1. Yes it does cause UB 2. Yes it is legal.
– n.m.
Nov 27 '18 at 4:24
@n.m. Does standard ever covered this situation? I cannot find that.
– bigxiao
Nov 27 '18 at 4:25
4
You're asking about 2 languages, so this is like 3 questions in one
– Antti Haapala
Nov 27 '18 at 5:21
2
@AnttiHaapala I believe it is a valid question, the OP has some notion of what it says in C and wants to know if it is undefined behavior and also wants to understand if it applies to C++. It might be worded better.
– Shafik Yaghmour
Nov 27 '18 at 5:25
1
@ShafikYaghmour bad precedent. The current tag wiki says: "A question should be tagged with both c and c++ if it is about: * Specific differences between C and C++". This is the question 3. The questions 1 and 2 are the C++ and C questions whether these definitions are compatible.
– Antti Haapala
Nov 27 '18 at 5:34
|
show 4 more comments
For example, in one source file:
extern int a[10];
int main()
{
(void)sizeof(a);
return 0;
}
and in a second source file we have:
int a[20];//different with the first source
Does the code above cause undefined behavior? As far as I know, in C it says:
All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined.
Are int[10]
and int[20]
compatible(in c
's view)? And what about C++ standard?
Besides, if there is no second source file, is it legal to use sizeof(a)
(a
has just a declaration) ?
c++ c language-lawyer undefined-behavior
For example, in one source file:
extern int a[10];
int main()
{
(void)sizeof(a);
return 0;
}
and in a second source file we have:
int a[20];//different with the first source
Does the code above cause undefined behavior? As far as I know, in C it says:
All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined.
Are int[10]
and int[20]
compatible(in c
's view)? And what about C++ standard?
Besides, if there is no second source file, is it legal to use sizeof(a)
(a
has just a declaration) ?
c++ c language-lawyer undefined-behavior
c++ c language-lawyer undefined-behavior
edited Nov 27 '18 at 7:34
Lundin
110k17161265
110k17161265
asked Nov 27 '18 at 4:16
bigxiaobigxiao
1,621923
1,621923
1
1. Yes it does cause UB 2. Yes it is legal.
– n.m.
Nov 27 '18 at 4:24
@n.m. Does standard ever covered this situation? I cannot find that.
– bigxiao
Nov 27 '18 at 4:25
4
You're asking about 2 languages, so this is like 3 questions in one
– Antti Haapala
Nov 27 '18 at 5:21
2
@AnttiHaapala I believe it is a valid question, the OP has some notion of what it says in C and wants to know if it is undefined behavior and also wants to understand if it applies to C++. It might be worded better.
– Shafik Yaghmour
Nov 27 '18 at 5:25
1
@ShafikYaghmour bad precedent. The current tag wiki says: "A question should be tagged with both c and c++ if it is about: * Specific differences between C and C++". This is the question 3. The questions 1 and 2 are the C++ and C questions whether these definitions are compatible.
– Antti Haapala
Nov 27 '18 at 5:34
|
show 4 more comments
1
1. Yes it does cause UB 2. Yes it is legal.
– n.m.
Nov 27 '18 at 4:24
@n.m. Does standard ever covered this situation? I cannot find that.
– bigxiao
Nov 27 '18 at 4:25
4
You're asking about 2 languages, so this is like 3 questions in one
– Antti Haapala
Nov 27 '18 at 5:21
2
@AnttiHaapala I believe it is a valid question, the OP has some notion of what it says in C and wants to know if it is undefined behavior and also wants to understand if it applies to C++. It might be worded better.
– Shafik Yaghmour
Nov 27 '18 at 5:25
1
@ShafikYaghmour bad precedent. The current tag wiki says: "A question should be tagged with both c and c++ if it is about: * Specific differences between C and C++". This is the question 3. The questions 1 and 2 are the C++ and C questions whether these definitions are compatible.
– Antti Haapala
Nov 27 '18 at 5:34
1
1
1. Yes it does cause UB 2. Yes it is legal.
– n.m.
Nov 27 '18 at 4:24
1. Yes it does cause UB 2. Yes it is legal.
– n.m.
Nov 27 '18 at 4:24
@n.m. Does standard ever covered this situation? I cannot find that.
– bigxiao
Nov 27 '18 at 4:25
@n.m. Does standard ever covered this situation? I cannot find that.
– bigxiao
Nov 27 '18 at 4:25
4
4
You're asking about 2 languages, so this is like 3 questions in one
– Antti Haapala
Nov 27 '18 at 5:21
You're asking about 2 languages, so this is like 3 questions in one
– Antti Haapala
Nov 27 '18 at 5:21
2
2
@AnttiHaapala I believe it is a valid question, the OP has some notion of what it says in C and wants to know if it is undefined behavior and also wants to understand if it applies to C++. It might be worded better.
– Shafik Yaghmour
Nov 27 '18 at 5:25
@AnttiHaapala I believe it is a valid question, the OP has some notion of what it says in C and wants to know if it is undefined behavior and also wants to understand if it applies to C++. It might be worded better.
– Shafik Yaghmour
Nov 27 '18 at 5:25
1
1
@ShafikYaghmour bad precedent. The current tag wiki says: "A question should be tagged with both c and c++ if it is about: * Specific differences between C and C++". This is the question 3. The questions 1 and 2 are the C++ and C questions whether these definitions are compatible.
– Antti Haapala
Nov 27 '18 at 5:34
@ShafikYaghmour bad precedent. The current tag wiki says: "A question should be tagged with both c and c++ if it is about: * Specific differences between C and C++". This is the question 3. The questions 1 and 2 are the C++ and C questions whether these definitions are compatible.
– Antti Haapala
Nov 27 '18 at 5:34
|
show 4 more comments
2 Answers
2
active
oldest
votes
Yes this is undefined behavior in C, they are not compatible, see C11 6.7.6.2 Array declarators paragraph 6
For two array types to be compatible, both shall have compatible
element types, and if both size specifiers are present, and are
integer constant expressions, then both size specifiers shall have the
same constant value. If the two array types are used in a context
which requires them to be compatible, it is undefined behavior if the
two size specifiers evaluate to unequal values.
and ill-formed no diagnostic required in C++ from [basic.link]p11:
After all adjustments of types (during which typedefs are replaced by their definitions), the types specified by all declarations referring to a given variable or function shall be identical, except that declarations for an array object can specify array types that differ by the presence or absence of a major array bound ([dcl.array]).
A violation of this rule on type identity does not require a diagnostic.
add a comment |
Some implementations will treat imported and exported symbols according to the rules defined by a set of conventions for the platform which are typically described in a document which is, in modern parlance, called the Application Binary Interface. If one module (compilation unit) exports an symbol and another imports it, each definition and their interaction will behave as described by the ABI, regardless of the rules of the language of the code that imports or exports the symbols.
Other implementations, especially those using Whole Program optimization, may treat symbols which are exported from one C compilation unit and used by another in a fashion different from what the ABI would suggest, especially if doing so would allow things to be more efficient, or allow a build system to produce better diagnostics.
Because both approaches have substantial advantages and disadvantages, the Standard is agnostic as to how implementations handle the interactions between functions in different modules. If a platform ABI happens to define a behavior, and a C implementation documents that C modules interact in the fashion defined by the ABI, then behavior would be defined on that implementation regardless of whether the Standard imposes any requirements. In other cases where the Standard imposes no requirements, anything could happen.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492665%2fdoes-the-confliction-between-declaration-and-definition-cause-undefined-behavior%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yes this is undefined behavior in C, they are not compatible, see C11 6.7.6.2 Array declarators paragraph 6
For two array types to be compatible, both shall have compatible
element types, and if both size specifiers are present, and are
integer constant expressions, then both size specifiers shall have the
same constant value. If the two array types are used in a context
which requires them to be compatible, it is undefined behavior if the
two size specifiers evaluate to unequal values.
and ill-formed no diagnostic required in C++ from [basic.link]p11:
After all adjustments of types (during which typedefs are replaced by their definitions), the types specified by all declarations referring to a given variable or function shall be identical, except that declarations for an array object can specify array types that differ by the presence or absence of a major array bound ([dcl.array]).
A violation of this rule on type identity does not require a diagnostic.
add a comment |
Yes this is undefined behavior in C, they are not compatible, see C11 6.7.6.2 Array declarators paragraph 6
For two array types to be compatible, both shall have compatible
element types, and if both size specifiers are present, and are
integer constant expressions, then both size specifiers shall have the
same constant value. If the two array types are used in a context
which requires them to be compatible, it is undefined behavior if the
two size specifiers evaluate to unequal values.
and ill-formed no diagnostic required in C++ from [basic.link]p11:
After all adjustments of types (during which typedefs are replaced by their definitions), the types specified by all declarations referring to a given variable or function shall be identical, except that declarations for an array object can specify array types that differ by the presence or absence of a major array bound ([dcl.array]).
A violation of this rule on type identity does not require a diagnostic.
add a comment |
Yes this is undefined behavior in C, they are not compatible, see C11 6.7.6.2 Array declarators paragraph 6
For two array types to be compatible, both shall have compatible
element types, and if both size specifiers are present, and are
integer constant expressions, then both size specifiers shall have the
same constant value. If the two array types are used in a context
which requires them to be compatible, it is undefined behavior if the
two size specifiers evaluate to unequal values.
and ill-formed no diagnostic required in C++ from [basic.link]p11:
After all adjustments of types (during which typedefs are replaced by their definitions), the types specified by all declarations referring to a given variable or function shall be identical, except that declarations for an array object can specify array types that differ by the presence or absence of a major array bound ([dcl.array]).
A violation of this rule on type identity does not require a diagnostic.
Yes this is undefined behavior in C, they are not compatible, see C11 6.7.6.2 Array declarators paragraph 6
For two array types to be compatible, both shall have compatible
element types, and if both size specifiers are present, and are
integer constant expressions, then both size specifiers shall have the
same constant value. If the two array types are used in a context
which requires them to be compatible, it is undefined behavior if the
two size specifiers evaluate to unequal values.
and ill-formed no diagnostic required in C++ from [basic.link]p11:
After all adjustments of types (during which typedefs are replaced by their definitions), the types specified by all declarations referring to a given variable or function shall be identical, except that declarations for an array object can specify array types that differ by the presence or absence of a major array bound ([dcl.array]).
A violation of this rule on type identity does not require a diagnostic.
edited Nov 27 '18 at 5:20
answered Nov 27 '18 at 5:12
Shafik YaghmourShafik Yaghmour
126k23326541
126k23326541
add a comment |
add a comment |
Some implementations will treat imported and exported symbols according to the rules defined by a set of conventions for the platform which are typically described in a document which is, in modern parlance, called the Application Binary Interface. If one module (compilation unit) exports an symbol and another imports it, each definition and their interaction will behave as described by the ABI, regardless of the rules of the language of the code that imports or exports the symbols.
Other implementations, especially those using Whole Program optimization, may treat symbols which are exported from one C compilation unit and used by another in a fashion different from what the ABI would suggest, especially if doing so would allow things to be more efficient, or allow a build system to produce better diagnostics.
Because both approaches have substantial advantages and disadvantages, the Standard is agnostic as to how implementations handle the interactions between functions in different modules. If a platform ABI happens to define a behavior, and a C implementation documents that C modules interact in the fashion defined by the ABI, then behavior would be defined on that implementation regardless of whether the Standard imposes any requirements. In other cases where the Standard imposes no requirements, anything could happen.
add a comment |
Some implementations will treat imported and exported symbols according to the rules defined by a set of conventions for the platform which are typically described in a document which is, in modern parlance, called the Application Binary Interface. If one module (compilation unit) exports an symbol and another imports it, each definition and their interaction will behave as described by the ABI, regardless of the rules of the language of the code that imports or exports the symbols.
Other implementations, especially those using Whole Program optimization, may treat symbols which are exported from one C compilation unit and used by another in a fashion different from what the ABI would suggest, especially if doing so would allow things to be more efficient, or allow a build system to produce better diagnostics.
Because both approaches have substantial advantages and disadvantages, the Standard is agnostic as to how implementations handle the interactions between functions in different modules. If a platform ABI happens to define a behavior, and a C implementation documents that C modules interact in the fashion defined by the ABI, then behavior would be defined on that implementation regardless of whether the Standard imposes any requirements. In other cases where the Standard imposes no requirements, anything could happen.
add a comment |
Some implementations will treat imported and exported symbols according to the rules defined by a set of conventions for the platform which are typically described in a document which is, in modern parlance, called the Application Binary Interface. If one module (compilation unit) exports an symbol and another imports it, each definition and their interaction will behave as described by the ABI, regardless of the rules of the language of the code that imports or exports the symbols.
Other implementations, especially those using Whole Program optimization, may treat symbols which are exported from one C compilation unit and used by another in a fashion different from what the ABI would suggest, especially if doing so would allow things to be more efficient, or allow a build system to produce better diagnostics.
Because both approaches have substantial advantages and disadvantages, the Standard is agnostic as to how implementations handle the interactions between functions in different modules. If a platform ABI happens to define a behavior, and a C implementation documents that C modules interact in the fashion defined by the ABI, then behavior would be defined on that implementation regardless of whether the Standard imposes any requirements. In other cases where the Standard imposes no requirements, anything could happen.
Some implementations will treat imported and exported symbols according to the rules defined by a set of conventions for the platform which are typically described in a document which is, in modern parlance, called the Application Binary Interface. If one module (compilation unit) exports an symbol and another imports it, each definition and their interaction will behave as described by the ABI, regardless of the rules of the language of the code that imports or exports the symbols.
Other implementations, especially those using Whole Program optimization, may treat symbols which are exported from one C compilation unit and used by another in a fashion different from what the ABI would suggest, especially if doing so would allow things to be more efficient, or allow a build system to produce better diagnostics.
Because both approaches have substantial advantages and disadvantages, the Standard is agnostic as to how implementations handle the interactions between functions in different modules. If a platform ABI happens to define a behavior, and a C implementation documents that C modules interact in the fashion defined by the ABI, then behavior would be defined on that implementation regardless of whether the Standard imposes any requirements. In other cases where the Standard imposes no requirements, anything could happen.
answered Nov 27 '18 at 21:26
supercatsupercat
57.1k3117151
57.1k3117151
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492665%2fdoes-the-confliction-between-declaration-and-definition-cause-undefined-behavior%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
1. Yes it does cause UB 2. Yes it is legal.
– n.m.
Nov 27 '18 at 4:24
@n.m. Does standard ever covered this situation? I cannot find that.
– bigxiao
Nov 27 '18 at 4:25
4
You're asking about 2 languages, so this is like 3 questions in one
– Antti Haapala
Nov 27 '18 at 5:21
2
@AnttiHaapala I believe it is a valid question, the OP has some notion of what it says in C and wants to know if it is undefined behavior and also wants to understand if it applies to C++. It might be worded better.
– Shafik Yaghmour
Nov 27 '18 at 5:25
1
@ShafikYaghmour bad precedent. The current tag wiki says: "A question should be tagged with both c and c++ if it is about: * Specific differences between C and C++". This is the question 3. The questions 1 and 2 are the C++ and C questions whether these definitions are compatible.
– Antti Haapala
Nov 27 '18 at 5:34