472,985 Members | 2,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,985 software developers and data experts.

Array definition and extern declaration with different size.

Hi,

I have an array defined in one file with an intializer as follows:

int arr[] = {0, 1, 2, 3};

I have a declaration of the array in another file as follows:

extern int arr[10];

This compiles without a problem on my implementation and arr ends up
being of size 10 on my implementation. Is this legal in "standard C"? I
was reading question 1.24 of the FAQ but that didn't seem to answer my
question in this case. Is the array definition with the initializer
somehow an incomplete definition?

Thanks for the help,

Josh

Jul 31 '06 #1
9 9328
I think it compiles OK, but may cause segmentation fault when run.
"joshc" <jo********@gmail.com>
??????:11*********************@m79g2000cwm.googleg roups.com...
Hi,

I have an array defined in one file with an intializer as follows:

int arr[] = {0, 1, 2, 3};

I have a declaration of the array in another file as follows:

extern int arr[10];

This compiles without a problem on my implementation and arr ends up
being of size 10 on my implementation. Is this legal in "standard C"? I
was reading question 1.24 of the FAQ but that didn't seem to answer my
question in this case. Is the array definition with the initializer
somehow an incomplete definition?

Thanks for the help,

Josh

Jul 31 '06 #2
On 30 Jul 2006 20:48:10 -0700, "joshc" <jo********@gmail.comwrote in
comp.lang.c:
Hi,

I have an array defined in one file with an intializer as follows:

int arr[] = {0, 1, 2, 3};

I have a declaration of the array in another file as follows:

extern int arr[10];

This compiles without a problem on my implementation and arr ends up
being of size 10 on my implementation. Is this legal in "standard C"? I
was reading question 1.24 of the FAQ but that didn't seem to answer my
question in this case. Is the array definition with the initializer
somehow an incomplete definition?

Thanks for the help,

Josh
Any size specified in the external declaration is ignored by the
compiler. The array contains four ints. Any attempt to access past
arr [3] produces undefined behavior.

This is the same as using a size in an array style parameter
definition (an unfortunate choice anyway), with the exception of the
C99 feature where the "static" keyword is used.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 31 '06 #3
Jack Klein wrote:
On 30 Jul 2006 20:48:10 -0700, "joshc" <jo********@gmail.comwrote in
comp.lang.c:
Hi,

I have an array defined in one file with an intializer as follows:

int arr[] = {0, 1, 2, 3};

I have a declaration of the array in another file as follows:

extern int arr[10];

This compiles without a problem on my implementation and arr ends up
being of size 10 on my implementation. Is this legal in "standard C"? I
was reading question 1.24 of the FAQ but that didn't seem to answer my
question in this case. Is the array definition with the initializer
somehow an incomplete definition?

Thanks for the help,

Josh

Any size specified in the external declaration is ignored by the
compiler. The array contains four ints. Any attempt to access past
arr [3] produces undefined behavior.

This is the same as using a size in an array style parameter
definition (an unfortunate choice anyway), with the exception of the
C99 feature where the "static" keyword is used.
That may be what happens for some compilers, but it is not what the
standard says, and if I recall correctly, there are implementations
that would display a warning or error message for such code during
linking.

n1124 6.2.7#2:
"All declarations that refer to the same object or function shall have
compatible type; otherwise, the behavior is undefined."

n1124 6.7.5.2#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."

Jul 31 '06 #4
Harald van Dijk wrote:
n1124 6.2.7#2:
"All declarations that refer to the same object or function shall have
compatible type; otherwise, the behavior is undefined."

n1124 6.7.5.2#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 havethe
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."
What does this tell us for the case where one of the size specifiers
is not present ?

Jul 31 '06 #5
Harald van Dijk wrote:
Jack Klein wrote:
On 30 Jul 2006 20:48:10 -0700, "joshc" <jo********@gmail.comwrote in
comp.lang.c:
Hi,
>
I have an array defined in one file with an intializer as follows:
>
int arr[] = {0, 1, 2, 3};
>
I have a declaration of the array in another file as follows:
>
extern int arr[10];
>
This compiles without a problem on my implementation and arr ends up
being of size 10 on my implementation. Is this legal in "standard C"?I
was reading question 1.24 of the FAQ but that didn't seem to answer my
question in this case. Is the array definition with the initializer
somehow an incomplete definition?
>
Thanks for the help,
>
Josh
Any size specified in the external declaration is ignored by the
compiler. The array contains four ints. Any attempt to access past
arr [3] produces undefined behavior.

This is the same as using a size in an array style parameter
definition (an unfortunate choice anyway), with the exception of the
C99 feature where the "static" keyword is used.

