两个静态变量的相同内存位置分配
Same memory location assignment for two static 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;
}
}
这是我的代码片段,"Ver_Value" 是一个分配给不同函数的结构。
所以我看到的问题是,当 Sw_Update 更新时,Sw_Type [1] 正在更新,我看到这两个具有相同的内存地址。
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.
没有Sw_Type [1]
。只有包含两个或更多元素的数组才有第二个条目,而 Sw_Type
不是包含两个或更多元素的数组。越界访问数组肯定会影响其他对象。
static int Sw_Type [];
构成 暂定定义 ,根据 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.
由于您的程序没有提供非暂定定义,因此就好像它以 static int Sw_Type [] = { 0 };
结尾一样。 (如果从上面引用的文本中不清楚结果确实是一个元素的数组,则在同一条款的第 5 段中的示例 2 中明确了。)
因此,Sw_Type
是一个 int
的数组。它仅包含元素 Sw_Type[0]
。 C 标准未定义访问 Sw_Type[1]
的行为。根据您报告的观察结果,似乎 Sw_Update
在内存中跟在 Sw_Type
之后,访问 Sw_Type[1]
会导致修改 Sw_Update
。这种行为当然不靠谱。
要使 Sw_Type
变大,您必须为其声明一个大小,与 static int Sw_Type[4];
一样。
注:6.9.2 3说“如果一个对象的标识符声明是一个暂定定义并且有内部链接,声明的类型不应是不完整的类型。”虽然这可能被解读为适用于每个声明中声明的类型,这是一个暂定定义,但我认为一旦它的复合类型在翻译单元的末尾被完全解析,它可能适用于对象的声明类型。实验上,Clang 可以先接受不完整的类型,然后再完成它。
我有下面的代码,我看到这两个变量被分配了相同的地址。这两个变量是完全不同的类型。无论如何我可以取消这个吗?在什么情况下相同的内存被分配给两个变量。
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;
}
}
这是我的代码片段,"Ver_Value" 是一个分配给不同函数的结构。
所以我看到的问题是,当 Sw_Update 更新时,Sw_Type [1] 正在更新,我看到这两个具有相同的内存地址。
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.
没有Sw_Type [1]
。只有包含两个或更多元素的数组才有第二个条目,而 Sw_Type
不是包含两个或更多元素的数组。越界访问数组肯定会影响其他对象。
static int Sw_Type [];
构成 暂定定义 ,根据 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.
由于您的程序没有提供非暂定定义,因此就好像它以 static int Sw_Type [] = { 0 };
结尾一样。 (如果从上面引用的文本中不清楚结果确实是一个元素的数组,则在同一条款的第 5 段中的示例 2 中明确了。)
因此,Sw_Type
是一个 int
的数组。它仅包含元素 Sw_Type[0]
。 C 标准未定义访问 Sw_Type[1]
的行为。根据您报告的观察结果,似乎 Sw_Update
在内存中跟在 Sw_Type
之后,访问 Sw_Type[1]
会导致修改 Sw_Update
。这种行为当然不靠谱。
要使 Sw_Type
变大,您必须为其声明一个大小,与 static int Sw_Type[4];
一样。
注:6.9.2 3说“如果一个对象的标识符声明是一个暂定定义并且有内部链接,声明的类型不应是不完整的类型。”虽然这可能被解读为适用于每个声明中声明的类型,这是一个暂定定义,但我认为一旦它的复合类型在翻译单元的末尾被完全解析,它可能适用于对象的声明类型。实验上,Clang 可以先接受不完整的类型,然后再完成它。