Reputation: 393
I have the below code and I see that the two variables have been assigned with same address. Both the variables are completely different type . Is there anyway I can void this ? And under what circumstances does the same memory gets allocated to both the variables .
static int Sw_Type [];
static BOOL Sw_Update;
void main()
{
int i;
int bytes = 3;
if (Sw_Update!= TRUE)
{
for(i = 0; i< bytes ;i++)
{
Sw_Type [i] = *Ver_Value;
Ver_Value++;
}
Sw_Update= TRUE;
}
}
This is a snippet of my code and "Ver_Value" is a structure which gets assigned in different function.
So the problem I am seeing is , when Sw_Update gets updated, Sw_Type [1] is getting updated and I see these two have same memory address.
Upvotes: 0
Views: 174
Reputation: 224082
static int Sw_Type [];
constitutes a tentative definition, per C 2018 6.9.2 2:
A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.
Since your program provides no non-tentative definition, it is as if it ended with static int Sw_Type [] = { 0 };
. (In case it is not clear from the text quoted above that the result is indeed an array of one element, it is made clear by Example 2 in paragraph 5 of the same clause.)
Thus, Sw_Type
is an array of one int
. It contains only the element Sw_Type[0]
. The behavior of accessing Sw_Type[1]
is not defined by the C standard. From the observations you report, it appears as if Sw_Update
follows Sw_Type
in memory, and accessing Sw_Type[1]
results in modifying Sw_Update
. This behavior is of course not reliable.
To make Sw_Type
larger, you must declare a size for it, as with static int Sw_Type[4];
.
Note: 6.9.2 3 says “If the declaration of an identifier for an object is a tentative definition and has internal linkage, the declared type shall not be an incomplete type.” While this might be read as applying to the declared type in each declaration that is a tentative definition, I think it might be intended to apply to the declared type of the object once its composite type is fully resolved at the end of the translation unit. Experimentally, Clang is okay with accepting an incomplete type at first and completing it later.
Upvotes: 4
Reputation: 182847
So the problem I am seeing is , when Sw_Update gets updated, Sw_Type [1] is getting updated and I see these two have same memory address.
There is no Sw_Type [1]
. Only an array with two or more elements has a second entry, and Sw_Type
is not an array with two or more elements. Accessing an array out of bounds can certainly stomp on other objects.
Upvotes: 1