That may be what happens for some compilers, but it is not what the
standard says, and if I recall correctly, there are implementations
that would display a warning or error message for such code during
linking.

n1124 6.2.7#2:
"All declarations that refer to the same object or function shall have
compatible type; otherwise, the behavior is undefined."

n1124 6.7.5.2#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 havethe
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."
Hmm. Re-reading it, though, I think you may technically be right for
this particular case, but not for "int arr[4] = {0, 1, 2, 3};"

Both 'int arr[]' and 'extern int arr[10]' have compatible (identical,
even) element type, and even though "int arr[] = {0, 1, 2, 3};"
declares arr as int [4] (per 6.7.8#22), it is not defined with a size
specifier (a syntactic construct), so it seems the behaviour is not
actually undefined. Maybe comp.std.c can help out.

Jul 31 '06 #6
sp****@gmail.com wrote:
Harald van Dijk wrote:
n1124 6.2.7#2:
"All declarations that refer to the same object or function shall have
compatible type; otherwise, the behavior is undefined."

n1124 6.7.5.2#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."

What does this tell us for the case where one of the size specifiers
is not present ?
Yeah, I noticed that too after posting. "int arr[] = { 0, 1, 2, 3 };"
defines arr as int[4], and (&arr) + 1 is allowed, though, so it's meant
to be undefined, I think, but it actually isn't in this case.

Jul 31 '06 #7

Jack Klein wrote:
On 30 Jul 2006 20:48:10 -0700, "joshc" <jo********@gmail.comwrote in
comp.lang.c:
Hi,

I have an array defined in one file with an intializer as follows:

int arr[] = {0, 1, 2, 3};

I have a declaration of the array in another file as follows:

extern int arr[10];

This compiles without a problem on my implementation and arr ends up
being of size 10 on my implementation. Is this legal in "standard C"? I
was reading question 1.24 of the FAQ but that didn't seem to answer my
question in this case. Is the array definition with the initializer
somehow an incomplete definition?

Thanks for the help,

Josh

Any size specified in the external declaration is ignored by the
compiler. The array contains four ints. Any attempt to access past
arr [3] produces undefined behavior.

This is the same as using a size in an array style parameter
definition (an unfortunate choice anyway), with the exception of the
C99 feature where the "static" keyword is used.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Thanks, Jack. I figured it was probably just how my compiler behaved
but I was surprised that the space allocated for the array was based on
the external declaration. I was expecting a warning but I guess that's
not required.

Thanks again.

Jul 31 '06 #8
On Mon, 31 Jul 2006 03:48:10 UTC, "joshc" <jo********@gmail.com>
wrote:
Hi,

I have an array defined in one file with an intializer as follows:

int arr[] = {0, 1, 2, 3};
This defines an array of 4 members. When the number of members defined
through the initialisers is too low you must define the real size to
get the list of initialisers expanded to the real number of members
you needs.
I have a declaration of the array in another file as follows:

extern int arr[10];
The best way would be:

insert the declaration into an header file and include it even on the
file you has to write the definition.

This will give the compiler the chance to extend the definition [] to
the size the declaration defines. On the other hand you can simply use
'extern int arr[]' in other translation units.
So 'int arr[ARR_SIZE] = { [incomplete] list of initialisers }; and
'extern arr[ARR_SIZE]' in a common header while ARR_SIZE describes the
size you needs really will give you in any place the chance to change
the arraysize only on one place.
This compiles without a problem on my implementation and arr ends up
being of size 10 on my implementation. Is this legal in "standard C"? I
was reading question 1.24 of the FAQ but that didn't seem to answer my
question in this case. Is the array definition with the initializer
somehow an incomplete definition?

Thanks for the help,

Josh

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Jul 31 '06 #9
sp****@gmail.com wrote:
>
Harald van D??k wrote:
>>
n1124 6.7.5.2#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."

What does this tell us for the case where one of the size specifiers
is not present ?
It tells you that the committee isn't infallible (in case you hadn't
already discovered that). I'm sure the intent is that a size imputed
from an initialization behave just like a constant size, but the
document doesn't actually say so.

-Larry Jones

Don't you hate it when your boogers freeze? -- Calvin
Aug 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
8
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line...
12
by: ur8x | last post by:
Why does this declaration give undefined result: file1: extern char * p; file2: char p; Let's assume p has been initialized, now accessing p...
5
by: Matthew Jakeman | last post by:
Hi, if i define an int array globally as follows : int test ; Is it possible to set the size of this array inside a function later in the code ? TIA Matt
7
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
13
by: Salvatore Di Fazio | last post by:
Hi, I've an array in an include that is used everywhere in the project. So to avoid the problem of declaration I made the following solution: ifndef __UNWALKABLETILES__ #define...
9
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.